Socket based connection. More...
#include <socket_server.hxx>
Public Member Functions | |
bool | fill_buffer (long long usec_timeout=-1, long long *usec_timeout_remaining=NULL) |
look for new data and store it in the local buffer | |
virtual void | close () |
close this connection. complete data waiting in the buffer can still be retrieved. | |
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 | check_timeout () |
check if timeout is expired, close connection if so | |
void | reset_timeout () |
reset the timeout, e.g. if something is received | |
void | set_timeout (int _timeout) |
unsigned int | get_id () |
get the id of this connection within the server object | |
void | add_callback (callback_event_type event, const boost::function< void()> &func) |
add a callback to one connection | |
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 | |
std::list< boost::function < void()> > | get_callback_list (callback_event_type event) |
get the callbacks in place for one event | |
Protected Types | |
typedef uint32_t | packet_size_indicator |
Protected Member Functions | |
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. | |
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 | |
Protected Attributes | |
int | sock |
unsigned int | recv_buffer_size |
unsigned int | write_block_size |
long long | write_timeout |
server * | my_server |
std::string | buffer |
Private Member Functions | |
socket_server_connection (int _sock, socket_type_value _stype, int _timeout) | |
~socket_server_connection () | |
std::ostream * | get_logstream (log_level_values level) |
get pointer to logging stream, returns NULL if no logging needed | |
void | real_write (const std::string &data) |
Friends | |
class | socket_server |
Socket based connection.
This class is used within a socket_server to represent the connection to each client.
typedef uint32_t libt2n::connection::packet_size_indicator [protected, inherited] |
libt2n::socket_server_connection::socket_server_connection | ( | int | _sock, | |
socket_type_value | _stype, | |||
int | _timeout | |||
) | [inline, private] |
libt2n::socket_server_connection::~socket_server_connection | ( | ) | [private] |
Destructor
References libt2n::server_connection::my_server, libt2n::socket_server::remove_connection_socket(), and libt2n::socket_handler::sock.
void libt2n::server_connection::add_callback | ( | callback_event_type | event, | |
const boost::function< void()> & | func | |||
) | [inherited] |
add a callback to one connection
event | event the function will be called at | |
func | functor (see boost::function) that will be called |
Reimplemented from libt2n::connection.
References libt2n::new_connection.
Referenced by libt2n::server::add_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::server_connection::check_timeout | ( | ) | [inherited] |
check if timeout is expired, close connection if so
References libt2n::connection::close(), libt2n::server_connection::connection_id, libt2n::debug, libt2n::server_connection::last_action_time, LOGSTREAM, and libt2n::server_connection::timeout.
void libt2n::socket_server_connection::close | ( | ) | [virtual] |
close this connection. complete data waiting in the buffer can still be retrieved.
Reimplemented from libt2n::socket_handler.
References libt2n::connection::is_closed(), libt2n::server_connection::my_server, libt2n::socket_server::remove_connection_socket(), and libt2n::socket_handler::sock.
void libt2n::connection::do_callbacks | ( | callback_event_type | event | ) | [protected, inherited] |
an event has occured, execute the callbacks that are registered for this event
event | event type that has occured |
References libt2n::connection::callbacks.
Referenced by libt2n::connection::close(), libt2n::connection::reopen(), libt2n::socket_client_connection::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.
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
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_connection::fill_buffer | ( | long long | usec_timeout = -1 , |
|
long long * | usec_timeout_remaining = NULL | |||
) | [virtual] |
look for new data and store it in the local buffer
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::connection.
References libt2n::connection::buffer, and libt2n::server_connection::reset_timeout().
std::list< boost::function< void()> > libt2n::connection::get_callback_list | ( | callback_event_type | event | ) | [inherited] |
get the callbacks in place for one event
event | event the callbacks should be registered for |
References libt2n::connection::callbacks.
Referenced by libt2n::command_client::replace_connection().
unsigned int libt2n::server_connection::get_id | ( | ) | [inline, inherited] |
get the id of this connection within the server object
References libt2n::server_connection::connection_id.
Referenced by libt2n::server_connection::get_logstream(), and libt2n::command_server::handle_packet().
std::ostream* libt2n::socket_server_connection::get_logstream | ( | log_level_values | level | ) | [inline, private, virtual] |
get pointer to logging stream, returns NULL if no logging needed
Reimplemented from libt2n::server_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.
[out] | data | the data package |
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] |
References libt2n::socket_handler::recv_buffer_size.
int libt2n::socket_handler::get_socket | ( | ) | [inline, inherited] |
References 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 libt2n::socket_server::new_connection(), libt2n::socket_client_connection::reconnect(), and libt2n::socket_server::~socket_server().
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.
bool libt2n::connection::is_closed | ( | ) | [inline, inherited] |
is this connection closed or not
References libt2n::connection::closed.
Referenced by close(), libt2n::socket_client_connection::close(), libt2n::connection::close(), libt2n::ReconnectIgnoreFailureSocketWrapper::get_connection(), libt2n::command_client::is_connection_closed(), libt2n::command_client::read_hello(), libt2n::command_client::read_packet(), libt2n::socket_client_connection::reconnect(), libt2n::connection::reopen(), libt2n::socket_client_connection::socket_client_connection(), and libt2n::connection::~connection().
bool libt2n::socket_handler::is_closed | ( | ) | [inherited] |
is the underlying socket connection still open?
References libt2n::socket_handler::sock.
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.
[out] | data | the data package |
full | size of the packet when it will be complete |
References libt2n::connection::buffer.
Referenced by libt2n::command_client::read_hello().
void libt2n::socket_server_connection::real_write | ( | const std::string & | data | ) | [inline, private, virtual] |
Implements libt2n::connection.
References libt2n::socket_handler::socket_write().
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
References libt2n::connection::closed, libt2n::connection::do_callbacks(), libt2n::connection::is_closed(), libt2n::new_connection, and libt2n::connection::remove_incomplete_packets().
Referenced by libt2n::socket_client_connection::reconnect().
void libt2n::server_connection::reset_timeout | ( | ) | [inherited] |
reset the timeout, e.g. if something is received
References libt2n::server_connection::last_action_time.
Referenced by fill_buffer(), and libt2n::server_connection::server_connection().
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 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(), libt2n::socket_server::new_connection(), and libt2n::socket_server::socket_server().
void libt2n::server_connection::set_timeout | ( | int | _timeout | ) | [inline, inherited] |
References libt2n::server_connection::timeout.
Referenced by libt2n::server_connection::server_connection().
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 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
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 real_write(), and libt2n::socket_client_connection::real_write().
void libt2n::connection::write | ( | const std::string & | data | ) | [inherited] |
send a blob to the peer
References libt2n::connection::real_write().
Referenced by libt2n::command_server::handle_packet(), libt2n::command_client::send_command(), and libt2n::command_server::send_hello().
friend class socket_server [friend] |
std::string libt2n::connection::buffer [protected, inherited] |
server* libt2n::server_connection::my_server [protected, inherited] |
unsigned int libt2n::socket_handler::recv_buffer_size [protected, inherited] |
int libt2n::socket_handler::sock [protected, inherited] |
Referenced by close(), libt2n::socket_handler::close(), libt2n::socket_client_connection::connect_with_timeout(), libt2n::socket_handler::data_waiting(), libt2n::socket_server::fill_buffer(), libt2n::socket_handler::fill_buffer(), libt2n::socket_handler::get_socket(), libt2n::socket_handler::is_closed(), libt2n::socket_server::new_connection(), libt2n::socket_server::socket_server(), libt2n::socket_handler::socket_write(), libt2n::socket_server::start_listening(), libt2n::socket_client_connection::tcp_connect(), libt2n::socket_client_connection::unix_connect(), libt2n::socket_handler::~socket_handler(), and ~socket_server_connection().
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().