/usr/include/claw/impl/basic_socketbuf.tpp is in libclaw-net-dev 1.7.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | /*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
/**
* \file basic_socketbuf.tpp
* \brief Implantation of the claw::net::basic_socketbuf class.
* \author Julien Jorge
*/
#include <claw/assert.hpp>
/*----------------------------------------------------------------------------*/
template<typename CharT, typename Traits>
const size_t claw::net::basic_socketbuf<CharT, Traits>::s_buffer_size = 256;
/*----------------------------------------------------------------------------*/
/**
* \brief Constructor.
* \param read_limit Number of second to wait before considering nothing will
* come in the socket. Negative values mean infinity.
* \post is_open() == false;
*/
template<typename CharT, typename Traits>
claw::net::basic_socketbuf<CharT, Traits>::basic_socketbuf( int read_limit )
: m_read_limit(read_limit), m_input_buffer(NULL), m_input_buffer_size(0),
m_output_buffer(NULL), m_output_buffer_size(0)
{
create_buffers();
} // basic_socketbuf::basic_socketbuf()
/*----------------------------------------------------------------------------*/
/**
* \brief Destructor.
*/
template<typename CharT, typename Traits>
claw::net::basic_socketbuf<CharT, Traits>::~basic_socketbuf()
{
close();
destroy_buffers();
} // basic_socketbuf::basic_socketbuf()
/*----------------------------------------------------------------------------*/
/**
* \brief Initialise the socket.
* \param address Address to open.
* \param port The port to connect to.
* \return this if everything works fine, NULL otherwise.
*/
template<typename CharT, typename Traits>
typename claw::net::basic_socketbuf<CharT, Traits>::self_type*
claw::net::basic_socketbuf<CharT, Traits>::open
( const std::string& address, int port )
{
self_type* result = NULL;
if (!this->is_open())
if ( basic_socket::open() )
{
if ( connect( address, port ) )
result = this;
else
close();
}
return result;
} // basic_socketbuf::open()
/*----------------------------------------------------------------------------*/
/**
* \brief Link the socket to a file descriptor.
* \param d The file descriptor.
* \return this if everything works fine, NULL otherwise.
* \remark This method should be only called by claw::net::basic_*socket_stream.
* \remark If this socket was open and \a fd is valid, the socket will be closed
* before that \a d will be assigned.
*/
template<typename CharT, typename Traits>
typename claw::net::basic_socketbuf<CharT, Traits>::self_type*
claw::net::basic_socketbuf<CharT, Traits>::open( socket_traits::descriptor d )
{
self_type* result = NULL;
if ( socket_traits::is_open(d) )
{
if (this->is_open())
{
if ( close() )
{
result = this;
m_descriptor = d;
}
}
else
{
result = this;
m_descriptor = d;
}
}
return result;
} // basic_socketbuf::open()
/*----------------------------------------------------------------------------*/
/**
* \brief Close the socket.
* \post is_open() == false;
*/
template<typename CharT, typename Traits>
typename claw::net::basic_socketbuf<CharT, Traits>::self_type*
claw::net::basic_socketbuf<CharT, Traits>::close()
{
if ( basic_socket::close() )
return this;
else
return NULL;
} // basic_socketbuf::close()
/*----------------------------------------------------------------------------*/
/**
* \brief Tell if the socket is open.
*/
template<typename CharT, typename Traits>
bool claw::net::basic_socketbuf<CharT, Traits>::is_open() const
{
return basic_socket::is_open();
} // // basic_socketbuf::is_open()
/*----------------------------------------------------------------------------*/
/**
* \brief Set the number of second to wait before considering nothing will come
* in the socket.
* \param read_limit The number of seconds. Negative values mean infinity.
*/
template<typename CharT, typename Traits>
void claw::net::basic_socketbuf<CharT, Traits>::set_read_time_limit
( int read_limit )
{
m_read_limit = read_limit;
} // // basic_socketbuf::set_read_time_limit()
/*----------------------------------------------------------------------------*/
/**
* \brief Write the buffered data in the socket.
* \pre is_open()
*/
template<typename CharT, typename Traits>
int claw::net::basic_socketbuf<CharT, Traits>::sync()
{
CLAW_PRECOND( is_open() );
CLAW_PRECOND( buffered() );
ssize_t write_count = 0;
ssize_t length = (this->pptr() - this->pbase()) * sizeof(char_type);
int_type result = 0;
if ( length > 0 )
write_count = send(m_descriptor, static_cast<const char*>(this->pbase()),
length, 0 );
if ( write_count >= 0 )
this->setp( m_output_buffer, m_output_buffer + m_output_buffer_size );
else
result = -1;
return result;
} // basic_socketbuf::sync()
/*----------------------------------------------------------------------------*/
/**
* \brief Fill the input buffer.
* \pre is_open() && gptr() != NULL
* \return The next symbol to be read ( *gptr() ) if there is data coming from
* the socket, traits::eof() otherwise.
*/
template<typename CharT, typename Traits>
typename claw::net::basic_socketbuf<CharT, Traits>::int_type
claw::net::basic_socketbuf<CharT, Traits>::underflow()
{
CLAW_PRECOND( buffered() );
CLAW_PRECOND( this->gptr() >= this->egptr() );
ssize_t read_count;
ssize_t length = m_input_buffer_size * sizeof(char_type);
int_type result = traits_type::eof();
if( !is_open() )
return result;
if ( socket_traits::select_read(m_descriptor, m_read_limit) )
read_count = recv(m_descriptor, static_cast<char*>(m_input_buffer), length,
0);
else
read_count = -1;
if ( read_count > 0 )
{
this->setg( m_input_buffer, m_input_buffer, m_input_buffer + read_count);
result = this->sgetc();
}
else
this->setg( m_input_buffer, m_input_buffer + m_input_buffer_size,
m_input_buffer + m_input_buffer_size );
return result;
} // basic_socketbuf::underflow()
/*----------------------------------------------------------------------------*/
/**
* \brief Synchronize the output buffer (ie. write in the socket).
* \param c
*/
template<typename CharT, typename Traits>
typename claw::net::basic_socketbuf<CharT, Traits>::int_type
claw::net::basic_socketbuf<CharT, Traits>::overflow( int_type c )
{
CLAW_PRECOND( is_open() );
CLAW_PRECOND( buffered() );
int_type result = traits_type::eof();
if ( sync() == 0 )
{
result = traits_type::not_eof(c);
if ( !traits_type::eq_int_type(c, traits_type::eof()) )
this->sputc(c);
}
return result;
} // basic_socketbuf::overflow()
/*----------------------------------------------------------------------------*/
/**
* \brief Connect the socket to a port.
* \param addr The address to connect to.
* \param port The port to connect to.
* \return true if the connection is established.
* \pre m_fd is a valid socket descriptor.
*/
template<typename CharT, typename Traits>
bool claw::net::basic_socketbuf<CharT, Traits>::connect
( const std::string& addr, int port )
{
CLAW_PRECOND( socket_traits::valid_descriptor(m_descriptor) );
return socket_traits::connect(m_descriptor, addr, port);
} // basic_socketbuf::connect()
/*----------------------------------------------------------------------------*/
/**
* \brief Create the buffers.
* \pre pbase() == eback() == NULL
*/
template<typename CharT, typename Traits>
void claw::net::basic_socketbuf<CharT, Traits>::create_buffers()
{
CLAW_PRECOND( this->pbase() == NULL );
CLAW_PRECOND( this->eback() == NULL );
m_input_buffer_size = m_output_buffer_size = s_buffer_size;
m_input_buffer = new char_type[m_input_buffer_size];
m_output_buffer = new char_type[m_output_buffer_size];
this->setg( m_input_buffer, m_input_buffer + m_input_buffer_size,
m_input_buffer + m_input_buffer_size );
this->setp( m_output_buffer, m_output_buffer + m_output_buffer_size );
} // basic_socketbuf::create_buffers()
/*----------------------------------------------------------------------------*/
/**
* \brief Destroy the buffers.
* \post pbase() == eback() == NULL
*/
template<typename CharT, typename Traits>
void claw::net::basic_socketbuf<CharT, Traits>::destroy_buffers()
{
if ( m_input_buffer )
{
delete[] m_input_buffer;
m_input_buffer = NULL;
}
if ( m_output_buffer )
{
delete[] m_output_buffer;
m_output_buffer = NULL;
}
this->setg( NULL, NULL, NULL );
this->setp( NULL, NULL );
} // basic_socketbuf::destroy_buffers()
/*----------------------------------------------------------------------------*/
/**
* \brief Tell if we use buffered input and output.
* \remark Should always be true !
*/
template<typename CharT, typename Traits>
bool claw::net::basic_socketbuf<CharT, Traits>::buffered() const
{
return this->pbase() && this->pptr() && this->epptr()
&& this->eback() && this->gptr() && this->egptr();
} // basic_socketbuf::buffered()
|