Intra2net AG

libt2n::socket_server Class Reference

Socket based server class. More...

#include <socket_server.hxx>

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

List of all members.

Public Member Functions

 socket_server (int port, const std::string &ip="0.0.0.0")
 create a new tcp-based server
 socket_server (const std::string &path, mode_t filemode=00770, const std::string &user="", const std::string &group="")
 create a new unix-socked-based server
 ~socket_server ()
bool fill_buffer (long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
 look for new connections and new data in any of the existing connections
std::set< int > get_sockets_set ()
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
void set_default_timeout (int _default_timeout)
 set the default timeout for new client connections
int get_default_timeout (void)
 get the current default timeout for client connections
void set_logging (std::ostream *_logstream, log_level_values _log_level)
 activate logging to the given stream. everything above the given level is logged.
server_connectionget_connection (unsigned int conn_id)
 Gets a connection by id.
void add_callback (callback_event_type event, const boost::function< void(unsigned int)> &func)
 add a callback
void close ()
void cleanup ()
 check for timeouts, remove closed connections. don't forget to call this from time to time.
bool get_packet (std::string &data)
 get a complete data packet from any client. The packet is removed from the connection buffer.
bool get_packet (std::string &data, unsigned int &conn_id)
 get a complete data packet from any client. The packet is removed from the connection buffer.

Protected Member Functions

std::ostream * get_logstream (log_level_values level)
 get pointer to logging stream, returns NULL if no logging needed
void set_socket_options (int sock)
 set options like fast reuse and keepalive every socket should have
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.
unsigned int add_connection (server_connection *newconn)
 add a new connection to the server
void do_callbacks (callback_event_type event, unsigned int conn_id)
 an event occured, call all server-side callbacks

Protected Attributes

int sock
unsigned int recv_buffer_size
unsigned int write_block_size
long long write_timeout
std::map< unsigned int,
server_connection * > 
connections

Private Member Functions

void start_listening ()
 start listening on a new server socket (called by the constructors)
void new_connection ()
 handle a new connection from a client
bool fill_connection_buffers ()
 call fill_buffer() on all connections, called from fill_buffer()
void remove_connection_socket (int sock)
 remove the socket of a connection after the connection has been closed

Private Attributes

fd_set connection_set
std::string unix_path
std::set< int > sockets_set

Friends

class socket_server_connection

Detailed Description

Socket based server class.

Use this class to instantiate a server listening for client connections. Call fill_buffer() to read data from the network and get_packet() to retrieve this data. Don't forget to call cleanup() from time to time to remove closed connections and close idle ones.


Constructor & Destructor Documentation

libt2n::socket_server::socket_server ( int  port,
const std::string &  ip = "0.0.0.0" 
)

create a new tcp-based server

Parameters:
port tcp port you want to listen on
ip the local ip you want to listen on. "0.0.0.0" means all local ips (default).

References libt2n::error, EXCEPTIONSTREAM, libt2n::socket_handler::set_socket_options(), libt2n::socket_handler::sock, and start_listening().

libt2n::socket_server::socket_server ( const std::string &  path,
mode_t  filemode = 00770,
const std::string &  user = "",
const std::string &  group = "" 
)

create a new unix-socked-based server

Parameters:
path path of the socket
filemode permissions you want to open the socket with
user local username for the socket
group local groupname for the socket

References libt2n::error, EXCEPTIONSTREAM, libt2n::socket_handler::set_socket_options(), libt2n::socket_handler::sock, start_listening(), and unix_path.

libt2n::socket_server::~socket_server (  ) 

Member Function Documentation

void libt2n::server::add_callback ( callback_event_type  event,
const boost::function< void(unsigned int)> &  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 like this: s.add_callback(new_connection,boost::bind(&my_class::func_to_call_back, boost::ref(*this), _1));

References libt2n::server::callbacks, libt2n::server::connections, and libt2n::new_connection.

Referenced by libt2n::command_server::command_server().

unsigned int libt2n::server::add_connection ( server_connection newconn  )  [protected, inherited]
void libt2n::server::cleanup (  )  [inherited]

check for timeouts, remove closed connections. don't forget to call this from time to time.

References libt2n::server::connections, libt2n::debug, and LOGSTREAM.

Referenced by libt2n::command_server::handle().

void libt2n::server::close (  )  [inherited]

Close all open connections

References libt2n::server::connections.

Referenced by ~socket_server().

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

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

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

void libt2n::server::do_callbacks ( callback_event_type  event,
unsigned int  conn_id 
) [protected, inherited]

an event occured, call all server-side callbacks

Parameters:
event event that occured
conn_id connection-id parameter that will be given to the callback-function

References libt2n::server::callbacks.

Referenced by libt2n::server::add_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_server::fill_buffer ( long long  usec_timeout = -1,
long long *  usec_timeout_remaining = NULL 
) [virtual]

look for new connections and new data in any of the existing connections

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::server.

References connection_set, libt2n::error, EXCEPTIONSTREAM, fill_connection_buffers(), new_connection(), and libt2n::socket_handler::sock.

bool libt2n::socket_server::fill_connection_buffers ( void   )  [private, virtual]

call fill_buffer() on all connections, called from fill_buffer()

Implements libt2n::server.

References libt2n::server::connections.

Referenced by fill_buffer().

server_connection * libt2n::server::get_connection ( unsigned int  conn_id  )  [inherited]

Gets a connection by id.

Parameters:
conn_id Connection ID
Return values:
Pointer to connection object

References libt2n::server::connections.

Referenced by libt2n::command_server::handle(), and libt2n::command_server::send_hello().

int libt2n::server::get_default_timeout ( void   )  [inline, inherited]

get the current default timeout for client connections

References libt2n::server::default_timeout.

Referenced by new_connection().

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

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

Reimplemented from libt2n::server.

bool libt2n::server::get_packet ( std::string &  data,
unsigned int &  conn_id 
) [inherited]

get a complete data packet from any client. The packet is removed from the connection buffer.

Parameters:
[out] data the data package
[out] conn_id the connection id we got this packet from
Return values:
true if packet found

References libt2n::server::connections, libt2n::debug, and LOGSTREAM.

bool libt2n::server::get_packet ( std::string &  data  )  [inline, inherited]

get a complete data packet from any client. The packet is removed from the connection buffer.

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

References libt2n::server::get_packet().

Referenced by libt2n::server::get_packet(), and libt2n::command_server::handle().

unsigned int libt2n::socket_handler::get_recv_buffer_size (  )  const [inline, inherited]
int libt2n::socket_handler::get_socket (  )  [inline, inherited]
std::set<int> libt2n::socket_server::get_sockets_set (  )  [inline]

References sockets_set.

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.

void libt2n::socket_server::new_connection (  )  [private]
void libt2n::socket_server::remove_connection_socket ( int  sock  )  [private]
void libt2n::server::set_default_timeout ( int  _default_timeout  )  [inline, inherited]

set the default timeout for new client connections

References libt2n::server::default_timeout.

Referenced by libt2n::server::server().

void libt2n::server::set_logging ( std::ostream *  _logstream,
log_level_values  _log_level 
) [inherited]

activate logging to the given stream. everything above the given level is logged.

References libt2n::server::log_level, and libt2n::server::logstream.

Referenced by libt2n::server::server().

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 libt2n::socket_client_connection::connect_with_timeout(), new_connection(), and 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]
void libt2n::socket_server::start_listening (  )  [private]

start listening on a new server socket (called by the constructors)

References connection_set, libt2n::error, EXCEPTIONSTREAM, libt2n::socket_handler::sock, and sockets_set.

Referenced by socket_server().


Friends And Related Function Documentation

friend class socket_server_connection [friend]

Referenced by new_connection().


Member Data Documentation

std::map<unsigned int, server_connection*> libt2n::server::connections [protected, inherited]
unsigned int libt2n::socket_handler::recv_buffer_size [protected, inherited]
int libt2n::socket_handler::sock [protected, inherited]
std::set<int> libt2n::socket_server::sockets_set [private]
std::string libt2n::socket_server::unix_path [private]

Referenced by socket_server(), and ~socket_server().

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