Class for accepting TCP connections from clients.
More...
|
| server_t (const std::string &interface, uint16_t port) |
| Allocates a TCP server. More...
|
|
uint16_t | get_port () const |
|
asio::ip::tcp::endpoint | get_endpoint () const |
|
asio::ip::address | get_address () const |
|
size_t | get_num_accepted_connections () const |
|
void | run () |
| Accepts connections on the TCP port and serves them. More...
|
|
virtual bool | on_received_line (std::shared_ptr< buffered_socket_t > c, const std::string &l) |
| This method is invoked when a line of text is received on one of the accepted connections. More...
|
|
virtual void | shutdown () |
| Shuts down the server: Close the acceptor (no new connections), shuts down the receiving direction of all accepted connections (no new commands, but responses can still be finished), registers a timer event that will cause event loop termination and return from the run() method 1 second in the future, giving us reasonably enough time for the pending responses to be sent out. More...
|
|
virtual | ~server_t ()=default |
| Make destructor virtual. More...
|
|
asio::io_context & | get_context () |
|
Class for accepting TCP connections from clients.
◆ server_t()
mha_tcp::server_t::server_t |
( |
const std::string & |
interface, |
|
|
uint16_t |
port |
|
) |
| |
Allocates a TCP server.
- Parameters
-
interface | Host name of the network interface to listen on. Can be "localhost" or "127.0.0.1" for localhost, "0.0.0.0" for any ipv4 interface, ... |
port | TCP port to open for incoming connections. If port==0, then the operating system will select a free port. |
- Exceptions
-
system_error | if the name given in interface cannot be resolved |
system_error | if we cannot bind to the requested interface |
◆ ~server_t()
virtual mha_tcp::server_t::~server_t |
( |
| ) |
|
|
virtualdefault |
◆ get_port()
uint16_t mha_tcp::server_t::get_port |
( |
| ) |
const |
- Returns
- The port number of the TCP port that has been opened. If the port specified in the constructor was 0, this will return the port that the operating system has selected.
◆ get_endpoint()
asio::ip::tcp::endpoint mha_tcp::server_t::get_endpoint |
( |
| ) |
const |
- Returns
- The local endpoint of the acceptor.
◆ get_address()
asio::ip::address mha_tcp::server_t::get_address |
( |
| ) |
const |
- Returns
- The ip adress that the server is bound to.
◆ get_num_accepted_connections()
size_t mha_tcp::server_t::get_num_accepted_connections |
( |
| ) |
const |
- Returns
- the number of TCP connections that ha ve been accepted
◆ run()
void mha_tcp::server_t::run |
( |
| ) |
|
Accepts connections on the TCP port and serves them.
Triggers the acceptance of the next connection to start things off.
◆ on_received_line()
bool mha_tcp::server_t::on_received_line |
( |
std::shared_ptr< buffered_socket_t > |
c, |
|
|
const std::string & |
l |
|
) |
| |
|
virtual |
This method is invoked when a line of text is received on one of the accepted connections.
Override this method to process the communication with the client.
- Parameters
-
c | the connection that has received this line |
l | the line that has been received, without the line ending |
- Returns
- client should return true when client wants to read another line of text, else false.
Reimplemented in mhaserver_t::tcp_server_t.
◆ shutdown()
void mha_tcp::server_t::shutdown |
( |
| ) |
|
|
virtual |
Shuts down the server: Close the acceptor (no new connections), shuts down the receiving direction of all accepted connections (no new commands, but responses can still be finished), registers a timer event that will cause event loop termination and return from the run() method 1 second in the future, giving us reasonably enough time for the pending responses to be sent out.
◆ get_context()
asio::io_context & mha_tcp::server_t::get_context |
( |
| ) |
|
- Returns
- the asio io context used to run the event loop
◆ trigger_accept()
void mha_tcp::server_t::trigger_accept |
( |
| ) |
|
|
private |
Triggers the acceptance of the next connection.
Called from run to accept the first connection and from the accept handler to accept each next connection. Once a connection from a client is accepted, the accept handler will register it with the event loop for receiving a line of text.
◆ post_trigger_read_line()
void mha_tcp::server_t::post_trigger_read_line |
( |
std::shared_ptr< buffered_socket_t > |
c | ) |
|
|
private |
Call trigger_read_line with a single detour through asio's event loop to avoid starving other connections when one connection floods us.
◆ trigger_read_line()
Triggers the reading of the next line from a connection.
- Parameters
-
c | The connection where the incoming data is expected from. |
◆ add_connection()
void mha_tcp::server_t::add_connection |
( |
std::shared_ptr< buffered_socket_t > |
connection | ) |
|
|
inlineprivate |
Add new connection to the list of connections, retire stale pointers.
◆ io_context
asio::io_context mha_tcp::server_t::io_context |
|
private |
The io context used to run event loops.
◆ acceptor
std::shared_ptr<asio::ip::tcp::acceptor> mha_tcp::server_t::acceptor |
|
private |
The underlying asio object used to accept incoming TCP connections.
◆ async_accept_has_been_triggered
bool mha_tcp::server_t::async_accept_has_been_triggered = false |
|
private |
Set to true when async_acceptance is triggered in trigger_accept (Only one accept can be in process at any time).
◆ num_accepted_connections
size_t mha_tcp::server_t::num_accepted_connections = 0U |
|
private |
Number of accepted connections (not necessarily still existing)
◆ connections
Weak pointers to the existing connections.
Needed to shutdown the active connections for incoming data when server shuts down.
The documentation for this class was generated from the following files: