Claw  1.7.3
isocket_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 isocket_stream.tpp
27  * \brief Implementation of the claw::net::basic_isocket_stream class.
28  * \author Julien Jorge
29  */
30 
31 /*----------------------------------------------------------------------------*/
32 /**
33  * \brief Constructor.
34  */
35 template<typename CharT, typename Traits>
36 claw::net::basic_isocket_stream<CharT, Traits>::basic_isocket_stream()
37  : super(&m_buffer)
38 {
39 
40 } // basic_isocket_stream::basic_isocket_stream()
41 
42 /*----------------------------------------------------------------------------*/
43 /**
44  * \brief Constructor.
45  * \param address The address to which we will connect.
46  * \param port The port number to use for the connection.
47  */
48 template<typename CharT, typename Traits>
49 claw::net::basic_isocket_stream<CharT, Traits>::basic_isocket_stream
50 ( const std::string& address, int port )
51  : super(&m_buffer)
52 {
53  open(address, port);
54 } // basic_isocket_stream::basic_isocket_stream()
55 
56 /*----------------------------------------------------------------------------*/
57 /**
58  * \brief Destructor.
59  */
60 template<typename CharT, typename Traits>
61 claw::net::basic_isocket_stream<CharT, Traits>::~basic_isocket_stream()
62 {
63  // nothing to do
64 } // basic_isocket_stream::~basic_isocket_stream()
65 
66 /*----------------------------------------------------------------------------*/
67 /**
68  * \brief Get the input buffer.
69  */
70 template<typename CharT, typename Traits>
71 typename claw::net::basic_isocket_stream<CharT, Traits>::buffer_type*
72 claw::net::basic_isocket_stream<CharT, Traits>::rdbuf() const
73 {
74  return const_cast<buffer_type*>(&m_buffer);
75 } // basic_isocket_stream::rdbuf()
76 
77 /*----------------------------------------------------------------------------*/
78 /**
79  * \brief Tell if the stream is open.
80  */
81 template<typename CharT, typename Traits>
82 bool claw::net::basic_isocket_stream<CharT, Traits>::is_open() const
83 {
84  return m_buffer.is_open();
85 } // basic_isocket_stream::()
86 
87 /*----------------------------------------------------------------------------*/
88 /**
89  * \brief Connect the socket to an address.
90  * \param address The address to which we will connect.
91  * \param port The port number to use for the connection.
92  */
93 template<typename CharT, typename Traits>
94 void claw::net::basic_isocket_stream<CharT, Traits>::open
95 ( const std::string& address, int port )
96 {
97  if ( !m_buffer.open(address, port) )
98  this->setstate(std::ios_base::failbit);
99  else
100  this->clear();
101 } // basic_isocket_stream::open()
102 
103 /*----------------------------------------------------------------------------*/
104 /**
105  * \brief Link the socket to a file descriptor.
106  * \param fd The file descriptor.
107  * \remark This method should be only called by claw::net::socket_server.
108  */
109 template<typename CharT, typename Traits>
110 void claw::net::basic_isocket_stream<CharT, Traits>::open( int fd )
111 {
112  if ( !m_buffer.open(fd) )
113  this->setstate(std::ios_base::failbit);
114  else
115  this->clear();
116 } // basic_isocket_stream::open()
117 
118 /*----------------------------------------------------------------------------*/
119 /**
120  * \brief Close the connection.
121  */
122 template<typename CharT, typename Traits>
123 void claw::net::basic_isocket_stream<CharT, Traits>::close()
124 {
125  if ( !m_buffer.close() )
126  this->setstate(std::ios_base::failbit);
127 } // basic_isocket_stream::close()
128