Socket buffer to be used with std::basic_stream, for easy socket reading and writing. More...
#include <basic_socketbuf.hpp>
Public Types | |
typedef CharT | char_type |
typedef Traits | traits_type |
typedef traits_type::int_type | int_type |
typedef traits_type::pos_type | pos_type |
typedef traits_type::off_type | off_type |
typedef basic_socketbuf < char_type, traits_type > | self_type |
Public Member Functions | |
basic_socketbuf (int read_limit=-1) | |
Constructor. | |
virtual | ~basic_socketbuf () |
Destructor. | |
self_type * | open (const std::string &addr, int port) |
Initialise the socket. | |
self_type * | open (socket_traits::descriptor d) |
Link the socket to a file descriptor. | |
self_type * | close () |
Close the socket. | |
bool | is_open () const |
Tell if the socket is open. | |
void | set_read_time_limit (int read_limit) |
Set the number of second to wait before considering nothing will come in the socket. | |
Protected Member Functions | |
virtual int | sync () |
Write the buffered data in the socket. | |
virtual int_type | underflow () |
Fill the input buffer. | |
virtual int_type | overflow (int_type c=traits_type::eof()) |
Synchronize the output buffer (ie. write in the socket). | |
Private Member Functions | |
bool | connect (const std::string &addr, int port) |
Connect the socket to a port. | |
void | create_buffers () |
Create the buffers. | |
void | destroy_buffers () |
Destroy the buffers. | |
bool | buffered () const |
Tell if we use buffered input and output. | |
Private Attributes | |
int | m_read_limit |
Number of second to wait before considering nothing will come in the socket. Negative values mean infinity. | |
char_type * | m_input_buffer |
Input buffer. | |
size_t | m_input_buffer_size |
Size of the input buffer. | |
char_type * | m_output_buffer |
Output buffer. | |
size_t | m_output_buffer_size |
Size of the output buffer. | |
Static Private Attributes | |
static const size_t | s_buffer_size = 256 |
Default size for the buffers. |
Socket buffer to be used with std::basic_stream, for easy socket reading and writing.
Definition at line 48 of file basic_socketbuf.hpp.
typedef CharT claw::net::basic_socketbuf< CharT, Traits >::char_type |
Definition at line 52 of file basic_socketbuf.hpp.
typedef traits_type::int_type claw::net::basic_socketbuf< CharT, Traits >::int_type |
Definition at line 54 of file basic_socketbuf.hpp.
typedef traits_type::off_type claw::net::basic_socketbuf< CharT, Traits >::off_type |
Definition at line 56 of file basic_socketbuf.hpp.
typedef traits_type::pos_type claw::net::basic_socketbuf< CharT, Traits >::pos_type |
Definition at line 55 of file basic_socketbuf.hpp.
typedef basic_socketbuf<char_type, traits_type> claw::net::basic_socketbuf< CharT, Traits >::self_type |
Definition at line 58 of file basic_socketbuf.hpp.
typedef Traits claw::net::basic_socketbuf< CharT, Traits >::traits_type |
Definition at line 53 of file basic_socketbuf.hpp.
claw::net::basic_socketbuf< CharT, Traits >::basic_socketbuf | ( | int | read_limit = -1 ) |
Constructor.
read_limit | Number of second to wait before considering nothing will come in the socket. Negative values mean infinity. |
Definition at line 44 of file basic_socketbuf.tpp.
References claw::net::basic_socketbuf< CharT, Traits >::create_buffers().
: 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()
claw::net::basic_socketbuf< CharT, Traits >::~basic_socketbuf | ( | ) | [virtual] |
Destructor.
Definition at line 56 of file basic_socketbuf.tpp.
{ close(); destroy_buffers(); } // basic_socketbuf::basic_socketbuf()
bool claw::net::basic_socketbuf< CharT, Traits >::buffered | ( | ) | const [private] |
Tell if we use buffered input and output.
Definition at line 320 of file basic_socketbuf.tpp.
{ return this->pbase() && this->pptr() && this->epptr() && this->eback() && this->gptr() && this->egptr(); } // basic_socketbuf::buffered()
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::close | ( | ) |
Close the socket.
Reimplemented from claw::net::basic_socket.
Definition at line 130 of file basic_socketbuf.tpp.
References claw::net::basic_socket::close().
{ if ( basic_socket::close() ) return this; else return NULL; } // basic_socketbuf::close()
bool claw::net::basic_socketbuf< CharT, Traits >::connect | ( | const std::string & | addr, |
int | port | ||
) | [private] |
Connect the socket to a port.
addr | The address to connect to. |
port | The port to connect to. |
Definition at line 262 of file basic_socketbuf.tpp.
References CLAW_PRECOND, claw::socket_traits_unix::connect(), and claw::socket_traits_unix::valid_descriptor().
{ CLAW_PRECOND( socket_traits::valid_descriptor(m_descriptor) ); return socket_traits::connect(m_descriptor, addr, port); } // basic_socketbuf::connect()
void claw::net::basic_socketbuf< CharT, Traits >::create_buffers | ( | ) | [private] |
Create the buffers.
Definition at line 275 of file basic_socketbuf.tpp.
References CLAW_PRECOND.
Referenced by claw::net::basic_socketbuf< CharT, Traits >::basic_socketbuf().
{ 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()
void claw::net::basic_socketbuf< CharT, Traits >::destroy_buffers | ( | ) | [private] |
Destroy the buffers.
Definition at line 296 of file basic_socketbuf.tpp.
{ 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()
bool claw::net::basic_socketbuf< CharT, Traits >::is_open | ( | ) | const |
Tell if the socket is open.
Reimplemented from claw::net::basic_socket.
Definition at line 143 of file basic_socketbuf.tpp.
References claw::net::basic_socket::is_open().
{ return basic_socket::is_open(); } // // basic_socketbuf::is_open()
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::open | ( | socket_traits::descriptor | d ) |
Link the socket to a file descriptor.
d | The file descriptor. |
Definition at line 99 of file basic_socketbuf.tpp.
References claw::socket_traits_unix::is_open().
{ 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()
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::open | ( | const std::string & | address, |
int | port | ||
) |
Initialise the socket.
address | Address to open. |
port | The port to connect to. |
Definition at line 72 of file basic_socketbuf.tpp.
References claw::net::basic_socket::open().
{ 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()
claw::net::basic_socketbuf< CharT, Traits >::int_type claw::net::basic_socketbuf< CharT, Traits >::overflow | ( | int_type | c = traits_type::eof() ) |
[protected, virtual] |
Synchronize the output buffer (ie. write in the socket).
c |
Definition at line 234 of file basic_socketbuf.tpp.
References CLAW_PRECOND.
{ 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()) ) sputc(c); } return result; } // basic_socketbuf::overflow()
void claw::net::basic_socketbuf< CharT, Traits >::set_read_time_limit | ( | int | read_limit ) |
Set the number of second to wait before considering nothing will come in the socket.
read_limit | The number of seconds. Negative values mean infinity. |
Definition at line 156 of file basic_socketbuf.tpp.
{ m_read_limit = read_limit; } // // basic_socketbuf::set_read_time_limit()
int claw::net::basic_socketbuf< CharT, Traits >::sync | ( | ) | [protected, virtual] |
Write the buffered data in the socket.
Definition at line 167 of file basic_socketbuf.tpp.
References CLAW_PRECOND.
{ 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 ) setp( m_output_buffer, m_output_buffer + m_output_buffer_size ); else result = -1; return result; } // basic_socketbuf::sync()
claw::net::basic_socketbuf< CharT, Traits >::int_type claw::net::basic_socketbuf< CharT, Traits >::underflow | ( | ) | [protected, virtual] |
Fill the input buffer.
Definition at line 197 of file basic_socketbuf.tpp.
References CLAW_PRECOND, and claw::socket_traits_unix::select_read().
{ 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 ) { setg( m_input_buffer, m_input_buffer, m_input_buffer + read_count); result = this->sgetc(); } else setg( m_input_buffer, m_input_buffer + m_input_buffer_size, m_input_buffer + m_input_buffer_size ); return result; } // basic_socketbuf::underflow()
char_type* claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer [private] |
Input buffer.
Definition at line 91 of file basic_socketbuf.hpp.
size_t claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer_size [private] |
Size of the input buffer.
Definition at line 94 of file basic_socketbuf.hpp.
char_type* claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer [private] |
Output buffer.
Definition at line 97 of file basic_socketbuf.hpp.
size_t claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer_size [private] |
Size of the output buffer.
Definition at line 100 of file basic_socketbuf.hpp.
int claw::net::basic_socketbuf< CharT, Traits >::m_read_limit [private] |
Number of second to wait before considering nothing will come in the socket. Negative values mean infinity.
Definition at line 88 of file basic_socketbuf.hpp.
const size_t claw::net::basic_socketbuf< CharT, Traits >::s_buffer_size = 256 [static, private] |
Default size for the buffers.
Definition at line 103 of file basic_socketbuf.hpp.