Claw 1.7.3
socket_stream.tpp
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
25/**
26 * \file socket_stream.tpp
27 * \brief Implementation of the claw::net::basic_socket_stream class.
28 * \author Julien Jorge
29 */
30
31/*----------------------------------------------------------------------------*/
32/**
33 * \brief Constructor.
34 * \param read_delay Number of second to wait before considering nothing will
35 * come in the socket. Negative values mean infinity.
36 */
37template<typename CharT, typename Traits>
38claw::net::basic_socket_stream<CharT, Traits>::basic_socket_stream
39( int read_delay )
40 : m_buffer(read_delay)
41{
42 this->init(&m_buffer);
43} // basic_socket_stream::basic_socket_stream()
44
45/*----------------------------------------------------------------------------*/
46/**
47 * \brief Constructor.
48 * \param address The address to which we will connect.
49 * \param port The port number to use for the connection.
50 * \param read_delay Number of second to wait before considering nothing will
51 * come in the socket. Negative values mean infinity.
52 */
53template<typename CharT, typename Traits>
54claw::net::basic_socket_stream<CharT, Traits>::basic_socket_stream
55( const char* address, int port, int read_delay )
56 : m_buffer(read_delay)
57{
58 this->init(&m_buffer);
59 open(address, port);
60} // basic_socket_stream::basic_socket_stream()
61
62/*----------------------------------------------------------------------------*/
63/**
64 * \brief Destructor.
65 */
66template<typename CharT, typename Traits>
67claw::net::basic_socket_stream<CharT, Traits>::~basic_socket_stream()
68{
69 // nothing to do
70} // basic_socket_stream::~basic_socket_stream()
71
72/*----------------------------------------------------------------------------*/
73/**
74 * \brief Get the buffer.
75 */
76template<typename CharT, typename Traits>
77typename claw::net::basic_socket_stream<CharT, Traits>::buffer_type*
78claw::net::basic_socket_stream<CharT, Traits>::rdbuf() const
79{
80 return const_cast<buffer_type*>(&m_buffer);
81} // basic_socket_stream::rdbuf()
82
83/*----------------------------------------------------------------------------*/
84/**
85 * \brief Tell if the stream is open.
86 */
87template<typename CharT, typename Traits>
88bool claw::net::basic_socket_stream<CharT, Traits>::is_open() const
89{
90 return m_buffer.is_open();
91} // basic_socket_stream::is_open()
92
93/*----------------------------------------------------------------------------*/
94/**
95 * \brief Set the number of second to wait before considering nothing will come
96 * in the socket.
97 * \param read_limit The number of seconds. Negative values mean infinity.
98 */
99template<typename CharT, typename Traits>
100void claw::net::basic_socket_stream<CharT, Traits>::set_read_time_limit
101( int read_limit )
102{
103 m_buffer.set_read_time_limit(read_limit);
104} // // basic_socket_stream::set_read_time_limit()
105
106/*----------------------------------------------------------------------------*/
107/**
108 * \brief Connect the socket to an address.
109 * \param address The address to which we will connect.
110 * \param port The port number to use for the connection.
111 */
112template<typename CharT, typename Traits>
113void claw::net::basic_socket_stream<CharT, Traits>::open
114( const char* address, int port )
115{
116 if ( !m_buffer.open(address, port) )
117 this->setstate(std::ios_base::failbit);
118 else
119 this->clear();
120} // basic_socket_stream::open()
121
122/*----------------------------------------------------------------------------*/
123/**
124 * \brief Link the socket to a file descriptor.
125 * \param fd The file descriptor.
126 * \remark This method should be only called by claw::net::socket_server.
127 */
128template<typename CharT, typename Traits>
129void claw::net::basic_socket_stream<CharT, Traits>::open( int fd )
130{
131 if ( !m_buffer.open(fd) )
132 this->setstate(std::ios_base::failbit);
133 else
134 this->clear();
135} // basic_socket_stream::open()
136
137/*----------------------------------------------------------------------------*/
138/**
139 * \brief Close the connection.
140 */
141template<typename CharT, typename Traits>
142void claw::net::basic_socket_stream<CharT, Traits>::close()
143{
144 if ( !m_buffer.close() )
145 this->setstate(std::ios_base::failbit);
146} // basic_socket_stream::close()
147