Intra2net AG

libt2n::socket_client_connection Class Reference

a connection from client to server using sockets. More...

#include <socket_client.hxx>

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

List of all members.

Public Member Functions

 socket_client_connection (int _port, const std::string &_server="127.0.0.1", long long _connect_timeout_usec=connect_timeout_usec_default, int _max_retries=max_retries_default, std::ostream *_logstream=NULL, log_level_values _log_level=none)
 returns a closed connection if connection could not be established, call get_last_error_msg() for details
 socket_client_connection (const std::string &_path, long long _connect_timeout_usec=connect_timeout_usec_default, int _max_retries=max_retries_default, std::ostream *_logstream=NULL, log_level_values _log_level=none)
 returns a closed connection if connection could not be established, call get_last_error_msg() for details
 ~socket_client_connection ()
bool fill_buffer (long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
 read data from the socket and copy it into buffer
virtual void close ()
void reconnect ()
 try to reconnect the current connection with the same connection credentials (host and port or path)
std::string get_last_error_msg (void)
void set_logging (std::ostream *_logstream, log_level_values _log_level)
 activate logging to the given stream. everything above the given level is logged.
bool is_closed ()
 is this connection closed or not
bool get_packet (std::string &data)
 read a complete data packet from the buffer. The packet is removed from the connection buffer.
unsigned int peek_packet (std::string &data)
 get (maybe incomplete) data of the next packet from the buffer. Does not remove the data from the connection buffer.
bool packet_available ()
 returns true if a complete data packet is in the buffer. retrieve it with get_packet().
void write (const std::string &data)
 send a blob to the peer
void add_callback (callback_event_type event, const boost::function< void()> &func)
 add a callback
std::list< boost::function
< void()> > 
get_callback_list (callback_event_type event)
 get the callbacks in place for one event
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

Static Public Attributes

static const int max_retries_default = 3
static const long long connect_timeout_usec_default = 30000000

Protected Types

typedef uint32_t packet_size_indicator

Protected Member Functions

std::ostream * get_logstream (log_level_values level)
 get pointer to logging stream, returns NULL if no logging needed
packet_size_indicator bytes_available ()
 get the number of bytes being available as next complete packet
void do_callbacks (callback_event_type event)
 an event has occured, execute the callbacks that are registered for this event
void reopen (void)
 reopen a already closed connection, removes incomplete packets from the buffer
void remove_incomplete_packets ()
 remove all data from buffer that is not a complete packet
void set_socket_options (int sock)
 set options like fast reuse and keepalive every socket should have
void socket_write (const std::string &data)
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

std::string buffer
int sock
unsigned int recv_buffer_size
unsigned int write_block_size
long long write_timeout

Private Member Functions

void real_write (const std::string &data)
void tcp_connect (int max_retries)
 establish a connection via tcp
void unix_connect (int max_retries)
 establish a connection via unix-socket
void connect_with_timeout (struct sockaddr *sock_addr, unsigned int sockaddr_size)
 execute a connect on a prepared socket (tcp or unix) respecting timeouts

Private Attributes

int max_retries
long long connect_timeout_usec
std::string path
std::string server
int port
std::string lastErrorMsg

Detailed Description

a connection from client to server using sockets.

Use this class to connect from a client to a server.


Member Typedef Documentation

typedef uint32_t libt2n::connection::packet_size_indicator [protected, inherited]

Constructor & Destructor Documentation

libt2n::socket_client_connection::socket_client_connection ( int  _port,
const std::string &  _server = "127.0.0.1",
long long  _connect_timeout_usec = connect_timeout_usec_default,
int  _max_retries = max_retries_default,
std::ostream *  _logstream = NULL,
log_level_values  _log_level = none 
)
libt2n::socket_client_connection::socket_client_connection ( const std::string &  _path,
long long  _connect_timeout_usec = connect_timeout_usec_default,
int  _max_retries = max_retries_default,
std::ostream *  _logstream = NULL,
log_level_values  _log_level = none 
)
libt2n::socket_client_connection::~socket_client_connection (  ) 

Destructor. Closes an open connection.


Member Function Documentation

void libt2n::connection::add_callback ( callback_event_type  event,
const boost::function< void()> &  func 
) [inherited]

add a callback

Parameters:
event event the function will be called at
func functor (see boost function) that will be called
Note:
use boost::bind to bind to member functions and parameters like this: 17 is a fixed parameter that is always added to the call c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17));

Reimplemented in libt2n::server_connection.

References libt2n::connection::callbacks.

Referenced by libt2n::command_client::command_client(), and libt2n::command_client::replace_connection().

connection::packet_size_indicator libt2n::connection::bytes_available (  )  [protected, inherited]

get the number of bytes being available as next complete packet

References libt2n::connection::buffer.

Referenced by libt2n::connection::get_packet(), and libt2n::connection::packet_available().

void libt2n::socket_client_connection::close (  )  [virtual]

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

Reimplemented from libt2n::socket_handler.

References libt2n::connection::is_closed().

Referenced by reconnect(), and socket_client_connection().

void libt2n::socket_client_connection::connect_with_timeout ( struct sockaddr *  sock_addr,
unsigned int  sockaddr_size 
) [private]

execute a connect on a prepared socket (tcp or unix) respecting timeouts

References connect_timeout_usec, libt2n::debug, libt2n::error, EXCEPTIONSTREAM, LOGSTREAM, libt2n::socket_handler::set_socket_options(), and libt2n::socket_handler::sock.

Referenced by tcp_connect(), and unix_connect().

void libt2n::connection::do_callbacks ( callback_event_type  event  )  [protected, inherited]

an event has occured, execute the callbacks that are registered for this event

Parameters:
event event type that has occured

References libt2n::connection::callbacks.

Referenced by libt2n::connection::close(), libt2n::connection::reopen(), socket_client_connection(), and libt2n::connection::~connection().

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

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 libt2n::socket_handler::close(), libt2n::socket_handler::data_waiting(), libt2n::debug, libt2n::error, EXCEPTIONSTREAM, libt2n::socket_handler::fill_buffer(), LOGSTREAM, libt2n::socket_handler::recv_buffer_size, and libt2n::socket_handler::sock.

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

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 libt2n::socket_handler::data_waiting().

Referenced by libt2n::socket_handler::fill_buffer().

bool libt2n::socket_client_connection::fill_buffer ( long long  usec_timeout = -1,
long long *  usec_timeout_remaining = NULL 
) [inline, virtual]

read data from the socket and copy it into buffer

Parameters:
usec_timeout wait until new data is found, max timeout usecs. -1: wait endless 0: return instantly
usec_timeout_remaining if non-NULL the function will write the not used time to the given target
Return values:
true if new data was found (does not mean that the received data is a complete packet though)

Implements libt2n::connection.

References libt2n::connection::buffer.

std::list< boost::function< void()> > libt2n::connection::get_callback_list ( callback_event_type  event  )  [inherited]

get the callbacks in place for one event

Parameters:
event event the callbacks should be registered for
Returns:
std::list of functors (boost::function) with the callbacks
Note:
if you want to get the callbacks for all events, loop from 0 to __events_end

References libt2n::connection::callbacks.

Referenced by libt2n::command_client::replace_connection().

std::string libt2n::socket_client_connection::get_last_error_msg ( void   )  [inline]

References lastErrorMsg.

std::ostream* libt2n::socket_client_connection::get_logstream ( log_level_values  level  )  [inline, protected, virtual]

get pointer to logging stream, returns NULL if no logging needed

Reimplemented from libt2n::client_connection.

bool libt2n::connection::get_packet ( std::string &  data  )  [inherited]

read a complete data packet from the buffer. The packet is removed from the connection buffer.

Parameters:
[out] data the data package
Return values:
true if packet found

References libt2n::connection::buffer, and libt2n::connection::bytes_available().

Referenced by libt2n::command_client::read_hello(), and libt2n::command_client::read_packet().

unsigned int libt2n::socket_handler::get_recv_buffer_size (  )  const [inline, inherited]
int libt2n::socket_handler::get_socket (  )  [inline, inherited]
socket_type_value libt2n::socket_handler::get_type (  )  [inline, inherited]
unsigned int libt2n::socket_handler::get_write_block_size (  )  const [inline, inherited]
long long libt2n::socket_handler::get_write_timeout (  )  const [inline, inherited]
bool libt2n::socket_handler::is_closed (  )  [inherited]

is the underlying socket connection still open?

References libt2n::socket_handler::sock.

bool libt2n::connection::is_closed (  )  [inline, inherited]
bool libt2n::connection::packet_available (  )  [inline, inherited]

returns true if a complete data packet is in the buffer. retrieve it with get_packet().

References libt2n::connection::bytes_available().

unsigned int libt2n::connection::peek_packet ( std::string &  data  )  [inherited]

get (maybe incomplete) data of the next packet from the buffer. Does not remove the data from the connection buffer.

Parameters:
[out] data the data package
Return values:
full size of the packet when it will be complete

References libt2n::connection::buffer.

Referenced by libt2n::command_client::read_hello().

void libt2n::socket_client_connection::real_write ( const std::string &  data  )  [inline, private, virtual]
void libt2n::socket_client_connection::reconnect (  ) 

try to reconnect the current connection with the same connection credentials (host and port or path)

Note:
will throw an exeption if reconnecting not possible

References close(), libt2n::debug, libt2n::socket_handler::get_type(), libt2n::connection::is_closed(), LOGSTREAM, max_retries, libt2n::connection::reopen(), tcp_connect(), libt2n::tcp_s, unix_connect(), and libt2n::unix_s.

void libt2n::connection::remove_incomplete_packets (  )  [protected, inherited]

remove all data from buffer that is not a complete packet

References libt2n::connection::buffer.

Referenced by libt2n::connection::reopen().

void libt2n::connection::reopen ( void   )  [protected, inherited]

reopen a already closed connection, removes incomplete packets from the buffer

Note:
Only call when the connection is closed.
Justs cares about the data of connection, reconnecting has to be done in a derived class.

References libt2n::connection::closed, libt2n::connection::do_callbacks(), libt2n::connection::is_closed(), libt2n::new_connection, and libt2n::connection::remove_incomplete_packets().

Referenced by reconnect().

void libt2n::client_connection::set_logging ( std::ostream *  _logstream,
log_level_values  _log_level 
) [inherited]
void libt2n::socket_handler::set_recv_buffer_size ( unsigned int  new_recv_buffer_size  )  [inherited]

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 libt2n::socket_handler::recv_buffer_size.

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

set options like fast reuse and keepalive every socket should have

References libt2n::error, and EXCEPTIONSTREAM.

Referenced by 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  )  [inherited]

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 libt2n::socket_handler::write_block_size.

void libt2n::socket_handler::set_write_timeout ( long long  new_write_timeout  )  [inherited]

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 libt2n::socket_handler::write_block_size.

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

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, libt2n::socket_handler::sock, libt2n::socket_handler::wait_ready_to_write(), libt2n::socket_handler::write_block_size, and libt2n::socket_handler::write_timeout.

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

void libt2n::socket_client_connection::tcp_connect ( int  max_retries  )  [private]
void libt2n::socket_client_connection::unix_connect ( int  max_retries  )  [private]
void libt2n::connection::write ( const std::string &  data  )  [inherited]

Member Data Documentation

std::string libt2n::connection::buffer [protected, inherited]
unsigned int libt2n::socket_handler::recv_buffer_size [protected, inherited]
int libt2n::socket_handler::sock [protected, inherited]
unsigned int libt2n::socket_handler::write_block_size [protected, inherited]
long long libt2n::socket_handler::write_timeout [protected, inherited]

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