Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

Public Member Functions | Private Attributes | List of all members
core_lib::asio::tcp::TcpTypedServer< MsgBldr > Class Template Referencefinal

A generic bi-directional TCP server. More...

`#include <TcpTypedServer.h>`

## Public Member Functions
| TcpTypedServer ()=delete
| Default constructor - deleted.
| TcpTypedServer (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, const MsgBldr &messageBuilder, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT)
| Initialisation constructor. More...
| TcpTypedServer (uint16_t listenPort, size_t minAmountToRead, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, const MsgBldr &messageBuilder, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT)
| Initialisation constructor. More...
| ~TcpTypedServer ()=default
| Default destructor.
| TcpTypedServer (const TcpTypedServer &)=delete
| Copy constructor - deleted.
TcpTypedServer & | operator= (const TcpTypedServer &)=delete
| Copy assignment operator - deleted.
| TcpTypedServer (TcpTypedServer &&)=delete
| Move constructor - deleted.
TcpTypedServer & | operator= (TcpTypedServer &&)=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 (int32_t messageId, const defs::char_buffer_t &message, 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
std::mutex | m_sendMutex
| Send message mutex.
const MsgBldr & | m_messageBuilder
| Referece to our message builder object.
TcpServer | m_tcpServer
| General purpose TCP server object.

## Detailed Description

### template<typename MsgBldr>
class core_lib::asio::tcp::TcpTypedServer< MsgBldr >

A generic bi-directional TCP server.

The template argument defines a message builder object that must have an interface compatible with that of the class core_lib::asio::messages::MessageBuilder.

This class forms the underpinnings of the SimpleTcpServer class.

This is also the class to be used when the user wants to specify their own message builder type and message header type.

## Constructor & Destructor Documentation

## ◆ TcpTypedServer() [1/2]

template<typename MsgBldr>

core_lib::asio::tcp::TcpTypedServer< MsgBldr >::TcpTypedServer | ( | 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,
|  | const MsgBldr & | messageBuilder,
|  | eSendOption | sendOption = `eSendOption::nagleOn`,
|  | size_t | maxAllowedUnsentAsyncMessages = `MAX_UNSENT_ASYNC_MSG_COUNT`
| ) |  |
| inline

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] | messageBuilder | - A const reference to our persistent message builder object of type MsgBldr.
  [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 exectued using this thread pool managed by a single IO context. This is the recommended constructor.

## ◆ TcpTypedServer() [2/2]

template<typename MsgBldr>

core_lib::asio::tcp::TcpTypedServer< MsgBldr >::TcpTypedServer | ( | uint16_t | listenPort,
|  | size_t | minAmountToRead,
|  | const defs::check_bytes_left_to_read_t & | checkBytesLeftToRead,
|  | const defs::message_received_handler_t & | messageReceivedHandler,
|  | const MsgBldr & | messageBuilder,
|  | eSendOption | sendOption = `eSendOption::nagleOn`,
|  | size_t | maxAllowedUnsentAsyncMessages = `MAX_UNSENT_ASYNC_MSG_COUNT`
| ) |  |
| inline

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] | messageBuilder | - A const reference to our persistent message builder object of type MsgBldr.
  [in] | sendOption | - Socket send option to control the use of the Nagle algorithm.
  [in] | maxAllowedUnsentAsyncMessages | - Maximum allowed number of unsent async messages.

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

## ◆ CloseAcceptor()

template<typename MsgBldr>

void core_lib::asio::tcp::TcpTypedServer< MsgBldr >::CloseAcceptor | ( |  | ) |
| inline

Manually close the acceptor.

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

## ◆ GetServerDetailsForClient()

template<typename MsgBldr>

defs::connection_t core_lib::asio::tcp::TcpTypedServer< MsgBldr >::GetServerDetailsForClient | ( | const defs::connection_t & | client | ) | const
| inline

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 xUnknownConnectionError is remoteEnd is not valid.

## ◆ IsConnected()

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::IsConnected | ( | const defs::connection_t & | client | ) | const
| inline

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

template<typename MsgBldr>

uint16_t core_lib::asio::tcp::TcpTypedServer< MsgBldr >::ListenPort | ( |  | ) | const
| inline

Retrieve this server's listen port.

Returns
  The listen port.

## ◆ NumberOfClients()

template<typename MsgBldr>

size_t core_lib::asio::tcp::TcpTypedServer< MsgBldr >::NumberOfClients | ( |  | ) | const
| inline

Retrieve this server's number of clients.

Returns
  The number of clients.

## ◆ NumberOfUnsentAsyncMessages()

template<typename MsgBldr>

size_t core_lib::asio::tcp::TcpTypedServer< MsgBldr >::NumberOfUnsentAsyncMessages | ( | const defs::connection_t & | client | ) | const
| inline

Get number of unsent async messages.

Parameters

  [in] | client | - Target connection details.

Returns
  Number of unsent messages

## ◆ SendMessageToAllClients() [1/4]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToAllClients | ( | int32_t | messageId,
|  | const defs::connection_t & | responseAddress = `defs::NULL_CONNECTION`
| ) |  | const
| inline

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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToAllClients | ( | int32_t | messageId,
|  | const defs::char_buffer_t & | message,
|  | const defs::connection_t & | responseAddress = `defs::NULL_CONNECTION`
| ) |  | const
| inline

Send a header plus message buffer 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() [3/4]

template<typename MsgBldr>
template<typename T , typename A = serialize::archives::out_port_bin_t>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToAllClients | ( | const defs::char_buffer_t & | message | ) | const
| inline

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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToClientAsync | ( | const defs::connection_t & | client,
|  | int32_t | messageId,
|  | const defs::connection_t & | responseAddress = `defs::NULL_CONNECTION`
| ) |  | const
| inline

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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToClientAsync | ( | const defs::connection_t & | client,
|  | const defs::char_buffer_t & | message,
|  | int32_t | messageId,
|  | const defs::connection_t & | responseAddress = `defs::NULL_CONNECTION`
| ) |  | const
| inline

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 MsgBldr>
template<typename T , typename A = serialize::archives::out_port_bin_t>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToClientAsync | ( | const defs::connection_t & | client,
|  | const defs::char_buffer_t & | message
| ) |  | const
| inline

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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToClientSync | ( | const defs::connection_t & | client,
|  | int32_t | messageId,
|  | const defs::connection_t & | responseAddress = `defs::NULL_CONNECTION`
| ) |  | const
| inline

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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToClientSync | ( | const defs::connection_t & | client,
|  | const defs::char_buffer_t & | message,
|  | int32_t | messageId,
|  | const defs::connection_t & | responseAddress = `defs::NULL_CONNECTION`
| ) |  | const
| inline

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 MsgBldr>
template<typename T , typename A = serialize::archives::out_port_bin_t>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::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]

template<typename MsgBldr>

bool core_lib::asio::tcp::TcpTypedServer< MsgBldr >::SendMessageToClientSync | ( | const defs::connection_t & | client,
|  | const defs::char_buffer_t & | message
| ) |  | const
| inline

Send a message buffer to a client synchronously.

Parameters

  [in] | client | - Client connection details.
  [in] | message | - Message buffer.

Returns
  Returns the success state of the send as a boolean.

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

- D:/Projects/CoreLibrary/Include/Asio/TcpTypedServer.h