|
Core Library
1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
|
A class implementing a collection of bi-directional TCP clients. More...
#include <TcpClientList.h>
Public Member Functions | |
| TcpClientList ()=delete | |
| Default constructor - deleted. | |
| TcpClientList (TcpClientList const &)=delete | |
| Copy constructor - deleted. | |
| TcpClientList & | operator= (TcpClientList const &)=delete |
| Copy assignment operator - deleted. | |
| TcpClientList (TcpClientList &&)=delete | |
| Move constructor - deleted. | |
| TcpClientList & | operator= (TcpClientList &&)=delete |
| Move assignment operator - deleted. | |
| TcpClientList (boost_iocontext_t &ioContext, size_t minAmountToRead, defs::check_bytes_left_to_read_t const &checkBytesLeftToRead, defs::message_received_handler_t const &messageReceivedHandler, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT) | |
| Initialisation constructor. More... | |
| TcpClientList (size_t minAmountToRead, defs::check_bytes_left_to_read_t const &checkBytesLeftToRead, defs::message_received_handler_t const &messageReceivedHandler, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT) | |
| Initialisation constructor. More... | |
| ~TcpClientList () | |
| 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 TCP clients and clear map. | |
| bool | SendMessageToServerAsync (defs::connection_t const &server, defs::char_buffer_t const &message) |
| Send a message buffer to the server asynchronously. More... | |
| bool | SendMessageToServerSync (defs::connection_t const &server, defs::char_buffer_t const &message) |
| Send a message buffer to the server synchronously. More... | |
| void | ClearList () |
| Clear all TCP clients from list. | |
| std::vector< defs::connection_t > | GetServerList () 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< TcpClient > |
| 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_t * | m_ioContextPtr {nullptr} |
| External boost IO context to manage ASIO. | |
| size_t | m_minAmountToRead {0} |
| Minimum amount of data to read on each receive, typical size of header block. | |
| defs::check_bytes_left_to_read_t | m_checkBytesLeftToRead {} |
| Function object capable of decoding the message and computing how many bytes are left until a complete message. | |
| defs::message_received_handler_t | m_messageReceivedHandler {} |
| 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. | |
| client_map_t | m_clientMap {} |
| Map of TCP clients. | |
A class implementing a collection of bi-directional TCP clients.
| core_lib::asio::tcp::TcpClientList::TcpClientList | ( | boost_iocontext_t & | ioContext, |
| size_t | minAmountToRead, | ||
| defs::check_bytes_left_to_read_t const & | checkBytesLeftToRead, | ||
| defs::message_received_handler_t const & | 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] | 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::TcpClientList::TcpClientList | ( | size_t | minAmountToRead, |
| defs::check_bytes_left_to_read_t const & | checkBytesLeftToRead, | ||
| defs::message_received_handler_t const & | messageReceivedHandler, | ||
| eSendOption | sendOption = eSendOption::nagleOn, |
||
| size_t | maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT |
||
| ) |
Initialisation constructor.
| [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 recommended.
| void core_lib::asio::tcp::TcpClientList::CloseConnection | ( | defs::connection_t const & | server | ) | const |
Manually close the client's connection.
| [in] | server | - Connection object describing server's address and port. |
Note that this object uses RAII so will close all connections when destroyed.
| void core_lib::asio::tcp::TcpClientList::CloseConnections | ( | ) | const |
Manually close the all client connections.
Note that this object uses RAII so will close all connections when destroyed.
| bool core_lib::asio::tcp::TcpClientList::Connected | ( | defs::connection_t const & | server | ) | const |
Check if the client is connected to the server.
| [in] | server | - Connection object describing server's address and port. |
|
private |
Create the client connection for the server.
| [in] | server | - Connection object describing server's address and port. |
|
private |
Find the client connection for the server.
| [in] | server | - Connection object describing server's address and port. |
| auto core_lib::asio::tcp::TcpClientList::GetClientDetailsForServer | ( | defs::connection_t const & | server | ) | const |
Retrieve this client's connection details.
| [in] | server | - Connection object describing server's address and port. |
Throws xUnknownConnectionError is remoteEnd is not valid.
| auto core_lib::asio::tcp::TcpClientList::GetServerList | ( | ) | const |
Get list of connections.
| size_t core_lib::asio::tcp::TcpClientList::NumberOfUnsentAsyncMessages | ( | const defs::connection_t & | server | ) | const |
Get number of unsent async messages.
| [in] | server | - Target connection details. |
| bool core_lib::asio::tcp::TcpClientList::SendMessageToServerAsync | ( | defs::connection_t const & | server, |
| defs::char_buffer_t const & | message | ||
| ) |
Send a message buffer to the server asynchronously.
| [in] | server | - Connection object describing server's address and port. |
| [in] | message | - Message buffer. |
| bool core_lib::asio::tcp::TcpClientList::SendMessageToServerSync | ( | defs::connection_t const & | server, |
| defs::char_buffer_t const & | message | ||
| ) |
Send a message buffer to the server synchronously.
| [in] | server | - Connection object describing server's address and port. |
| [in] | message | - Message buffer. |
| auto core_lib::asio::tcp::TcpClientList::ServerConnection | ( | defs::connection_t const & | clientConn | ) | const |
Retrieve server connection details for a client.
| [in] | clientConn | - Connection object describing server's address and port. |