Intra2net AG

libt2n::socket_handler Class Reference

handles socket based communication. Don't use directly, use socket_server or socket_client_connection instead. More...

#include <socket_handler.hxx>

Inheritance diagram for libt2n::socket_handler:
Inheritance graph
[legend]

List of all members.

Public Member Functions

socket_type_value get_type ()
 is this a tcp or unix socket connection
int get_socket ()
bool is_closed ()
 is the underlying socket connection still open?
void set_recv_buffer_size (unsigned int new_recv_buffer_size)
 set a new size for the receive buffer.
void set_write_block_size (unsigned int new_write_block_size)
 set new size for the data chunks when writeing.
void set_write_timeout (long long new_write_timeout)
 set new timeout for writing a block
unsigned int get_recv_buffer_size () const
unsigned int get_write_block_size () const
long long get_write_timeout () const

Protected Member Functions

 socket_handler (int _sock, socket_type_value _socket_type)
 ~socket_handler ()
void set_socket_options (int sock)
 set options like fast reuse and keepalive every socket should have
virtual std::ostream * get_logstream (log_level_values level)
void socket_write (const std::string &data)
virtual void close ()
bool fill_buffer (std::string &buffer, long long usec_timeout, long long *usec_timeout_remaining=NULL)
 read data from the raw socket and copy it into the provided buffer
bool fill_buffer (std::string &buffer)
 read data from the raw socket and copy it into the provided buffer. Returns instantly if no data is waiting.

Protected Attributes

int sock
unsigned int recv_buffer_size
unsigned int write_block_size
long long write_timeout

Private Member Functions

bool data_waiting (long long usec_timeout, long long *usec_timeout_remaining=NULL)
 check if new data is waiting on the raw socket
void wait_ready_to_write (int socket, long long write_block_timeout)
 wait until the socket is ready to write again

Private Attributes

socket_type_value socket_type

Static Private Attributes

static const unsigned int default_recv_buffer_size = 2048
static const unsigned int default_write_block_size = 4096
static const long long default_write_timeout = 30000000

Detailed Description

handles socket based communication. Don't use directly, use socket_server or socket_client_connection instead.


Constructor & Destructor Documentation

libt2n::socket_handler::socket_handler ( int  _sock,
socket_type_value  _socket_type 
) [protected]
libt2n::socket_handler::~socket_handler (  )  [protected]

Destructor. Closes open socket

References close(), and sock.


Member Function Documentation

void libt2n::socket_handler::close (  )  [protected, virtual]

close the underlying socket connection. Don't call directly, use the version provided by the connection class you are using.

Reimplemented in libt2n::socket_client_connection, and libt2n::socket_server_connection.

References libt2n::debug, LOGSTREAM, and sock.

Referenced by fill_buffer(), and ~socket_handler().

bool libt2n::socket_handler::data_waiting ( long long  usec_timeout,
long long *  usec_timeout_remaining = NULL 
) [private]

check if new data is waiting on the raw socket

Parameters:
[in,out] usec_timeout wait until new data is found, max timeout usecs. -1: wait endless 0: return instantly
[out] usec_timeout_remaining microseconds from the timeout that were not used

References sock.

Referenced by fill_buffer().

bool libt2n::socket_handler::fill_buffer ( std::string &  buffer  )  [protected]

read data from the raw socket and copy it into the provided buffer. Returns instantly if no data is waiting.

Parameters:
buffer the buffer where to append the new data

References close(), data_waiting(), libt2n::debug, libt2n::error, EXCEPTIONSTREAM, fill_buffer(), LOGSTREAM, recv_buffer_size, and sock.

bool libt2n::socket_handler::fill_buffer ( std::string &  buffer,
long long  usec_timeout,
long long *  usec_timeout_remaining = NULL 
) [protected]

read data from the raw socket and copy it into the provided buffer

Parameters:
buffer the buffer where to append the new data
[in,out] usec_timeout wait until new data is found, max timeout usecs. -1: wait endless 0: return instantly
[out] usec_timeout_remaining microseconds from the timeout that were not used

References data_waiting().

Referenced by fill_buffer().

virtual std::ostream* libt2n::socket_handler::get_logstream ( log_level_values  level  )  [inline, protected, virtual]
unsigned int libt2n::socket_handler::get_recv_buffer_size (  )  const [inline]

References recv_buffer_size.

int libt2n::socket_handler::get_socket (  )  [inline]

References sock.

socket_type_value libt2n::socket_handler::get_type (  )  [inline]
unsigned int libt2n::socket_handler::get_write_block_size (  )  const [inline]

References write_block_size.

long long libt2n::socket_handler::get_write_timeout (  )  const [inline]

References write_timeout.

bool libt2n::socket_handler::is_closed (  ) 

is the underlying socket connection still open?

References sock.

void libt2n::socket_handler::set_recv_buffer_size ( unsigned int  new_recv_buffer_size  ) 

set a new size for the receive buffer.

Parameters:
new_recv_buffer_size the new size for the receive buffer.

The receive buffer determines the amount of data which is tried to read at once from the underlying socket.

The value is normalized to be at least 512 bytes and at max 32K bytes.

References recv_buffer_size.

void libt2n::socket_handler::set_socket_options ( int  sock  )  [protected]

set options like fast reuse and keepalive every socket should have

References libt2n::error, and EXCEPTIONSTREAM.

Referenced by libt2n::socket_client_connection::connect_with_timeout(), libt2n::socket_server::new_connection(), and libt2n::socket_server::socket_server().

void libt2n::socket_handler::set_write_block_size ( unsigned int  new_write_block_size  ) 

set new size for the data chunks when writeing.

Parameters:
new_write_block_size the new chunk size.

The write block size determines the amount of data which is tried to write to the socket when data needs to be sended. Since writing data is done in a loop, this does not limit the amunt of data which can be written.

The value is normalized to be at least 512 bytes and at max 32K bytes.

References write_block_size.

void libt2n::socket_handler::set_write_timeout ( long long  new_write_timeout  ) 

set new timeout for writing a block

Parameters:
new_write_timeout the new timeout in usecs, -1: wait endless

The write timeout determines the maximum amount of time that is waited between writing each block. If the timeout is exceeded, write will throw t2n_transfer_error

References write_block_size.

void libt2n::socket_handler::socket_write ( const std::string &  data  )  [protected]

writes raw data to the socket. Don't use directly, use the write() function provided by the connection because it encapsulates the data.

References libt2n::debug, libt2n::error, EXCEPTIONSTREAM, LOGSTREAM, sock, wait_ready_to_write(), write_block_size, and write_timeout.

Referenced by libt2n::socket_server_connection::real_write(), and libt2n::socket_client_connection::real_write().

void libt2n::socket_handler::wait_ready_to_write ( int  socket,
long long  write_block_timeout 
) [private]

wait until the socket is ready to write again

References libt2n::error, and EXCEPTIONSTREAM.

Referenced by socket_write().


Member Data Documentation

const unsigned int libt2n::socket_handler::default_recv_buffer_size = 2048 [static, private]
const unsigned int libt2n::socket_handler::default_write_block_size = 4096 [static, private]
const long long libt2n::socket_handler::default_write_timeout = 30000000 [static, private]
unsigned int libt2n::socket_handler::recv_buffer_size [protected]

Referenced by get_type().

unsigned int libt2n::socket_handler::write_block_size [protected]

Referenced by get_write_timeout(), and socket_write().


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

Generated on 13 May 2015 by  doxygen 1.6.1
© Intra2net AG 2024 | Legal | Contact