|
Core Library
1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
|
A simple bi-directional multi-client TCP server, which uses the class MessageHeader as the message header type. More...
#include <SimpleTcpServer.h>
Public Member Functions | |
| SimpleTcpServer ()=delete | |
| Default constructor - deleted. | |
| SimpleTcpServer (boost_iocontext_t &ioContext, uint16_t listenPort, const defs::default_message_dispatcher_t &messageDispatcher, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT, size_t memPoolMsgCount=0) | |
| Initialisation constructor. More... | |
| SimpleTcpServer (uint16_t listenPort, const defs::default_message_dispatcher_t &messageDispatcher, eSendOption sendOption=eSendOption::nagleOn, size_t maxAllowedUnsentAsyncMessages=MAX_UNSENT_ASYNC_MSG_COUNT, size_t memPoolMsgCount=0) | |
| Initialisation constructor. More... | |
| ~SimpleTcpServer ()=default | |
| Default destructor. | |
| SimpleTcpServer (const SimpleTcpServer &)=delete | |
| Copy constructor - deleted. | |
| SimpleTcpServer & | operator= (const SimpleTcpServer &)=delete |
| Copy assignment operator - deleted. | |
| SimpleTcpServer (SimpleTcpServer &&)=delete | |
| Move constructor - deleted. | |
| SimpleTcpServer & | operator= (SimpleTcpServer &&)=delete |
| Move assignment operator - deleted. | |
| defs::connection_t | GetServerDetailsForClient (const defs::connection_t &client) const |
| Retrieve this server's connection details for a given client. More... | |
| uint16_t | ListenPort () const |
| Retrieve this server's listen port. More... | |
| size_t | NumberOfClients () const |
| Retrieve this server's number of clients. More... | |
| void | CloseAcceptor () |
| Manually close the acceptor. More... | |
| void | OpenAcceptor () |
| Manually open the acceptor. | |
| bool | SendMessageToClientAsync (const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a header-only message to a client asynchronously. More... | |
| bool | SendMessageToClientSync (const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a header-only message to a client synchronously. More... | |
| bool | SendMessageToAllClients (int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a header-only message to all clients asynchronously. More... | |
| bool | SendMessageToClientAsync (const defs::connection_t &client, const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a header plus message buffer to a client asynchronously. More... | |
| bool | SendMessageToClientSync (const defs::connection_t &client, const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a header plus message buffer to a client synchronously. More... | |
| bool | SendMessageToAllClients (const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a header plus message buffer to all clients asynchronously. More... | |
| template<typename T , typename A = serialize::archives::out_port_bin_t> | |
| bool | SendMessageToClientAsync (const T &message, const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a full message to a client asynchronously. More... | |
| template<typename T , typename A = serialize::archives::out_port_bin_t> | |
| bool | SendMessageToClientSync (const T &message, const defs::connection_t &client, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a full message to a client synchronously. More... | |
| template<typename T , typename A = serialize::archives::out_port_bin_t> | |
| bool | SendMessageToAllClients (const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const |
| Send a full message to all clients asynchronously. More... | |
| bool | SendMessageToClientAsync (const defs::connection_t &client, const defs::char_buffer_t &message) const |
| Send a message buffer to a client asynchronously. More... | |
| bool | SendMessageToClientSync (const defs::connection_t &client, const defs::char_buffer_t &message) const |
| Send a message buffer to a client synchronously. More... | |
| bool | SendMessageToAllClients (const defs::char_buffer_t &message) const |
| Send a message buffer to all clients asynchronously. More... | |
| size_t | NumberOfUnsentAsyncMessages (const defs::connection_t &client) const |
| Get number of unsent async messages. More... | |
| bool | IsConnected (const defs::connection_t &client) const |
| Tells if a given client is currently connected to the server. More... | |
Private Attributes | |
| messages::MessageBuilder | m_messageBuilder {} |
| Default message builder object of type core_lib::asio::messages::MessageBuilder. | |
| messages::MessageHandler | m_messageHandler {} |
| Default message handler object of type core_lib::asio::messages::MessageHandler. | |
| TcpTypedServer< messages::MessageBuilder > | m_tcpTypedServer |
| Our actual typed TCP server object. | |
A simple bi-directional multi-client TCP server, which uses the class MessageHeader as the message header type.
| core_lib::asio::tcp::SimpleTcpServer::SimpleTcpServer | ( | boost_iocontext_t & | ioContext, |
| uint16_t | listenPort, | ||
| const defs::default_message_dispatcher_t & | messageDispatcher, | ||
| eSendOption | sendOption = eSendOption::nagleOn, |
||
| size_t | maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT, |
||
| size_t | memPoolMsgCount = 0 |
||
| ) |
Initialisation constructor.
| [in] | ioContext | - External boost IO context to manage ASIO. |
| [in] | listenPort | - Our listen port for all detected networks. |
| [in] | messageDispatcher | - Callback to use to dispatch received messages. |
| [in] | sendOption | - Socket send option to control the use of the Nagle algorithm. |
| [in] | maxAllowedUnsentAsyncMessages | - Maximum allowed number of unsent async messages. |
| [in] | memPoolMsgCount | - Number of messages 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.
| core_lib::asio::tcp::SimpleTcpServer::SimpleTcpServer | ( | uint16_t | listenPort, |
| const defs::default_message_dispatcher_t & | messageDispatcher, | ||
| eSendOption | sendOption = eSendOption::nagleOn, |
||
| size_t | maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT, |
||
| size_t | memPoolMsgCount = 0 |
||
| ) |
Initialisation constructor.
| [in] | listenPort | - Our listen port for all detected networks. |
| [in] | messageDispatcher | - Callback to use to dispatch received messages. |
| [in] | sendOption | - Socket send option to control the use of the Nagle algorithm. |
| [in] | maxAllowedUnsentAsyncMessages | - Maximum allowed number of unsent async messages. |
| [in] | memPoolMsgCount | - Number of messages 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.
| void core_lib::asio::tcp::SimpleTcpServer::CloseAcceptor | ( | ) |
Manually close the acceptor.
Note that this object is RAII so will automatically close the acceptor in its destructor.
| auto core_lib::asio::tcp::SimpleTcpServer::GetServerDetailsForClient | ( | const defs::connection_t & | client | ) | const |
Retrieve this server's connection details for a given client.
| [in] | client | - A client's connection details. |
If no such client is known to the server then it returns { "0.0.0.0", listenPort}.
| bool core_lib::asio::tcp::SimpleTcpServer::IsConnected | ( | const defs::connection_t & | client | ) | const |
Tells if a given client is currently connected to the server.
| [in] | target | - Target connection details. |
| uint16_t core_lib::asio::tcp::SimpleTcpServer::ListenPort | ( | ) | const |
Retrieve this server's listen port.
| size_t core_lib::asio::tcp::SimpleTcpServer::NumberOfClients | ( | ) | const |
Retrieve this server's number of clients.
| size_t core_lib::asio::tcp::SimpleTcpServer::NumberOfUnsentAsyncMessages | ( | const defs::connection_t & | client | ) | const |
Get number of unsent async messages.
| [in] | client | - Target connection details. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients | ( | int32_t | messageId, |
| const defs::connection_t & | responseAddress = defs::NULL_CONNECTION |
||
| ) | const |
Send a header-only message to all clients asynchronously.
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where a client should send a response, the default value will mean the response address will point to this server socket. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients | ( | const defs::char_buffer_t & | message, |
| int32_t | messageId, | ||
| const defs::connection_t & | responseAddress = defs::NULL_CONNECTION |
||
| ) | const |
Send a header plus message buffer to all clients asynchronously.
| [in] | message | - Message buffer. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where a client should send a response, the default value will mean the response address will point to this server socket. |
|
inline |
Send a full message to all clients asynchronously.
| [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 clients should send a response, the default value will mean the response address will point to this server socket. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients | ( | const defs::char_buffer_t & | message | ) | const |
Send a message buffer to all clients asynchronously.
| [in] | message | - Message buffer. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync | ( | const defs::connection_t & | client, |
| int32_t | messageId, | ||
| const defs::connection_t & | responseAddress = defs::NULL_CONNECTION |
||
| ) | const |
Send a header-only message to a client asynchronously.
| [in] | client | - Client connection details. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync | ( | const defs::connection_t & | client, |
| const defs::char_buffer_t & | message, | ||
| int32_t | messageId, | ||
| const defs::connection_t & | responseAddress = defs::NULL_CONNECTION |
||
| ) | const |
Send a header plus message buffer to a client asynchronously.
| [in] | client | - Client connection details. |
| [in] | message | - Message buffer. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket. |
|
inline |
Send a full message to a client asynchronously.
| [in] | message | - The message of type T to send behind the header serialized to an boost::serialization-compatible archive of type A. |
| [in] | client | - Client connection details. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync | ( | const defs::connection_t & | client, |
| const defs::char_buffer_t & | message | ||
| ) | const |
Send a message buffer to a client asynchronously.
| [in] | client | - Client connection details. |
| [in] | message | - Message buffer. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync | ( | const defs::connection_t & | client, |
| int32_t | messageId, | ||
| const defs::connection_t & | responseAddress = defs::NULL_CONNECTION |
||
| ) | const |
Send a header-only message to a client synchronously.
| [in] | client | - Client connection details. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync | ( | const defs::connection_t & | client, |
| const defs::char_buffer_t & | message, | ||
| int32_t | messageId, | ||
| const defs::connection_t & | responseAddress = defs::NULL_CONNECTION |
||
| ) | const |
Send a header plus message buffer to a client synchronously.
| [in] | client | - Client connection details. |
| [in] | message | - Message buffer. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket. |
|
inline |
Send a full message to a client synchronously.
| [in] | message | - The message of type T to send behind the header serialized to an boost::serialization-compatible archive of type A. |
| [in] | client | - Client connection details. |
| [in] | messageId | - Unique message ID to insert into message header. |
| [in] | responseAddress | - (Optional) The address and port where the client should send a response, the default value will mean the response address will point to this server socket. |
| bool core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync | ( | const defs::connection_t & | client, |
| const defs::char_buffer_t & | message | ||
| ) | const |
Send a message buffer to a client synchronously.
| [in] | client | - Client connection details. |
| [in] | message | - Message buffer. |