Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

SimpleTcpServer.h
Go to the documentation of this file.
1
2 // This file is part of CoreLibrary containing useful reusable utility
3 // classes.
4 //
5 // Copyright (C) 2014 to present, Duncan Crutchley
6 // Contact <dac1976github@outlook.com>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published
10 // by the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License and GNU Lesser General Public License
17 // for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // and GNU Lesser General Public License along with this program. If
21 // not, see <http://www.gnu.org/licenses/>.
22
28 #ifndef SIMPLETCPSERVER
29 #define SIMPLETCPSERVER
30
31 #include "TcpTypedServer.h"
32
34 namespace core_lib
35 {
37 namespace asio
38 {
40 namespace tcp
41 {
44 class CORE_LIBRARY_DLL_SHARED_API SimpleTcpServer final
45 {
46 public:
48 SimpleTcpServer() = delete;
70 SimpleTcpServer(boost_iocontext_t& ioContext, uint16_t listenPort,
71 const defs::default_message_dispatcher_t& messageDispatcher,
72 eSendOption sendOption = eSendOption::nagleOn,
73 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
74 size_t memPoolMsgCount = 0);
95 SimpleTcpServer(uint16_t listenPort,
96 const defs::default_message_dispatcher_t& messageDispatcher,
97 eSendOption sendOption = eSendOption::nagleOn,
98 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
99 size_t memPoolMsgCount = 0);
101 ~SimpleTcpServer() = default;
103 SimpleTcpServer(const SimpleTcpServer&) = delete;
105 SimpleTcpServer& operator=(const SimpleTcpServer&) = delete;
107 SimpleTcpServer(SimpleTcpServer&&) = delete;
109 SimpleTcpServer& operator=(SimpleTcpServer&&) = delete;
117 defs::connection_t GetServerDetailsForClient(const defs::connection_t& client) const;
122 uint16_t ListenPort() const;
127 size_t NumberOfClients() const;
132 void CloseAcceptor();
134 void OpenAcceptor();
143 bool SendMessageToClientAsync(
144 const defs::connection_t& client, int32_t messageId,
145 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const;
154 bool SendMessageToClientSync(
155 const defs::connection_t& client, int32_t messageId,
156 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const;
164 bool SendMessageToAllClients(
165 int32_t messageId, const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const;
175 bool SendMessageToClientAsync(
176 const defs::connection_t& client, const defs::char_buffer_t& message, int32_t messageId,
177 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const;
187 bool SendMessageToClientSync(
188 const defs::connection_t& client, const defs::char_buffer_t& message, int32_t messageId,
189 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const;
198 bool SendMessageToAllClients(
199 const defs::char_buffer_t& message, int32_t messageId,
200 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const;
211 template <typename T, typename A = serialize::archives::out_port_bin_t>
212 bool SendMessageToClientAsync(
213 const T& message, const defs::connection_t& client, int32_t messageId,
214 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
215 {
216 return m_tcpTypedServer.SendMessageToClientAsync<T, A>(
217 message, client, messageId, responseAddress);
218 }
229 template <typename T, typename A = serialize::archives::out_port_bin_t>
230 bool
231 SendMessageToClientSync(const T& message, const defs::connection_t& client, int32_t messageId,
232 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
233 {
234 return m_tcpTypedServer.SendMessageToClientSync<T, A>(
235 message, client, messageId, responseAddress);
236 }
246 template <typename T, typename A = serialize::archives::out_port_bin_t>
247 bool
248 SendMessageToAllClients(const T& message, int32_t messageId,
249 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
250 {
251 return m_tcpTypedServer.SendMessageToAllClients<T, A>(message, messageId, responseAddress);
252 }
259 bool SendMessageToClientAsync(const defs::connection_t& client,
260 const defs::char_buffer_t& message) const;
266 bool SendMessageToClientSync(const defs::connection_t& client,
267 const defs::char_buffer_t& message) const;
273 bool SendMessageToAllClients(const defs::char_buffer_t& message) const;
279 size_t NumberOfUnsentAsyncMessages(const defs::connection_t& client) const;
280
286 bool IsConnected(const defs::connection_t& client) const;
287
288 private:
290 messages::MessageBuilder m_messageBuilder{};
292 messages::MessageHandler m_messageHandler{};
294 TcpTypedServer<messages::MessageBuilder> m_tcpTypedServer;
295 };
296
297 } // namespace tcp
298 } // namespace asio
299 } // namespace core_lib
300
301 #endif // SIMPLETCPSERVER
core_lib::asio::defs::default_message_dispatcher_t
std::function< void(default_received_message_ptr_t)> default_message_dispatcher_t
Typedef to default message dispatcher function object.
Definition: AsioDefines.h:286
core_lib::asio::messages::MessageHandler
Default message handler class.
Definition: MessageUtils.h:55
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::tcp::eSendOption
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
core_lib::asio::tcp::TcpTypedServer
A generic bi-directional TCP server.
Definition: TcpTypedServer.h:56
core_lib::asio::tcp::SimpleTcpServer
A simple bi-directional multi-client TCP server, which uses the class MessageHeader as the message he...
Definition: SimpleTcpServer.h:44
core_lib::asio::tcp::SimpleTcpServer::m_tcpTypedServer
TcpTypedServer< messages::MessageBuilder > m_tcpTypedServer
Our actual typed TCP server object.
Definition: SimpleTcpServer.h:294
core_lib::asio::tcp::eSendOption::nagleOn
nagleOn - Send when possible.
TcpTypedServer.h
File containing TCP typed server class declaration.
core_lib::asio::defs::connection_t
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientSync
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.
Definition: SimpleTcpServer.h:231
core_lib::asio::messages::MessageBuilder
Default message builder class.
Definition: MessageUtils.h:268
core_lib::asio::tcp::SimpleTcpServer::SendMessageToAllClients
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.
Definition: SimpleTcpServer.h:248
core_lib::asio::defs::char_buffer_t
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
core_lib::asio::tcp::SimpleTcpServer::SendMessageToClientAsync
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.
Definition: SimpleTcpServer.h:212
core_lib::asio::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).