|
Core Library
1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
|
A generic bi-directional TCP server. More...
#include <TcpTypedServer.h>
Public Member Functions | |
| TcpTypedServer ()=delete | |
| Default constructor - deleted. | |
| TcpTypedServer (boost_iocontext_t &ioContext, uint16_t listenPort, 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... | |
| TcpTypedServer (uint16_t listenPort, 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... | |
| ~TcpTypedServer ()=default | |
| Default destructor. | |
| TcpTypedServer (const TcpTypedServer &)=delete | |
| Copy constructor - deleted. | |
| TcpTypedServer & | operator= (const TcpTypedServer &)=delete |
| Copy assignment operator - deleted. | |
| TcpTypedServer (TcpTypedServer &&)=delete | |
| Move constructor - deleted. | |
| TcpTypedServer & | operator= (TcpTypedServer &&)=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 (int32_t messageId, const defs::char_buffer_t &message, 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 | |
| std::mutex | m_sendMutex |
| Send message mutex. | |
| const MsgBldr & | m_messageBuilder |
| Referece to our message builder object. | |
| TcpServer | m_tcpServer |
| General purpose TCP server object. | |
A generic bi-directional TCP server.
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 SimpleTcpServer class.
This is also the class to be used when the user wants to specify their own message builder type and message header type.
|
inline |
Initialisation constructor.
| [in] | ioContext | - External boost IO context to manage ASIO. |
| [in] | listenPort | - Our listen port for all detected networks. |
| [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 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 exectued using this thread pool managed by a single IO context. This is the recommended constructor.
|
inline |
Initialisation constructor.
| [in] | listenPort | - Our listen port for all detected networks. |
| [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 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. |
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.
|
inline |
Manually close the acceptor.
Note that this object is RAII so will automatically close the acceptor in its destructor.
|
inline |
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}.
Throws xUnknownConnectionError is remoteEnd is not valid.
|
inline |
Tells if a given client is currently connected to the server.
| [in] | target | - Target connection details. |
|
inline |
Retrieve this server's listen port.
|
inline |
Retrieve this server's number of clients.
|
inline |
Get number of unsent async messages.
| [in] | client | - Target connection details. |
|
inline |
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. |
|
inline |
Send a header plus message buffer 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. |
|
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. |
|
inline |
Send a message buffer to all clients asynchronously.
| [in] | message | - Message buffer. |
|
inline |
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. |
|
inline |
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. |
|
inline |
Send a message buffer to a client asynchronously.
| [in] | client | - Client connection details. |
| [in] | message | - Message buffer. |
|
inline |
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. |
|
inline |
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. |
|
inline |
Send a message buffer to a client synchronously.
| [in] | client | - Client connection details. |
| [in] | message | - Message buffer. |