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

A bi-directional TCP client. More...

#include <TcpClient.h>

Public Member Functions

 TcpClient ()=delete
 Default constructor - deleted.
 
 TcpClient (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, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT)
 Initialisation constructor. More...
 
 TcpClient (const defs::connection_t &server, 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...
 
 ~TcpClient ()
 Default destructor.
 
 TcpClient (const TcpClient &)=delete
 Copy constructor - deleted.
 
TcpClientoperator= (const TcpClient &)=delete
 Copy assignment operator - deleted.
 
 TcpClient (TcpClient &&)=delete
 Move constructor - deleted.
 
TcpClientoperator= (TcpClient &&)=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 () const
 Retrieve this client's connection details. More...
 
bool CheckAndCreateConnection ()
 Check connection and create if required. More...
 
void CloseConnection ()
 Manually close the connection. 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 Member Functions

void CreateConnection ()
 Create connection to server.
 

Private Attributes

std::unique_ptr< IoContextThreadGroupm_ioThreadGroup {}
 I/O context thread group.
 
boost_iocontext_tm_ioContext
 I/O context reference.
 
defs::connection_t m_server {}
 Server connection details.
 
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 send option.
 
size_t m_maxAllowedUnsentAsyncMessages {MAX_UNSENT_ASYNC_MSG_COUNT}
 Max allowed unsent async message counter.
 
TcpConnections m_serverConnection {}
 TCP connections object.
 

Detailed Description

A bi-directional TCP client.

This class forms the underpinnings of the TcpTypedClient class.

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

Constructor & Destructor Documentation

◆ TcpClient() [1/2]

core_lib::asio::tcp::TcpClient::TcpClient ( 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,
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]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]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.

◆ TcpClient() [2/2]

core_lib::asio::tcp::TcpClient::TcpClient ( const defs::connection_t server,
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]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]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 recommend.

Member Function Documentation

◆ CheckAndCreateConnection()

bool core_lib::asio::tcp::TcpClient::CheckAndCreateConnection ( )

Check connection and create if required.

Returns
True if connection exists, false otherwise.

◆ CloseConnection()

void core_lib::asio::tcp::TcpClient::CloseConnection ( )

Manually close the connection.

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

◆ Connected()

bool core_lib::asio::tcp::TcpClient::Connected ( ) const

Check if the client is connected to the server.

Returns
True if connected, false otherwise.

◆ GetClientDetailsForServer()

auto core_lib::asio::tcp::TcpClient::GetClientDetailsForServer ( ) const

Retrieve this client's connection details.

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

Throws xUnknownConnectionError is remoteEnd is not valid.

◆ NumberOfUnsentAsyncMessages()

size_t core_lib::asio::tcp::TcpClient::NumberOfUnsentAsyncMessages ( ) const

Get number of unsent async messages.

Returns
Number of pending queued async messages

◆ SendMessageToServerAsync()

bool core_lib::asio::tcp::TcpClient::SendMessageToServerAsync ( const defs::char_buffer_t message)

Send a message buffer to the server asynchronously.

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

◆ SendMessageToServerSync()

bool core_lib::asio::tcp::TcpClient::SendMessageToServerSync ( const defs::char_buffer_t message)

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

auto core_lib::asio::tcp::TcpClient::ServerConnection ( ) const

Retrieve server connection details.

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

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