#include <socket_server.hxx>

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 | |
| socket_type_value | get_type () |
| is this a tcp or unix socket connection | |
| 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_connection * | get_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 | 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. | |
| 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 |
Friends | |
| class | socket_server_connection |
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.
| libt2n::socket_server::socket_server | ( | int | port, | |
| const std::string & | ip = "0.0.0.0" | |||
| ) |
create a new tcp-based server
| 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
| 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 | ( | ) |
References libt2n::socket_handler::close(), libt2n::socket_handler::get_type(), unix_path, and libt2n::unix_s.
| void libt2n::socket_server::start_listening | ( | ) | [private] |
start listening on a new server socket (called by the constructors)
References connection_set, libt2n::error, EXCEPTIONSTREAM, and libt2n::socket_handler::sock.
Referenced by socket_server().
| void libt2n::socket_server::new_connection | ( | ) | [private] |
handle a new connection from a client
References libt2n::server::add_connection(), connection_set, libt2n::error, EXCEPTIONSTREAM, libt2n::server::get_default_timeout(), libt2n::socket_handler::get_type(), LOGSTREAM, libt2n::socket_handler::set_socket_options(), libt2n::socket_handler::sock, and socket_server_connection.
Referenced by fill_buffer().
| 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().
| void libt2n::socket_server::remove_connection_socket | ( | int | sock | ) | [private] |
| 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.
References libt2n::server::get_logstream().
| 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
| 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 |
| 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.
| 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::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 libt2n::socket_client_connection::real_write().
| 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_server_connection::close(), libt2n::socket_client_connection::close(), libt2n::socket_handler::fill_buffer(), and ~socket_server().
| 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
| 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_server_connection::fill_buffer(), libt2n::socket_handler::fill_buffer(), and libt2n::socket_client_connection::fill_buffer().
| 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.
| 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.
| socket_type_value libt2n::socket_handler::get_type | ( | ) | [inline, inherited] |
is this a tcp or unix socket connection
References libt2n::socket_handler::socket_type.
Referenced by new_connection(), libt2n::socket_client_connection::reconnect(), and ~socket_server().
| bool libt2n::socket_handler::is_closed | ( | ) | [inherited] |
| void libt2n::socket_handler::set_recv_buffer_size | ( | unsigned int | new_recv_buffer_size | ) | [inherited] |
set a new size for the receive buffer.
| new_recv_buffer_size | the new size for the receive buffer. |
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_write_block_size | ( | unsigned int | new_write_block_size | ) | [inherited] |
set new size for the data chunks when writeing.
| new_write_block_size | the new chunk size. |
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
| new_write_timeout | the new timeout in usecs, -1: wait endless |
References libt2n::socket_handler::write_block_size.
| unsigned int libt2n::socket_handler::get_recv_buffer_size | ( | ) | const [inline, inherited] |
References libt2n::socket_handler::recv_buffer_size.
| unsigned int libt2n::socket_handler::get_write_block_size | ( | ) | const [inline, inherited] |
References libt2n::socket_handler::write_block_size.
| long long libt2n::socket_handler::get_write_timeout | ( | ) | const [inline, inherited] |
References libt2n::socket_handler::write_timeout.
| int libt2n::server::add_connection | ( | server_connection * | newconn | ) | [protected, inherited] |
add a new connection to the server
References libt2n::__events_end, libt2n::server_connection::add_callback(), libt2n::server::callbacks, libt2n::connection_closed, libt2n::server::connections, libt2n::debug, libt2n::server::do_callbacks(), LOGSTREAM, libt2n::new_connection, libt2n::server::next_id, libt2n::server_connection::set_id(), and libt2n::server_connection::set_server().
Referenced by new_connection().
| void libt2n::server::do_callbacks | ( | callback_event_type | event, | |
| unsigned int | conn_id | |||
| ) | [protected, inherited] |
an event occured, call all server-side callbacks
| 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().
| 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().
| 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().
| 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().
| server_connection * libt2n::server::get_connection | ( | unsigned int | conn_id | ) | [inherited] |
Gets a connection by id.
| conn_id | Connection ID |
| Pointer | to connection object |
References libt2n::server::connections.
Referenced by libt2n::command_server::handle(), and libt2n::command_server::send_hello().
| void libt2n::server::add_callback | ( | callback_event_type | event, | |
| const boost::function< void(unsigned int)> & | func | |||
| ) | [inherited] |
add a callback
| event | event the function will be called at | |
| func | functor (see boost::function) that will be called |
References libt2n::server::callbacks, libt2n::server::connections, and libt2n::new_connection.
Referenced by libt2n::command_server::command_server().
| 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().
| 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.
| [out] | data | the data package |
| true | if packet found |
Referenced by libt2n::command_server::handle().
| 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.
| [out] | data | the data package |
| [out] | conn_id | the connection id we got this packet from |
| true | if packet found |
References libt2n::server::connections, libt2n::debug, and LOGSTREAM.
friend class socket_server_connection [friend] |
Referenced by new_connection().
fd_set libt2n::socket_server::connection_set [private] |
Referenced by fill_buffer(), new_connection(), remove_connection_socket(), and start_listening().
std::string libt2n::socket_server::unix_path [private] |
Referenced by socket_server(), and ~socket_server().
int libt2n::socket_handler::sock [protected, inherited] |
Referenced by libt2n::socket_server_connection::close(), libt2n::socket_handler::close(), libt2n::socket_client_connection::connect_with_timeout(), libt2n::socket_handler::data_waiting(), fill_buffer(), libt2n::socket_handler::fill_buffer(), libt2n::socket_handler::is_closed(), new_connection(), socket_server(), libt2n::socket_handler::socket_write(), start_listening(), libt2n::socket_client_connection::tcp_connect(), and libt2n::socket_client_connection::unix_connect().
unsigned int libt2n::socket_handler::recv_buffer_size [protected, inherited] |
unsigned int libt2n::socket_handler::write_block_size [protected, inherited] |
long long libt2n::socket_handler::write_timeout [protected, inherited] |
Referenced by libt2n::socket_handler::get_write_timeout(), and libt2n::socket_handler::socket_write().
std::map<unsigned int, server_connection*> libt2n::server::connections [protected, inherited] |
1.5.6