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::TcpTypedClient< MsgBldr > Class Template Referencefinal

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

#include <TcpTypedClient.h>

Public Member Functions

 TcpTypedClient ()=delete
 Default constructor - deleted.
 
 TcpTypedClient (boost_iocontext_t &ioContext, const defs::connection_t &server, 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...
 
 TcpTypedClient (const defs::connection_t &server, 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...
 
 ~TcpTypedClient ()=default
 Default destructor.
 
 TcpTypedClient (const TcpTypedClient &)=delete
 Copy constructor - deleted.
 
TcpTypedClientoperator= (const TcpTypedClient &)=delete
 Copy assignment operator - deleted.
 
 TcpTypedClient (TcpTypedClient &&)=delete
 Move constructor - deleted.
 
TcpTypedClientoperator= (TcpTypedClient &&)=delete
 Move assignment operator - deleted.
 
defs::connection_t ServerConnection () const
 Retrieve server connection details. More...
 
bool Connected () const
 Check if the client is connected to the server. More...
 
defs::connection_t GetClientDetailsForServer ()
 Retrieve this client's connection details. More...
 
void CloseConnection ()
 Manually close the connection. More...
 
bool SendMessageToServerAsync (int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
 Send a header-only message to the server asynchronously. More...
 
bool SendMessageToServerSync (int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
 Send a header-only message to the server synchronously. More...
 
bool SendMessageToServerAsync (const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
 Send a header plus message buffer to the server asynchronously. More...
 
bool SendMessageToServerSync (const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &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 (const T &message, int32_t messageId, const defs::connection_t &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 (const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
 Send a full message to the server synchronously. More...
 
bool SendMessageToServerAsync (const defs::char_buffer_t &message)
 Send a message buffer to the server asynchronously. More...
 
bool SendMessageToServerSync (const defs::char_buffer_t &message)
 Send a message buffer to the server synchronously. More...
 
size_t NumberOfUnsentAsyncMessages () const
 Get number of unsent async messages. More...
 

Private Attributes

std::mutex m_sendMutex
 Send message mutex.
 
const MsgBldr & m_messageBuilder
 Referece to our message builder object.
 
TcpClient m_tcpClient
 General purpose TCP client object.
 

Detailed Description

template<typename MsgBldr>
class core_lib::asio::tcp::TcpTypedClient< MsgBldr >

A generic bi-directional TCP client.

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 SimpleTcpClient 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

◆ TcpTypedClient() [1/2]

template<typename MsgBldr>
core_lib::asio::tcp::TcpTypedClient< MsgBldr >::TcpTypedClient ( boost_iocontext_t ioContext,
const defs::connection_t server,
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]server- Connection object describing target server's address and port.
[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 cpable 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 executed using this thread pool managed by a single IO context. This is the recommended constructor.

◆ TcpTypedClient() [2/2]

template<typename MsgBldr>
core_lib::asio::tcp::TcpTypedClient< MsgBldr >::TcpTypedClient ( const defs::connection_t server,
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]server- Connection object describing target server's address and port.
[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 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

◆ CloseConnection()

template<typename MsgBldr>
void core_lib::asio::tcp::TcpTypedClient< MsgBldr >::CloseConnection ( )
inline

Manually close the connection.

Note that this object uses RAII so will close the connection when destroyed.

◆ Connected()

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::Connected ( ) const
inline

Check if the client is connected to the server.

Returns
True if connected, false otherwise.

◆ GetClientDetailsForServer()

template<typename MsgBldr>
defs::connection_t core_lib::asio::tcp::TcpTypedClient< MsgBldr >::GetClientDetailsForServer ( )
inline

Retrieve this client's connection details.

Returns
Connection object describing this client's address and port.

Throws xUnknownConnectionError is remoteEnd is not valid.

◆ NumberOfUnsentAsyncMessages()

template<typename MsgBldr>
size_t core_lib::asio::tcp::TcpTypedClient< MsgBldr >::NumberOfUnsentAsyncMessages ( ) const
inline

Get number of unsent async messages.

Returns
Number of pending queued async messages

◆ SendMessageToServerAsync() [1/4]

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerAsync ( int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
)
inline

Send a header-only message to the server asynchronously.

Parameters
[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]

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerAsync ( const defs::char_buffer_t message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
)
inline

Send a header plus message buffer to the server asynchronously.

Parameters
[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 MsgBldr>
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerAsync ( const T &  message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
)
inline

Send a full message to the server 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 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]

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerAsync ( const defs::char_buffer_t message)
inline

Send a message buffer to the server asynchronously.

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

◆ SendMessageToServerSync() [1/4]

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerSync ( int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
)
inline

Send a header-only message to the server synchronously.

Parameters
[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() [2/4]

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerSync ( const defs::char_buffer_t message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
)
inline

Send a header plus message buffer to the server synchronously.

Parameters
[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 MsgBldr>
template<typename T , typename A = serialize::archives::out_port_bin_t>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerSync ( const T &  message,
int32_t  messageId,
const defs::connection_t responseAddress = defs::NULL_CONNECTION 
)
inline

Send a full message to the server 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]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]

template<typename MsgBldr>
bool core_lib::asio::tcp::TcpTypedClient< MsgBldr >::SendMessageToServerSync ( const defs::char_buffer_t message)
inline

Send a message buffer to the server synchronously.

Parameters
[in]message- Message buffer.
Returns
Returns the success state of the send as a boolean.

◆ ServerConnection()

template<typename MsgBldr>
defs::connection_t core_lib::asio::tcp::TcpTypedClient< MsgBldr >::ServerConnection ( ) const
inline

Retrieve server connection details.

Returns
- Connection object describing target server's address and port.

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