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::SimpleTcpServer Class Referencefinal

A simple bi-directional multi-client TCP server, which uses the class MessageHeader as the message header type. More...

#include <SimpleTcpServer.h>

Public Member Functions

 SimpleTcpServer ()=delete
 Default constructor - deleted.
 
 SimpleTcpServer (boost_iocontext_t &ioContext, uint16_t listenPort, const defs::default_message_dispatcher_t &messageDispatcher, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT, size_t memPoolMsgCount=0)
 Initialisation constructor. More...
 
 SimpleTcpServer (uint16_t listenPort, const defs::default_message_dispatcher_t &messageDispatcher, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT, size_t memPoolMsgCount=0)
 Initialisation constructor. More...
 
 ~SimpleTcpServer ()=default
 Default destructor.
 
 SimpleTcpServer (const SimpleTcpServer &)=delete
 Copy constructor - deleted.
 
SimpleTcpServeroperator= (const SimpleTcpServer &)=delete
 Copy assignment operator - deleted.
 
 SimpleTcpServer (SimpleTcpServer &&)=delete
 Move constructor - deleted.
 
SimpleTcpServeroperator= (SimpleTcpServer &&)=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, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a header-only message to a client asynchronously. More...
 
bool SendMessageToClientSync (const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a header-only message to a client synchronously. More...
 
bool SendMessageToAllClients (int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a header-only message to all clients asynchronously. More...
 
bool SendMessageToClientAsync (const defs::connection_t &client, const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a header plus message buffer to a client asynchronously. More...
 
bool SendMessageToClientSync (const defs::connection_t &client, const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a header plus message buffer to a client synchronously. More...
 
bool SendMessageToAllClients (const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a header plus message buffer to all clients asynchronously. More...
 
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool SendMessageToClientAsync (const T &message, const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a full message to a client asynchronously. More...
 
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool SendMessageToClientSync (const T &message, const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a full message to a client synchronously. More...
 
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool SendMessageToAllClients (const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
 Send a full message to all clients asynchronously. More...
 
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 Attributes

messages::MessageBuilder m_messageBuilder {}
 Default message builder object of type core_lib::asio::messages::MessageBuilder.
 
messages::MessageHandler m_messageHandler {}
 Default message handler object of type core_lib::asio::messages::MessageHandler.
 
TcpTypedServer< messages::MessageBuilderm_tcpTypedServer
 Our actual typed TCP server object.
 

Detailed Description

A simple bi-directional multi-client TCP server, which uses the class MessageHeader as the message header type.

Constructor & Destructor Documentation

◆ SimpleTcpServer() [1/2]

core_lib::asio::tcp::SimpleTcpServer::SimpleTcpServer ( boost_iocontext_t ioContext,
uint16_t  listenPort,
const defs::default_message_dispatcher_t messageDispatcher,
eSendOption  sendOption = eSendOption::nagleOn,
size_t  maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
size_t  memPoolMsgCount = 0 
)

Initialisation constructor.

Parameters
[in]ioContext- External boost IO context to manage ASIO.
[in]listenPort- Our listen port for all detected networks.
[in]messageDispatcher- Callback to use to dispatch received messages.
[in]sendOption- Socket send option to control the use of the Nagle algorithm.
[in]maxAllowedUnsentAsyncMessages- Maximum allowed number of unsent async messages.
[in]memPoolMsgCount- Number of messages in pool for received message handling, defaults to 0, which implies no pool used.

Typically use this constructor when managing a bool of threads using an instance of core_lib::asioIoServoceThreadGroup 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.

NOTE: When the message pool feature is used then all messages passed to the the registered dispatcher are managed by the internal pool. Care must be taken in the dispatcher to process the messages as quickly as possibly so the pool doesn't fill and start overwriting older messages. If the messages need to be kept then it is the dispatchers job to make a suitable copy of the received message.

◆ SimpleTcpServer() [2/2]

core_lib::asio::tcp::SimpleTcpServer::SimpleTcpServer ( uint16_t  listenPort,
const defs::default_message_dispatcher_t messageDispatcher,
eSendOption  sendOption = eSendOption::nagleOn,
size_t  maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
size_t  memPoolMsgCount = 0 
)

Initialisation constructor.

Parameters
[in]listenPort- Our listen port for all detected networks.
[in]messageDispatcher- Callback to use to dispatch received messages.
[in]sendOption- Socket send option to control the use of the Nagle algorithm.
[in]maxAllowedUnsentAsyncMessages- Maximum allowed number of unsent async messages.
[in]memPoolMsgCount- Number of messages in pool for received message handling, defaults to 0, which implies no pool used.

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 recommend.

NOTE: When the message pool feature is used then all messages passed to the the registered dispatcher are managed by the internal pool. Care must be taken in the dispatcher to process the messages as quickly as possibly so the pool doesn't fill and start overwriting older messages. If the messages need to be kept then it is the dispatchers job to make a suitable copy of the received message.

Member Function Documentation

◆ CloseAcceptor()

void core_lib::asio::tcp::SimpleTcpServer::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::SimpleTcpServer::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}.

◆ IsConnected()

bool core_lib::asio::tcp::SimpleTcpServer::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::SimpleTcpServer::ListenPort ( ) const

Retrieve this server's listen port.

Returns
The listen port.

◆ NumberOfClients()

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

Retrieve this server's number of clients.

Returns
The number of clients.

◆ NumberOfUnsentAsyncMessages()

size_t core_lib::asio::tcp::SimpleTcpServer::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() [1/4]

bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients ( int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const

Send a header-only message to all clients asynchronously.

Parameters
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where a client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToAllClients() [2/4]

bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients ( const defs::char_buffer_t message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const

Send a header plus message buffer to all clients asynchronously.

Parameters
[in]message- Message buffer.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where a client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToAllClients() [3/4]

template<typename T , typename A = serialize::archives::out_port_bin_t>
bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients ( const T &  message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const
inline

Send a full message to all clients asynchronously.

Parameters
[in]message- The message of type T to send behind the header serialized to an boost::serialization-compatible archive of type A.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the clients should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToAllClients() [4/4]

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

Send a message buffer to all clients asynchronously.

Parameters
[in]message- Message buffer.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToClientAsync() [1/4]

bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync ( const defs::connection_t client,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const

Send a header-only message to a client asynchronously.

Parameters
[in]client- Client connection details.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToClientAsync() [2/4]

bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync ( const defs::connection_t client,
const defs::char_buffer_t message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const

Send a header plus message buffer to a client asynchronously.

Parameters
[in]client- Client connection details.
[in]message- Message buffer.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToClientAsync() [3/4]

template<typename T , typename A = serialize::archives::out_port_bin_t>
bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync ( const T &  message,
const defs::connection_t client,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const
inline

Send a full message to a client asynchronously.

Parameters
[in]message- The message of type T to send behind the header serialized to an boost::serialization-compatible archive of type A.
[in]client- Client connection details.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToClientAsync() [4/4]

bool core_lib::asio::tcp::SimpleTcpServer::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
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToClientSync() [1/4]

bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync ( const defs::connection_t client,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const

Send a header-only message to a client synchronously.

Parameters
[in]client- Client connection details.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of the send as a boolean.

◆ SendMessageToClientSync() [2/4]

bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync ( const defs::connection_t client,
const defs::char_buffer_t message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const

Send a header plus message buffer to a client synchronously.

Parameters
[in]client- Client connection details.
[in]message- Message buffer.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of the send as a boolean.

◆ SendMessageToClientSync() [3/4]

template<typename T , typename A = serialize::archives::out_port_bin_t>
bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync ( const T &  message,
const defs::connection_t client,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
) const
inline

Send a full message to a client synchronously.

Parameters
[in]message- The message of type T to send behind the header serialized to an boost::serialization-compatible archive of type A.
[in]client- Client connection details.
[in]messageId- Unique message ID to insert into message header.
[in]responseAddress- (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket.
Returns
Returns the success state of the send as a boolean.

◆ SendMessageToClientSync() [4/4]

bool core_lib::asio::tcp::SimpleTcpServer::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.

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