claw::net::basic_socketbuf< CharT, Traits > Class Template Reference

Socket buffer to be used with std::basic_stream, for easy socket reading and writing. More...

#include <basic_socketbuf.hpp>

Inheritance diagram for claw::net::basic_socketbuf< CharT, Traits >:
claw::net::basic_socket

List of all members.

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 ()
 Constructor.
virtual ~basic_socketbuf ()
 Destructor.
self_typeopen (const std::string &addr, int port)
 Initialise the socket.
self_typeopen (socket_traits::descriptor d)
 Link the socket to a file descriptor.
self_typeclose ()
 Close the socket.
bool is_open () const
 Tell if the socket is open.

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

char_typem_input_buffer
 Input buffer.
size_t m_input_buffer_size
 Size of the input buffer.
char_typem_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.

Detailed Description

template<typename CharT, typename Traits>
class claw::net::basic_socketbuf< CharT, Traits >

Socket buffer to be used with std::basic_stream, for easy socket reading and writing.

Author:
Julien Jorge

Definition at line 48 of file basic_socketbuf.hpp.


Member Typedef Documentation

template<typename CharT, typename Traits>
typedef CharT claw::net::basic_socketbuf< CharT, Traits >::char_type

Definition at line 52 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
typedef traits_type::int_type claw::net::basic_socketbuf< CharT, Traits >::int_type

Definition at line 54 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
typedef traits_type::off_type claw::net::basic_socketbuf< CharT, Traits >::off_type

Definition at line 56 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
typedef traits_type::pos_type claw::net::basic_socketbuf< CharT, Traits >::pos_type

Definition at line 55 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
typedef basic_socketbuf<char_type, traits_type> claw::net::basic_socketbuf< CharT, Traits >::self_type

Definition at line 58 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
typedef Traits claw::net::basic_socketbuf< CharT, Traits >::traits_type

Definition at line 53 of file basic_socketbuf.hpp.


Constructor & Destructor Documentation

template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::basic_socketbuf (  )  [inline]

Constructor.

Postcondition:
is_open() == false;

Definition at line 42 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::create_buffers().

00043   : m_input_buffer(NULL), m_input_buffer_size(0),
00044     m_output_buffer(NULL), m_output_buffer_size(0)
00045 {
00046   create_buffers();
00047 } // basic_socketbuf::basic_socketbuf()

template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::~basic_socketbuf (  )  [inline, virtual]

Destructor.

Definition at line 54 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::close(), and claw::net::basic_socketbuf< CharT, Traits >::destroy_buffers().

00055 {
00056   close();
00057   destroy_buffers();
00058 } // basic_socketbuf::basic_socketbuf()


Member Function Documentation

template<typename CharT , typename Traits >
bool claw::net::basic_socketbuf< CharT, Traits >::buffered (  )  const [inline, private]

Tell if we use buffered input and output.

Remarks:
Should always be true !

Definition at line 303 of file basic_socketbuf.tpp.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::overflow(), claw::net::basic_socketbuf< CharT, Traits >::sync(), and claw::net::basic_socketbuf< CharT, Traits >::underflow().

00304 {
00305   return this->pbase() && this->pptr() && this->epptr() 
00306     && this->eback() && this->gptr() && this->egptr();
00307 } // basic_socketbuf::buffered()

template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::close (  )  [inline]
template<typename CharT , typename Traits >
bool claw::net::basic_socketbuf< CharT, Traits >::connect ( const std::string &  addr,
int  port 
) [inline, private]

Connect the socket to a port.

Parameters:
addr The address to connect to.
port The port to connect to.
Returns:
true if the connection is established.
Precondition:
m_fd is a valid socket descriptor.

Definition at line 245 of file basic_socketbuf.tpp.

References CLAW_PRECOND, claw::socket_traits_unix::connect(), and claw::socket_traits_unix::valid_descriptor().

00246 {
00247   CLAW_PRECOND( socket_traits::valid_descriptor(m_descriptor) );
00248 
00249   return socket_traits::connect(m_descriptor, addr, port);
00250 } // basic_socketbuf::connect()

template<typename CharT , typename Traits >
void claw::net::basic_socketbuf< CharT, Traits >::create_buffers (  )  [inline, private]
template<typename CharT , typename Traits >
void claw::net::basic_socketbuf< CharT, Traits >::destroy_buffers (  )  [inline, private]

Destroy the buffers.

Postcondition:
pbase() == eback() == NULL

Definition at line 279 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer, and claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::~basic_socketbuf().

00280 {
00281   if ( m_input_buffer )
00282     {
00283       delete[] m_input_buffer;
00284       m_input_buffer = NULL;
00285     }
00286 
00287   if ( m_output_buffer )
00288     {
00289       delete[] m_output_buffer;
00290       m_output_buffer = NULL;
00291     }
00292 
00293   this->setg( NULL, NULL, NULL );
00294   this->setp( NULL, NULL );
00295 } // basic_socketbuf::destroy_buffers()

template<typename CharT , typename Traits >
bool claw::net::basic_socketbuf< CharT, Traits >::is_open (  )  const [inline]
template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::open ( socket_traits::descriptor  d  )  [inline]

Link the socket to a file descriptor.

Parameters:
d The file descriptor.
Returns:
this if everything works fine, NULL otherwise.
Remarks:
This method should be only called by claw::net::basic_*socket_stream.
If this socket was open and fd is valid, the socket will be closed before that d will be assigned.

Definition at line 97 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::close(), claw::net::basic_socketbuf< CharT, Traits >::is_open(), claw::socket_traits_unix::is_open(), and claw::net::basic_socket::m_descriptor.

00098 {
00099   self_type* result = NULL;
00100 
00101   if ( socket_traits::is_open(d) )
00102     {
00103       if (this->is_open())
00104         {
00105           if ( close() )
00106             {
00107               result = this;
00108               m_descriptor = d;
00109             }
00110         }
00111       else
00112         {
00113           result = this;
00114           m_descriptor = d;
00115         }
00116     }
00117 
00118   return result;
00119 } // basic_socketbuf::open()

template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::open ( const std::string &  address,
int  port 
) [inline]

Initialise the socket.

Parameters:
address Address to open.
port The port to connect to.
Returns:
this if everything works fine, NULL otherwise.

Definition at line 70 of file basic_socketbuf.tpp.

References claw::net::basic_socket::open().

Referenced by claw::net::basic_socket_stream< CharT, Traits >::open(), claw::net::basic_osocket_stream< CharT, Traits >::open(), and claw::net::basic_isocket_stream< CharT, Traits >::open().

00071 {
00072   self_type* result = NULL;
00073 
00074   if (!this->is_open())
00075     if ( basic_socket::open() )
00076       {
00077         if ( connect( address, port ) )
00078           result = this;
00079         else
00080     close();
00081       }
00082 
00083   return result;
00084 } // basic_socketbuf::open()

template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::int_type claw::net::basic_socketbuf< CharT, Traits >::overflow ( int_type  c = traits_type::eof()  )  [inline, protected, virtual]

Synchronize the output buffer (ie. write in the socket).

Parameters:
c 

Definition at line 217 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::buffered(), CLAW_PRECOND, claw::net::basic_socketbuf< CharT, Traits >::is_open(), and claw::net::basic_socketbuf< CharT, Traits >::sync().

00218 {
00219   CLAW_PRECOND( is_open() );
00220   CLAW_PRECOND( buffered() );
00221 
00222   int_type result = traits_type::eof();
00223 
00224   if ( sync() == 0 )
00225     {
00226       result = traits_type::not_eof(c);
00227 
00228       if ( !traits_type::eq_int_type(c, traits_type::eof()) )
00229         sputc(c);
00230     }
00231 
00232   return result;
00233 } // basic_socketbuf::overflow()

template<typename CharT , typename Traits >
int claw::net::basic_socketbuf< CharT, Traits >::sync (  )  [inline, protected, virtual]

Write the buffered data in the socket.

Precondition:
is_open()

Definition at line 152 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::buffered(), CLAW_PRECOND, claw::net::basic_socketbuf< CharT, Traits >::is_open(), claw::net::basic_socket::m_descriptor, claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer, and claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer_size.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::overflow().

00153 {
00154   CLAW_PRECOND( is_open() );
00155   CLAW_PRECOND( buffered() );
00156 
00157   ssize_t write_count = 0;
00158   ssize_t length = (this->pptr() - this->pbase()) * sizeof(char_type);
00159   int_type result = 0;
00160 
00161   if ( length > 0 )
00162     write_count = send(m_descriptor, static_cast<const char*>(this->pbase()),
00163            length, 0 );
00164 
00165   if ( write_count >= 0 )
00166     setp( m_output_buffer, m_output_buffer + m_output_buffer_size );
00167   else
00168     result = -1;
00169 
00170   return result;
00171 } // basic_socketbuf::sync()

template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::int_type claw::net::basic_socketbuf< CharT, Traits >::underflow (  )  [inline, protected, virtual]

Fill the input buffer.

Precondition:
is_open() && gptr() != NULL
Returns:
The next symbol to be read ( *gptr() ) if there is data coming from the socket, traits::eof() otherwise.

Definition at line 182 of file basic_socketbuf.tpp.

References claw::net::basic_socketbuf< CharT, Traits >::buffered(), CLAW_PRECOND, claw::net::basic_socketbuf< CharT, Traits >::is_open(), claw::net::basic_socket::m_descriptor, claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer, claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer_size, and claw::socket_traits_unix::select_read().

00183 {
00184   CLAW_PRECOND( is_open() );
00185   CLAW_PRECOND( buffered() );
00186   CLAW_PRECOND( this->gptr() >= this->egptr() );
00187 
00188   ssize_t read_count;
00189   ssize_t length = m_input_buffer_size * sizeof(char_type);
00190   int_type result = traits_type::eof();
00191 
00192   if ( socket_traits::select_read(m_descriptor) )
00193     read_count = recv(m_descriptor, static_cast<char*>(m_input_buffer), length,
00194           0);
00195   else
00196     read_count = -1;
00197 
00198   if ( read_count > 0 )
00199     {
00200       setg( m_input_buffer, m_input_buffer, m_input_buffer + read_count);
00201       result = this->sgetc();
00202     }
00203   else
00204     setg( m_input_buffer, m_input_buffer + m_input_buffer_size,
00205           m_input_buffer + m_input_buffer_size );
00206 
00207   return result;
00208 } // basic_socketbuf::underflow()


Member Data Documentation

template<typename CharT, typename Traits>
char_type* claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer [private]
template<typename CharT, typename Traits>
size_t claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer_size [private]
template<typename CharT, typename Traits>
char_type* claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer [private]
template<typename CharT, typename Traits>
size_t claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer_size [private]
template<typename CharT, typename Traits>
const size_t claw::net::basic_socketbuf< CharT, Traits >::s_buffer_size = 256 [inline, static, private]

Default size for the buffers.

Definition at line 97 of file basic_socketbuf.hpp.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::create_buffers().


The documentation for this class was generated from the following files:

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1