TCP Server class. More...
#include <tcp_server.h>


Public Types | |
|
typedef delegate2< const socket &, const socket &, socket * > | on_io_handler |
|
typedef delegate3< const socket &, std::istream &, std::ostream &, void > | on_connect_handler |
| typedef delegate1< socket &, void > | on_disconnect_handler |
|
typedef delegate3< const socket &, std::istream &, std::ostream &, bool > | on_process_data_handler |
|
typedef delegate1< const dbp::exception &, void > | on_exception_handler |
Public Member Functions | |
| tcp_server (size_t worker_threads=2, size_t queue_size=32768) | |
| Constructor. | |
| ~tcp_server () | |
| Destructor. | |
| int | timeout () |
| Get timeout value. | |
| tcp_server & | timeout (int value) |
| Set timeout value. | |
| void | start (const std::string &bind="*:0") |
| Start the server. | |
| void | stop () |
| Stop the server. | |
| bool | is_running () |
| Detect server status. | |
| void | on_create_io_handler (on_io_handler handler) |
| Assign 'on_create_io' event handler. | |
| void | on_connect (on_connect_handler handler) |
| void | on_disconnect (on_disconnect_handler handler) |
| void | on_process_data (on_process_data_handler handler) |
| void | on_exception (on_exception_handler handler) |
TCP Server class.
This class provides generic TCP server features: it listens on the address/port specified, accepts new connections and process them. All networking details are hidden from the user; the interaction with the client is doing by events.
The server is asynchronous and multithreaded: one thread is listening the port; working threads are deal with data. There is the pool of incoming connections - the maximum number of active clients.
| bool dbp::tcp_server::is_running | ( | ) |
Detect server status.
| void dbp::tcp_server::on_create_io_handler | ( | on_io_handler | handler | ) | [inline] |
Assign 'on_create_io' event handler.
Your class can assign the event handler to the tcp_server on_connect event. The handler is called by tcp_server class when the client connects to server.
| handler | the event handler delegate |
| void dbp::tcp_server::start | ( | const std::string & | bind = "*:0" |
) |
Start the server.
Starts the socket listening and processes the requests. The listening will completed on addresses and ports provided by bind parameter in the following format:
host:port;host:port;...
where host is IP address or host name to listen on, or '*' meaning any address and port is the port number, for example: "*:80;localhost:443". The zero port meaning random port available selected by OS.
| bind | the address(es) and port(s) to listen on |
| void dbp::tcp_server::stop | ( | ) |
Stop the server.
Signals to all working tasks to stop and shuts down the server.