handles socket based communication. Don't use directly, use socket_server or socket_client_connection instead. More...
#include <socket_handler.hxx>
Public Member Functions | |
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 |
Protected Member Functions | |
socket_handler (int _sock, socket_type_value _socket_type) | |
~socket_handler () | |
void | set_socket_options (int sock) |
set options like fast reuse and keepalive every socket should have | |
virtual std::ostream * | get_logstream (log_level_values level) |
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. | |
Protected Attributes | |
int | sock |
unsigned int | recv_buffer_size |
unsigned int | write_block_size |
long long | write_timeout |
Private Member Functions | |
bool | data_waiting (long long usec_timeout, long long *usec_timeout_remaining=NULL) |
check if new data is waiting on the raw socket | |
void | wait_ready_to_write (int socket, long long write_block_timeout) |
wait until the socket is ready to write again | |
Private Attributes | |
socket_type_value | socket_type |
Static Private Attributes | |
static const unsigned int | default_recv_buffer_size = 2048 |
static const unsigned int | default_write_block_size = 4096 |
static const long long | default_write_timeout = 30000000 |
handles socket based communication. Don't use directly, use socket_server or socket_client_connection instead.
libt2n::socket_handler::socket_handler | ( | int | _sock, | |
socket_type_value | _socket_type | |||
) | [protected] |
libt2n::socket_handler::~socket_handler | ( | ) | [protected] |
void libt2n::socket_handler::close | ( | ) | [protected, virtual] |
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 sock.
Referenced by fill_buffer(), and ~socket_handler().
bool libt2n::socket_handler::data_waiting | ( | long long | usec_timeout, | |
long long * | usec_timeout_remaining = NULL | |||
) | [private] |
check if new data is waiting on the raw socket
[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 sock.
Referenced by fill_buffer().
bool libt2n::socket_handler::fill_buffer | ( | std::string & | buffer | ) | [protected] |
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 close(), data_waiting(), libt2n::debug, libt2n::error, EXCEPTIONSTREAM, fill_buffer(), LOGSTREAM, recv_buffer_size, and sock.
bool libt2n::socket_handler::fill_buffer | ( | std::string & | buffer, | |
long long | usec_timeout, | |||
long long * | usec_timeout_remaining = NULL | |||
) | [protected] |
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 data_waiting().
Referenced by fill_buffer().
virtual std::ostream* libt2n::socket_handler::get_logstream | ( | log_level_values | level | ) | [inline, protected, virtual] |
Reimplemented in libt2n::socket_client_connection, libt2n::socket_server, and libt2n::socket_server_connection.
unsigned int libt2n::socket_handler::get_recv_buffer_size | ( | ) | const [inline] |
References recv_buffer_size.
int libt2n::socket_handler::get_socket | ( | ) | [inline] |
References sock.
socket_type_value libt2n::socket_handler::get_type | ( | ) | [inline] |
is this a tcp or unix socket connection
References 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] |
References write_block_size.
long long libt2n::socket_handler::get_write_timeout | ( | ) | const [inline] |
References write_timeout.
bool libt2n::socket_handler::is_closed | ( | ) |
is the underlying socket connection still open?
References sock.
void libt2n::socket_handler::set_recv_buffer_size | ( | unsigned int | new_recv_buffer_size | ) |
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 recv_buffer_size.
void libt2n::socket_handler::set_socket_options | ( | int | sock | ) | [protected] |
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::socket_handler::set_write_block_size | ( | unsigned int | new_write_block_size | ) |
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 write_block_size.
void libt2n::socket_handler::set_write_timeout | ( | long long | new_write_timeout | ) |
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 write_block_size.
void libt2n::socket_handler::socket_write | ( | const std::string & | data | ) | [protected] |
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, sock, wait_ready_to_write(), write_block_size, and write_timeout.
Referenced by libt2n::socket_server_connection::real_write(), and libt2n::socket_client_connection::real_write().
void libt2n::socket_handler::wait_ready_to_write | ( | int | socket, | |
long long | write_block_timeout | |||
) | [private] |
wait until the socket is ready to write again
References libt2n::error, and EXCEPTIONSTREAM.
Referenced by socket_write().
const unsigned int libt2n::socket_handler::default_recv_buffer_size = 2048 [static, private] |
const unsigned int libt2n::socket_handler::default_write_block_size = 4096 [static, private] |
const long long libt2n::socket_handler::default_write_timeout = 30000000 [static, private] |
unsigned int libt2n::socket_handler::recv_buffer_size [protected] |
Referenced by fill_buffer(), get_recv_buffer_size(), and set_recv_buffer_size().
int libt2n::socket_handler::sock [protected] |
Referenced by libt2n::socket_server_connection::close(), close(), libt2n::socket_client_connection::connect_with_timeout(), data_waiting(), libt2n::socket_server::fill_buffer(), fill_buffer(), get_socket(), is_closed(), libt2n::socket_server::new_connection(), libt2n::socket_server::socket_server(), socket_write(), libt2n::socket_server::start_listening(), libt2n::socket_client_connection::tcp_connect(), libt2n::socket_client_connection::unix_connect(), ~socket_handler(), and libt2n::socket_server_connection::~socket_server_connection().
Referenced by get_type().
unsigned int libt2n::socket_handler::write_block_size [protected] |
Referenced by get_write_block_size(), set_write_block_size(), set_write_timeout(), and socket_write().
long long libt2n::socket_handler::write_timeout [protected] |
Referenced by get_write_timeout(), and socket_write().