Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

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 basic_streambuf

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 (int read_limit=-1)
 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.
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_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 ( int  read_limit = -1 )

Constructor.

Parameters:
read_limitNumber of second to wait before considering nothing will come in the socket. Negative values mean infinity.
Postcondition:
is_open() == false;

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()
template<typename CharT , typename Traits >
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()

Member Function Documentation

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

Tell if we use buffered input and output.

Remarks:
Should always be true !

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()
template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::self_type * claw::net::basic_socketbuf< CharT, Traits >::close (  )

Close the socket.

Postcondition:
is_open() == false;

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()
template<typename CharT , typename Traits >
bool claw::net::basic_socketbuf< CharT, Traits >::connect ( const std::string &  addr,
int  port 
) [private]

Connect the socket to a port.

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

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()
template<typename CharT , typename Traits >
void claw::net::basic_socketbuf< CharT, Traits >::create_buffers (  ) [private]

Create the buffers.

Precondition:
pbase() == eback() == NULL

Definition at line 275 of file basic_socketbuf.tpp.

References CLAW_PRECOND.

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

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

Destroy the buffers.

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

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()
template<typename CharT , typename Traits >
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()
template<typename CharT , typename Traits >
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.

Parameters:
dThe 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 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()
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 
)

Initialise the socket.

Parameters:
addressAddress to open.
portThe port to connect to.
Returns:
this if everything works fine, NULL otherwise.

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()
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() ) [protected, virtual]

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

Parameters:
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()
template<typename CharT , typename Traits >
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.

Parameters:
read_limitThe 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()
template<typename CharT , typename Traits >
int claw::net::basic_socketbuf< CharT, Traits >::sync (  ) [protected, virtual]

Write the buffered data in the socket.

Precondition:
is_open()

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()
template<typename CharT , typename Traits >
claw::net::basic_socketbuf< CharT, Traits >::int_type claw::net::basic_socketbuf< CharT, Traits >::underflow (  ) [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 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()

Member Data Documentation

template<typename CharT, typename Traits>
char_type* claw::net::basic_socketbuf< CharT, Traits >::m_input_buffer [private]

Input buffer.

Definition at line 91 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
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.

template<typename CharT, typename Traits>
char_type* claw::net::basic_socketbuf< CharT, Traits >::m_output_buffer [private]

Output buffer.

Definition at line 97 of file basic_socketbuf.hpp.

template<typename CharT, typename Traits>
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.

template<typename CharT, typename Traits>
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.

template<typename CharT, typename Traits>
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.


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