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

A class implementing a collection of bi-directional simple TCP clients. More...

#include <SimpleTcpClientList.h>

Public Member Functions

 SimpleTcpClientList ()=delete
 Default constructor - deleted.
 
 SimpleTcpClientList (SimpleTcpClientList const &)=delete
 Copy constructor - deleted.
 
SimpleTcpClientListoperator= (SimpleTcpClientList const &)=delete
 Copy assignment operator - deleted.
 
 SimpleTcpClientList (SimpleTcpClientList &&)=delete
 Move constructor - deleted.
 
SimpleTcpClientListoperator= (SimpleTcpClientList &&)=delete
 Move assignment operator - deleted.
 
 SimpleTcpClientList (boost_iocontext_t &ioContext, defs::default_message_dispatcher_t const &messageDispatcher, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT, size_t memPoolMsgCount=0)
 Initialisation constructor. More...
 
 SimpleTcpClientList (defs::default_message_dispatcher_t const &messageDispatcher, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT, size_t memPoolMsgCount=0)
 Initialisation constructor. More...
 
 ~SimpleTcpClientList ()
 Default destructor.
 
defs::connection_t ServerConnection (defs::connection_t const &clientConn) const
 Retrieve server connection details for a client. More...
 
bool Connected (defs::connection_t const &server) const
 Check if the client is connected to the server. More...
 
defs::connection_t GetClientDetailsForServer (defs::connection_t const &server) const
 Retrieve this client's connection details. More...
 
void CloseConnection (defs::connection_t const &server) const
 Manually close the client's connection. More...
 
void CloseConnections () const
 Manually close the all client connections. More...
 
void ClearConnections ()
 Destroy all simple TCP clients and clear map.
 
bool SendMessageToServerAsync (defs::connection_t const &server, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
 Send a header-only message to the server asynchronously. More...
 
bool SendMessageToServerSync (defs::connection_t const &server, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
 Send a header-only message to the server synchronously. More...
 
bool SendMessageToServerAsync (defs::connection_t const &server, const defs::char_buffer_t &message, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
 Send a header plus message buffer to the server asynchronously. More...
 
bool SendMessageToServerSync (defs::connection_t const &server, const defs::char_buffer_t &message, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
 Send a header plus message buffer to the server synchronously. More...
 
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool SendMessageToServerAsync (defs::connection_t const &server, T const &message, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
 Send a full message to the server asynchronously. More...
 
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool SendMessageToServerSync (defs::connection_t const &server, T const &message, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
 Send a full message to the server synchronously. More...
 
bool SendMessageToServerAsync (defs::connection_t const &server, const defs::char_buffer_t &message)
 Send a message buffer to the server asynchronously. More...
 
bool SendMessageToServerSync (defs::connection_t const &server, const defs::char_buffer_t &message)
 Send a message buffer to the server synchronously. More...
 
void ClearList ()
 Clear all TCP clients from list.
 
std::vector< defs::connection_tGetServerList () const
 Get list of connections. More...
 
size_t NumberOfUnsentAsyncMessages (const defs::connection_t &server) const
 Get number of unsent async messages. More...
 

Private Types

using client_ptr_t = std::shared_ptr< SimpleTcpClient >
 
using client_map_t = std::map< defs::connection_t, client_ptr_t >
 

Private Member Functions

client_ptr_t CreateTcpClient (defs::connection_t const &server)
 Create the client connection for the server. More...
 
client_ptr_t FindTcpClient (defs::connection_t const &server) const
 Find the client connection for the server. More...
 

Private Attributes

std::mutex m_mutex
 Mutex to make access to map thread safe.
 
boost_iocontext_tm_ioContextPtr {nullptr}
 External boost IO context to manage ASIO.
 
defs::default_message_dispatcher_t m_messageDispatcher {}
 Function object cpable of handling a received message and disptaching it accordingly.
 
eSendOption m_sendOption {eSendOption::nagleOn}
 Socket send option to control the use of the Nagle algorithm.
 
size_t m_maxAllowedUnsentAsyncMessages {MAX_UNSENT_ASYNC_MSG_COUNT}
 Max allowed unsent async message counter per client.
 
size_t m_memPoolMsgCount {0}
 Number of messages (per client) in pool for received message.
 
client_map_t m_clientMap {}
 Map of simple TCP clients.
 

Detailed Description

A class implementing a collection of bi-directional simple TCP clients.

Constructor & Destructor Documentation

◆ SimpleTcpClientList() [1/2]

core_lib::asio::tcp::SimpleTcpClientList::SimpleTcpClientList ( boost_iocontext_t ioContext,
defs::default_message_dispatcher_t const &  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]messageDispatcher- 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 per client.
[in]memPoolMsgCount- Number of messages (per client) 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.

◆ SimpleTcpClientList() [2/2]

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

Initialisation constructor.

Parameters
[in]messageDispatcher- 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 per client.
[in]memPoolMsgCount- Number of messages (per client) 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

◆ CloseConnection()

void core_lib::asio::tcp::SimpleTcpClientList::CloseConnection ( defs::connection_t const &  server) const

Manually close the client's connection.

Parameters
[in]server- Connection object describing server's address and port.

Note that this object uses RAII so will close all connections when destroyed.

◆ CloseConnections()

void core_lib::asio::tcp::SimpleTcpClientList::CloseConnections ( ) const

Manually close the all client connections.

Note that this object uses RAII so will close all connections when destroyed.

◆ Connected()

bool core_lib::asio::tcp::SimpleTcpClientList::Connected ( defs::connection_t const &  server) const

Check if the client is connected to the server.

Parameters
[in]server- Connection object describing server's address and port.
Returns
True if connected, false otherwise.

◆ CreateTcpClient()

auto core_lib::asio::tcp::SimpleTcpClientList::CreateTcpClient ( defs::connection_t const &  server)
private

Create the client connection for the server.

Parameters
[in]server- Connection object describing server's address and port.
Returns
A std::shared_ptr to a TcpClient or a null pointer of not found.

◆ FindTcpClient()

auto core_lib::asio::tcp::SimpleTcpClientList::FindTcpClient ( defs::connection_t const &  server) const
private

Find the client connection for the server.

Parameters
[in]server- Connection object describing server's address and port.
Returns
A std::shared_ptr to a TcpClient or a null pointer of not found.

◆ GetClientDetailsForServer()

auto core_lib::asio::tcp::SimpleTcpClientList::GetClientDetailsForServer ( defs::connection_t const &  server) const

Retrieve this client's connection details.

Parameters
[in]server- Connection object describing server's address and port.
Returns
Connection object describing this client's address and port.

Throws xUnknownConnectionError is remoteEnd is not valid.

◆ GetServerList()

auto core_lib::asio::tcp::SimpleTcpClientList::GetServerList ( ) const

Get list of connections.

Returns
- list of server connection details.

◆ NumberOfUnsentAsyncMessages()

size_t core_lib::asio::tcp::SimpleTcpClientList::NumberOfUnsentAsyncMessages ( const defs::connection_t server) const

Get number of unsent async messages.

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

◆ SendMessageToServerAsync() [1/4]

bool core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerAsync ( defs::connection_t const &  server,
int32_t  messageId,
defs::connection_t const &  responseAddress = defs::NULL_CONNECTION 
)

Send a header-only message to the server asynchronously.

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

◆ SendMessageToServerAsync() [2/4]

bool core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerAsync ( defs::connection_t const &  server,
const defs::char_buffer_t message,
int32_t  messageId,
defs::connection_t const &  responseAddress = defs::NULL_CONNECTION 
)

Send a header plus message buffer to the server asynchronously.

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

◆ SendMessageToServerAsync() [3/4]

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

Send a full message to the server asynchronously.

Parameters
[in]server- Connection object describing server's address and port.
[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 server should send the response, the default value will mean the response address will point to this client socket.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToServerAsync() [4/4]

bool core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerAsync ( defs::connection_t const &  server,
const defs::char_buffer_t message 
)

Send a message buffer to the server asynchronously.

Parameters
[in]server- Connection object describing server's address and port.
[in]message- Message buffer.
Returns
Returns the success state of whether the message was posted to the send queue.

◆ SendMessageToServerSync() [1/4]

bool core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerSync ( defs::connection_t const &  server,
int32_t  messageId,
defs::connection_t const &  responseAddress = defs::NULL_CONNECTION 
)

Send a header-only message to the server synchronously.

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

This method only sends a simple core_lib::asio::defs::MessageHeader object to the server.

◆ SendMessageToServerSync() [2/4]

bool core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerSync ( defs::connection_t const &  server,
const defs::char_buffer_t message,
int32_t  messageId,
defs::connection_t const &  responseAddress = defs::NULL_CONNECTION 
)

Send a header plus message buffer to the server synchronously.

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

◆ SendMessageToServerSync() [3/4]

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

Send a full message to the server synchronously.

Parameters
[in]server- Connection object describing server's address and port.
[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 server should send the response, the default value will mean the response address will point to this client socket.
Returns
Returns the success state of the send as a boolean.

◆ SendMessageToServerSync() [4/4]

bool core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerSync ( defs::connection_t const &  server,
const defs::char_buffer_t message 
)

Send a message buffer to the server synchronously.

Parameters
[in]server- Connection object describing server's address and port.
[in]message- Message buffer.
Returns
Returns the success state of the send as a boolean.

◆ ServerConnection()

auto core_lib::asio::tcp::SimpleTcpClientList::ServerConnection ( defs::connection_t const &  clientConn) const

Retrieve server connection details for a client.

Parameters
[in]clientConn- Connection object describing server's address and port.
Returns
- Connection object describing target server's address and port.

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