|
Core Library
1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
|
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. | |
| TcpClient & | operator= (const TcpClient &)=delete |
| Copy assignment operator - deleted. | |
| TcpClient (TcpClient &&)=delete | |
| Move constructor - deleted. | |
| TcpClient & | operator= (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< IoContextThreadGroup > | m_ioThreadGroup {} |
| I/O context thread group. | |
| boost_iocontext_t & | m_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. | |
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.
| 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.
| [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.
| 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.
| [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.
| bool core_lib::asio::tcp::TcpClient::CheckAndCreateConnection | ( | ) |
Check connection and create if required.
| void core_lib::asio::tcp::TcpClient::CloseConnection | ( | ) |
Manually close the connection.
Note that this object uses RAII so will close the connection when destroyed.
| bool core_lib::asio::tcp::TcpClient::Connected | ( | ) | const |
Check if the client is connected to the server.
| auto core_lib::asio::tcp::TcpClient::GetClientDetailsForServer | ( | ) | const |
Retrieve this client's connection details.
Throws xUnknownConnectionError is remoteEnd is not valid.
| size_t core_lib::asio::tcp::TcpClient::NumberOfUnsentAsyncMessages | ( | ) | const |
Get number of unsent async messages.
| bool core_lib::asio::tcp::TcpClient::SendMessageToServerAsync | ( | const defs::char_buffer_t & | message | ) |
Send a message buffer to the server asynchronously.
| [in] | message | - Message buffer. |
| bool core_lib::asio::tcp::TcpClient::SendMessageToServerSync | ( | const defs::char_buffer_t & | message | ) |
Send a message buffer to the server synchronously.
| [in] | message | - Message buffer. |
| auto core_lib::asio::tcp::TcpClient::ServerConnection | ( | ) | const |
Retrieve server connection details.