Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::asio::tcp::TcpServer Class Referencefinal

A bi-directional TCP server. More...

#include <TcpServer.h>

Public Member Functions

 TcpServer ()=delete
 Default constructor - deleted.
 
 TcpServer (boost_iocontext_t &ioContext, uint16_t listenPort, size_t minAmountToRead, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT)
 Initialisation constructor. More...
 
 TcpServer (uint16_t listenPort, size_t minAmountToRead, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT)
 Initialisation constructor. More...
 
 ~TcpServer ()
 Default destructor.
 
 TcpServer (const TcpServer &)=delete
 Copy constructor - deleted.
 
TcpServeroperator= (const TcpServer &)=delete
 Copy assignment operator - deleted.
 
 TcpServer (TcpServer &&)=delete
 Move constructor - deleted.
 
TcpServeroperator= (TcpServer &&)=delete
 Move assignment operator - deleted.
 
defs::connection_t GetServerDetailsForClient (const defs::connection_t &client) const
 Retrieve this server's connection details for a given client. More...
 
uint16_t ListenPort () const
 Retrieve this server's listen port. More...
 
size_t NumberOfClients () const
 Retrieve this server's number of clients. More...
 
void CloseAcceptor ()
 Manually close the acceptor. More...
 
void OpenAcceptor ()
 Manually open the acceptor.
 
bool SendMessageToClientAsync (const defs::connection_t &client, const defs::char_buffer_t &message) const
 Send a message buffer to a client asynchronously. More...
 
bool SendMessageToClientSync (const defs::connection_t &client, const defs::char_buffer_t &message) const
 Send a message buffer to a client synchronously. More...
 
bool SendMessageToAllClients (const defs::char_buffer_t &message) const
 Send a message buffer to all clients asynchronously. More...
 
size_t NumberOfUnsentAsyncMessages (const defs::connection_t &client) const
 Get number of unsent async messages. More...
 
bool IsConnected (const defs::connection_t &client) const
 Tells if a given client is currently connected to the server. More...
 

Private Member Functions

void AcceptConnection ()
 Accept a connection.
 
void AcceptHandler (defs::tcp_conn_ptr_t connection, const boost_sys::error_code &error)
 Accept handler for new connections. More...
 
void ProcessCloseAcceptor ()
 Process closing the acceptor.
 

Private Attributes

std::unique_ptr< IoContextThreadGroupm_ioThreadGroup {}
 I/O context thread group.
 
boost_iocontext_tm_ioContext
 I/O context reference.
 
boost_iocontext_t::strand m_strand
 I/O context strand.
 
std::unique_ptr< boost_tcp_acceptor_tm_acceptor {}
 The connection acceptor.
 
uint16_t m_listenPort {0}
 Server listen port.
 
size_t m_minAmountToRead {0}
 Minimum amount to read from socket.
 
defs::check_bytes_left_to_read_t m_checkBytesLeftToRead {}
 Callback to check number of bytes left to read.
 
defs::message_received_handler_t m_messageReceivedHandler {}
 Callback to handle received message.
 
eSendOption m_sendOption {eSendOption::nagleOn}
 Socket receive option.
 
size_t m_maxAllowedUnsentAsyncMessages {MAX_UNSENT_ASYNC_MSG_COUNT}
 Max allowed unsent async message counter.
 
TcpConnections m_clientConnections {}
 TCP connections object.
 
threads::SyncEvent m_closedEvent {}
 Close event.
 

Detailed Description

A bi-directional TCP server.

This class forms the underpinnings of the TcpTypedServer class.

This class is also suitable when the user only wants to deal with char buffers for network messages.

Constructor & Destructor Documentation

◆ TcpServer() [1/2]

core_lib::asio::tcp::TcpServer::TcpServer ( boost_iocontext_t ioContext,
uint16_t  listenPort,
size_t  minAmountToRead,
const defs::check_bytes_left_to_read_t checkBytesLeftToRead,
const defs::message_received_handler_t messageReceivedHandler,
eSendOption  sendOption = eSendOption::nagleOn,
size_t  maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT 
)

Initialisation constructor.

Parameters
[in]ioContext- External boost IO context to manage ASIO.
[in]listenPort- Our listen port for all detected networks.
[in]minAmountToRead- Minimum amount of data to read on each receive, typical size of header block.
[in]checkBytesLeftToRead- Function object capable of decoding the message and computing how many bytes are left until a complete message.
[in]messageReceivedHandler- Function object capable of handling a received message and dispatching it accordingly.
[in]sendOption- Socket send option to control the use of the Nagle algorithm.
[in]maxAllowedUnsentAsyncMessages- Maximum allowed number of unsent async messages.

Typically use this constructor when managing a bool of threads using an instance of core_lib::asio::IoContextThreadGroup in your application to manage a pool of std::threads. This means you can use a single thread pool and all ASIO operations will be executed using this thread pool managed by a single IO context. This is the recommended constructor.

◆ TcpServer() [2/2]

core_lib::asio::tcp::TcpServer::TcpServer ( uint16_t  listenPort,
size_t  minAmountToRead,
const defs::check_bytes_left_to_read_t checkBytesLeftToRead,
const defs::message_received_handler_t messageReceivedHandler,
eSendOption  sendOption = eSendOption::nagleOn,
size_t  maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT 
)

Initialisation constructor.

Parameters
[in]listenPort- Our listen port for all detected networks.
[in]minAmountToRead- Minimum amount of data to read on each receive, typical size of header block.
[in]checkBytesLeftToRead- Function object capable of decoding the message and computing how many bytes are left until a complete message.
[in]messageReceivedHandler- Function object capable of handling a received message and dispatching it accordingly.
[in]sendOption- Socket send option to control the use of the Nagle algorithm.

This constructor does not require an external IO context to run instead it creates its own IO context object along with its own thread. For very simple cases this version will be fine but in more performance and resource critical situations the external IO context constructor is recommended.

Member Function Documentation

◆ AcceptHandler()

void core_lib::asio::tcp::TcpServer::AcceptHandler ( defs::tcp_conn_ptr_t  connection,
const boost_sys::error_code &  error 
)
private

Accept handler for new connections.

Parameters
[in]connection- Client connection.
[in]error- An error code if fault occurred.

◆ CloseAcceptor()

void core_lib::asio::tcp::TcpServer::CloseAcceptor ( )

Manually close the acceptor.

Note that this object is RAII so will automatically close the acceptor in its destructor.

◆ GetServerDetailsForClient()

auto core_lib::asio::tcp::TcpServer::GetServerDetailsForClient ( const defs::connection_t client) const

Retrieve this server's connection details for a given client.

Parameters
[in]client- A client's connection details.
Returns
- Connection object describing target server's address and port.

If no such client is known to the server then it returns { "0.0.0.0", listenPort}.

Throws std::invalid_argument is remoteEnd is not valid.

◆ IsConnected()

bool core_lib::asio::tcp::TcpServer::IsConnected ( const defs::connection_t client) const

Tells if a given client is currently connected to the server.

Parameters
[in]target- Target connection details.
Returns
true if connected, false if not

◆ ListenPort()

uint16_t core_lib::asio::tcp::TcpServer::ListenPort ( ) const

Retrieve this server's listen port.

Returns
The listen port.

◆ NumberOfClients()

size_t core_lib::asio::tcp::TcpServer::NumberOfClients ( ) const

Retrieve this server's number of clients.

Returns
The number of clients.

◆ NumberOfUnsentAsyncMessages()

size_t core_lib::asio::tcp::TcpServer::NumberOfUnsentAsyncMessages ( const defs::connection_t client) const

Get number of unsent async messages.

Parameters
[in]client- Target connection details.
Returns
Number of unsent messages

◆ SendMessageToAllClients()

bool core_lib::asio::tcp::TcpServer::SendMessageToAllClients ( const defs::char_buffer_t message) const

Send a message buffer to all clients asynchronously.

Parameters
[in]message- Message buffer.
Returns
True if sent successfully, false otherwise.

◆ SendMessageToClientAsync()

bool core_lib::asio::tcp::TcpServer::SendMessageToClientAsync ( const defs::connection_t client,
const defs::char_buffer_t message 
) const

Send a message buffer to a client asynchronously.

Parameters
[in]client- Client connection details.
[in]message- Message buffer.
Returns
True if sent successfully, false otherwise.

◆ SendMessageToClientSync()

bool core_lib::asio::tcp::TcpServer::SendMessageToClientSync ( const defs::connection_t client,
const defs::char_buffer_t message 
) const

Send a message buffer to a client synchronously.

Parameters
[in]client- Client connection details.
[in]message- Message buffer.
Returns
True if sent successfully, false otherwise.

The documentation for this class was generated from the following files: