The Open Master Hearing Aid (openMHA)  openMHA
Open community platform for hearing aid algorithm research
mha_tcp::server_t Class Reference

Class for accepting TCP connections from clients. More...

Inheritance diagram for mha_tcp::server_t:
Inheritance graph

Public Member Functions

 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 ()
 

Private Member Functions

void trigger_accept ()
 Triggers the acceptance of the next connection. More...
 
void post_trigger_read_line (std::shared_ptr< buffered_socket_t > c)
 Call trigger_read_line with a single detour through asio's event loop to avoid starving other connections when one connection floods us. More...
 
void trigger_read_line (std::shared_ptr< buffered_socket_t > c)
 Triggers the reading of the next line from a connection. More...
 
void add_connection (std::shared_ptr< buffered_socket_t > connection)
 Add new connection to the list of connections, retire stale pointers. More...
 

Private Attributes

asio::io_context io_context
 The io context used to run event loops. More...
 
std::shared_ptr< asio::ip::tcp::acceptor > acceptor
 The underlying asio object used to accept incoming TCP connections. More...
 
bool async_accept_has_been_triggered = false
 Set to true when async_acceptance is triggered in trigger_accept (Only one accept can be in process at any time). More...
 
size_t num_accepted_connections = 0U
 Number of accepted connections (not necessarily still existing) More...
 
std::vector< std::weak_ptr< buffered_socket_t > > connections
 Weak pointers to the existing connections. More...
 

Detailed Description

Class for accepting TCP connections from clients.

Constructor & Destructor Documentation

◆ server_t()

mha_tcp::server_t::server_t ( const std::string &  interface,
uint16_t  port 
)

Allocates a TCP server.

Parameters
interfaceHost 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, ...
portTCP port to open for incoming connections. If port==0, then the operating system will select a free port.
Exceptions
system_errorif the name given in interface cannot be resolved
system_errorif we cannot bind to the requested interface

◆ ~server_t()

virtual mha_tcp::server_t::~server_t ( )
virtualdefault

Make destructor virtual.

Member Function Documentation

◆ 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
cthe connection that has received this line
lthe 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()

void mha_tcp::server_t::trigger_read_line ( std::shared_ptr< buffered_socket_t c)
private

Triggers the reading of the next line from a connection.

Parameters
cThe 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.

Member Data Documentation

◆ 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

std::vector<std::weak_ptr<buffered_socket_t> > mha_tcp::server_t::connections
private

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: