2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
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.
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.
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
23 contact: julien.jorge@gamned.org
26 * \file socket_stream.tpp
27 * \brief Implementation of the claw::net::basic_socket_stream class.
28 * \author Julien Jorge
31/*----------------------------------------------------------------------------*/
34 * \param read_delay Number of second to wait before considering nothing will
35 * come in the socket. Negative values mean infinity.
37template<typename CharT, typename Traits>
38claw::net::basic_socket_stream<CharT, Traits>::basic_socket_stream
40 : m_buffer(read_delay)
42 this->init(&m_buffer);
43} // basic_socket_stream::basic_socket_stream()
45/*----------------------------------------------------------------------------*/
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.
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)
58 this->init(&m_buffer);
60} // basic_socket_stream::basic_socket_stream()
62/*----------------------------------------------------------------------------*/
66template<typename CharT, typename Traits>
67claw::net::basic_socket_stream<CharT, Traits>::~basic_socket_stream()
70} // basic_socket_stream::~basic_socket_stream()
72/*----------------------------------------------------------------------------*/
74 * \brief Get the buffer.
76template<typename CharT, typename Traits>
77typename claw::net::basic_socket_stream<CharT, Traits>::buffer_type*
78claw::net::basic_socket_stream<CharT, Traits>::rdbuf() const
80 return const_cast<buffer_type*>(&m_buffer);
81} // basic_socket_stream::rdbuf()
83/*----------------------------------------------------------------------------*/
85 * \brief Tell if the stream is open.
87template<typename CharT, typename Traits>
88bool claw::net::basic_socket_stream<CharT, Traits>::is_open() const
90 return m_buffer.is_open();
91} // basic_socket_stream::is_open()
93/*----------------------------------------------------------------------------*/
95 * \brief Set the number of second to wait before considering nothing will come
97 * \param read_limit The number of seconds. Negative values mean infinity.
99template<typename CharT, typename Traits>
100void claw::net::basic_socket_stream<CharT, Traits>::set_read_time_limit
103 m_buffer.set_read_time_limit(read_limit);
104} // // basic_socket_stream::set_read_time_limit()
106/*----------------------------------------------------------------------------*/
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.
112template<typename CharT, typename Traits>
113void claw::net::basic_socket_stream<CharT, Traits>::open
114( const char* address, int port )
116 if ( !m_buffer.open(address, port) )
117 this->setstate(std::ios_base::failbit);
120} // basic_socket_stream::open()
122/*----------------------------------------------------------------------------*/
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.
128template<typename CharT, typename Traits>
129void claw::net::basic_socket_stream<CharT, Traits>::open( int fd )
131 if ( !m_buffer.open(fd) )
132 this->setstate(std::ios_base::failbit);
135} // basic_socket_stream::open()
137/*----------------------------------------------------------------------------*/
139 * \brief Close the connection.
141template<typename CharT, typename Traits>
142void claw::net::basic_socket_stream<CharT, Traits>::close()
144 if ( !m_buffer.close() )
145 this->setstate(std::ios_base::failbit);
146} // basic_socket_stream::close()