a connection from client to server using sockets. More...
#include <socket_client.hxx>
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 |
a connection from client to server using sockets.
Use this class to connect from a client to a server.
typedef uint32_t libt2n::connection::packet_size_indicator [protected, inherited] |
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 | |||
) |
returns a closed connection if connection could not be established, call get_last_error_msg() for details
References close(), connect_timeout_usec, libt2n::debug, libt2n::connection::do_callbacks(), libt2n::connection::is_closed(), lastErrorMsg, LOGSTREAM, max_retries, libt2n::new_connection, port, libt2n::client_connection::set_logging(), tcp_connect(), and libt2n::t2n_exception::what().
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 | |||
) |
returns a closed connection if connection could not be established, call get_last_error_msg() for details
References close(), connect_timeout_usec, libt2n::debug, libt2n::connection::do_callbacks(), libt2n::connection::is_closed(), lastErrorMsg, LOGSTREAM, max_retries, libt2n::new_connection, path, libt2n::client_connection::set_logging(), unix_connect(), and libt2n::t2n_exception::what().
libt2n::socket_client_connection::~socket_client_connection | ( | ) |
Destructor. Closes an open connection.
void libt2n::connection::add_callback | ( | callback_event_type | event, | |
const boost::function< void()> & | func | |||
) | [inherited] |
add a callback
event | event the function will be called at | |
func | functor (see boost function) that will be called |
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
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.
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_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
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.
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().
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.
[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(), 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::socket_handler::is_closed | ( | ) | [inherited] |
is the underlying socket connection still open?
References libt2n::socket_handler::sock.
bool libt2n::connection::is_closed | ( | ) | [inline, inherited] |
is this connection closed or not
References libt2n::connection::closed.
Referenced by libt2n::socket_server_connection::close(), close(), libt2n::connection::close(), libt2n::ReconnectIgnoreFailureSocketWrapper::get_connection(), libt2n::command_client::is_connection_closed(), libt2n::command_client::read_hello(), libt2n::command_client::read_packet(), reconnect(), libt2n::connection::reopen(), socket_client_connection(), and libt2n::connection::~connection().
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_client_connection::real_write | ( | const std::string & | data | ) | [inline, private, virtual] |
Implements libt2n::connection.
References libt2n::socket_handler::socket_write().
void libt2n::socket_client_connection::reconnect | ( | ) |
try to reconnect the current connection with the same connection credentials (host and port or path)
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
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] |
activate logging to the given stream. everything above the given level is logged.
References libt2n::client_connection::log_level, and libt2n::client_connection::logstream.
Referenced by libt2n::client_connection::client_connection(), libt2n::BasicSocketWrapper::set_logging(), libt2n::ConnectionWrapper::set_logging_on_connection(), and socket_client_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 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.
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 libt2n::socket_server_connection::real_write(), and real_write().
void libt2n::socket_client_connection::tcp_connect | ( | int | max_retries | ) | [private] |
establish a connection via tcp
References connect_with_timeout(), libt2n::debug, LOGSTREAM, port, and libt2n::socket_handler::sock.
Referenced by reconnect(), and socket_client_connection().
void libt2n::socket_client_connection::unix_connect | ( | int | max_retries | ) | [private] |
establish a connection via unix-socket
References connect_with_timeout(), libt2n::debug, LOGSTREAM, path, and libt2n::socket_handler::sock.
Referenced by reconnect(), and socket_client_connection().
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().
std::string libt2n::connection::buffer [protected, inherited] |
long long libt2n::socket_client_connection::connect_timeout_usec [private] |
Referenced by connect_with_timeout(), and socket_client_connection().
const long long libt2n::socket_client_connection::connect_timeout_usec_default = 30000000 [static] |
std::string libt2n::socket_client_connection::lastErrorMsg [private] |
Referenced by get_last_error_msg(), and socket_client_connection().
int libt2n::socket_client_connection::max_retries [private] |
Referenced by reconnect(), and socket_client_connection().
const int libt2n::socket_client_connection::max_retries_default = 3 [static] |
std::string libt2n::socket_client_connection::path [private] |
Referenced by socket_client_connection(), and unix_connect().
int libt2n::socket_client_connection::port [private] |
Referenced by socket_client_connection(), and tcp_connect().
unsigned int libt2n::socket_handler::recv_buffer_size [protected, inherited] |
std::string libt2n::socket_client_connection::server [private] |
int libt2n::socket_handler::sock [protected, inherited] |
Referenced by libt2n::socket_server_connection::close(), libt2n::socket_handler::close(), 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(), tcp_connect(), unix_connect(), libt2n::socket_handler::~socket_handler(), and libt2n::socket_server_connection::~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().