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

TcpTypedServer.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 TCPTYPEDSERVER
29 #define TCPTYPEDSERVER
30
31 #include <mutex>
32 #include "TcpServer.h"
33 #include "MessageUtils.h"
34
36 namespace core_lib
37 {
39 namespace asio
40 {
42 namespace tcp
43 {
56 template <typename MsgBldr> class TcpTypedServer final
57 {
58 public:
60 TcpTypedServer() = delete;
80 TcpTypedServer(boost_iocontext_t& ioContext, uint16_t listenPort, size_t minAmountToRead,
81 const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
82 const defs::message_received_handler_t& messageReceivedHandler,
83 const MsgBldr& messageBuilder, eSendOption sendOption = eSendOption::nagleOn,
84 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT)
85 : m_messageBuilder{messageBuilder}
86 , m_tcpServer{ioContext,
87 listenPort,
88 minAmountToRead,
89 checkBytesLeftToRead,
90 messageReceivedHandler,
91 sendOption,
92 maxAllowedUnsentAsyncMessages}
93 {
94 }
113 TcpTypedServer(uint16_t listenPort, size_t minAmountToRead,
114 const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
115 const defs::message_received_handler_t& messageReceivedHandler,
116 const MsgBldr& messageBuilder, eSendOption sendOption = eSendOption::nagleOn,
117 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT)
118 : m_messageBuilder{messageBuilder}
119 , m_tcpServer{listenPort,
120 minAmountToRead,
121 checkBytesLeftToRead,
122 messageReceivedHandler,
123 sendOption,
124 maxAllowedUnsentAsyncMessages}
125 {
126 }
128 ~TcpTypedServer() = default;
130 TcpTypedServer(const TcpTypedServer&) = delete;
132 TcpTypedServer& operator=(const TcpTypedServer&) = delete;
134 TcpTypedServer(TcpTypedServer&&) = delete;
136 TcpTypedServer& operator=(TcpTypedServer&&) = delete;
146 defs::connection_t GetServerDetailsForClient(const defs::connection_t& client) const
147 {
148 return m_tcpServer.GetServerDetailsForClient(client);
149 }
154 uint16_t ListenPort() const
155 {
156 return m_tcpServer.ListenPort();
157 }
162 size_t NumberOfClients() const
163 {
164 return m_tcpServer.NumberOfClients();
165 }
170 void CloseAcceptor()
171 {
172 m_tcpServer.CloseAcceptor();
173 }
175 void OpenAcceptor()
176 {
177 m_tcpServer.OpenAcceptor();
178 }
187 bool SendMessageToClientAsync(
188 const defs::connection_t& client, int32_t messageId,
189 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
190 {
191 std::lock_guard<std::mutex> lock(m_sendMutex);
192
193 try
194 {
195 auto const& messageBuffer = messages::BuildMessage(
196 messageId, responseAddress, GetServerDetailsForClient(client), m_messageBuilder);
197
198 return m_tcpServer.SendMessageToClientAsync(client, messageBuffer);
199 }
200 catch (...)
201 {
202 // Do nothing.
203 return false;
204 }
205 }
214 bool
215 SendMessageToClientSync(const defs::connection_t& client, int32_t messageId,
216 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
217 {
218 std::lock_guard<std::mutex> lock(m_sendMutex);
219
220 try
221 {
222 auto const& messageBuffer = messages::BuildMessage(
223 messageId, responseAddress, GetServerDetailsForClient(client), m_messageBuilder);
224
225 return m_tcpServer.SendMessageToClientSync(client, messageBuffer);
226 }
227 catch (...)
228 {
229 return false;
230 }
231 }
239 bool
240 SendMessageToAllClients(int32_t messageId,
241 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
242 {
243 std::lock_guard<std::mutex> lock(m_sendMutex);
244
245 try
246 {
247 auto const& messageBuffer =
248 messages::BuildMessage(messageId,
249 responseAddress,
250 GetServerDetailsForClient(defs::NULL_CONNECTION),
251 m_messageBuilder);
252
253 return m_tcpServer.SendMessageToAllClients(messageBuffer);
254 }
255 catch (...)
256 {
257 // Do nothing.
258 return false;
259 }
260 }
270 bool SendMessageToClientAsync(
271 const defs::connection_t& client, const defs::char_buffer_t& message, int32_t messageId,
272 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
273 {
274 std::lock_guard<std::mutex> lock(m_sendMutex);
275
276 try
277 {
278 auto const& messageBuffer = messages::BuildMessage(message,
279 messageId,
280 responseAddress,
281 GetServerDetailsForClient(client),
282 m_messageBuilder);
283
284 return m_tcpServer.SendMessageToClientAsync(client, messageBuffer);
285 }
286 catch (...)
287 {
288 // Do nothing.
289 return false;
290 }
291 }
301 bool
302 SendMessageToClientSync(const defs::connection_t& client, const defs::char_buffer_t& message,
303 int32_t messageId,
304 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
305 {
306 std::lock_guard<std::mutex> lock(m_sendMutex);
307
308 try
309 {
310 auto const& messageBuffer = messages::BuildMessage(message,
311 messageId,
312 responseAddress,
313 GetServerDetailsForClient(client),
314 m_messageBuilder);
315
316 return m_tcpServer.SendMessageToClientSync(client, messageBuffer);
317 }
318 catch (...)
319 {
320 return false;
321 }
322 }
330 bool
331 SendMessageToAllClients(int32_t messageId, const defs::char_buffer_t& message,
332 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
333 {
334 std::lock_guard<std::mutex> lock(m_sendMutex);
335
336 try
337 {
338 auto const& messageBuffer =
339 messages::BuildMessage(message,
340 messageId,
341 responseAddress,
342 GetServerDetailsForClient(defs::NULL_CONNECTION),
343 m_messageBuilder);
344
345 return m_tcpServer.SendMessageToAllClients(messageBuffer);
346 }
347 catch (...)
348 {
349 // Do nothing.
350 return false;
351 }
352 }
363 template <typename T, typename A = serialize::archives::out_port_bin_t>
364 bool SendMessageToClientAsync(
365 const T& message, const defs::connection_t& client, int32_t messageId,
366 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
367 {
368 std::lock_guard<std::mutex> lock(m_sendMutex);
369
370 try
371 {
372 auto const& messageBuffer =
373 messages::BuildMessage<T, A, MsgBldr>(message,
374 messageId,
375 responseAddress,
376 GetServerDetailsForClient(client),
377 m_messageBuilder);
378
379 return m_tcpServer.SendMessageToClientAsync(client, messageBuffer);
380 }
381 catch (...)
382 {
383 // Do nothing.
384 return false;
385 }
386 }
397 template <typename T, typename A = serialize::archives::out_port_bin_t>
398 bool
399 SendMessageToClientSync(const T& message, const defs::connection_t& client, int32_t messageId,
400 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
401 {
402 std::lock_guard<std::mutex> lock(m_sendMutex);
403
404 try
405 {
406 auto const& messageBuffer =
407 messages::BuildMessage<T, A, MsgBldr>(message,
408 messageId,
409 responseAddress,
410 GetServerDetailsForClient(client),
411 m_messageBuilder);
412
413 return m_tcpServer.SendMessageToClientSync(client, messageBuffer);
414 }
415 catch (...)
416 {
417 return false;
418 }
419 }
429 template <typename T, typename A = serialize::archives::out_port_bin_t>
430 bool
431 SendMessageToAllClients(const T& message, int32_t messageId,
432 const defs::connection_t& responseAddress = defs::NULL_CONNECTION) const
433 {
434 std::lock_guard<std::mutex> lock(m_sendMutex);
435
436 try
437 {
438 auto const& messageBuffer = messages::BuildMessage<T, A, MsgBldr>(
439 message,
440 messageId,
441 responseAddress,
442 GetServerDetailsForClient(defs::NULL_CONNECTION),
443 m_messageBuilder);
444
445 return m_tcpServer.SendMessageToAllClients(messageBuffer);
446 #
447 }
448 catch (...)
449 {
450 // Do nothing.
451 return false;
452 }
453 }
460 bool SendMessageToClientAsync(const defs::connection_t& client,
461 const defs::char_buffer_t& message) const
462 {
463 try
464 {
465 return m_tcpServer.SendMessageToClientAsync(client, message);
466 }
467 catch (...)
468 {
469 // Do nothing.
470 return false;
471 }
472 }
479 bool SendMessageToClientSync(const defs::connection_t& client,
480 const defs::char_buffer_t& message) const
481 {
482 try
483 {
484 return m_tcpServer.SendMessageToClientSync(client, message);
485 }
486 catch (...)
487 {
488 return false;
489 }
490 }
496 bool SendMessageToAllClients(const defs::char_buffer_t& message) const
497 {
498 try
499 {
500 return m_tcpServer.SendMessageToAllClients(message);
501 }
502 catch (...)
503 {
504 // Do nothing.
505 return false;
506 }
507 }
513 size_t NumberOfUnsentAsyncMessages(const defs::connection_t& client) const
514 {
515 return m_tcpServer.NumberOfUnsentAsyncMessages(client);
516 }
517
523 bool IsConnected(const defs::connection_t& client) const
524 {
525 return m_tcpServer.IsConnected(client);
526 }
527
528 private:
530 mutable std::mutex m_sendMutex;
532 const MsgBldr& m_messageBuilder;
534 TcpServer m_tcpServer;
535 };
536
537 } // namespace tcp
538 } // namespace asio
539 } // namespace core_lib
540
541 #endif // TCPTYPEDCLIENT
core_lib::asio::tcp::TcpTypedServer::TcpTypedServer
TcpTypedServer()=delete
Default constructor - deleted.
core_lib::asio::tcp::TcpTypedServer::SendMessageToClientAsync
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.
Definition: TcpTypedServer.h:187
core_lib::asio::tcp::TcpTypedServer::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: TcpTypedServer.h:399
core_lib::asio::tcp::TcpTypedServer::SendMessageToClientSync
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.
Definition: TcpTypedServer.h:302
core_lib::asio::tcp::TcpTypedServer::m_messageBuilder
const MsgBldr & m_messageBuilder
Referece to our message builder object.
Definition: TcpTypedServer.h:532
core_lib::asio::tcp::TcpServer::SendMessageToClientSync
bool SendMessageToClientSync(const defs::connection_t &client, const defs::char_buffer_t &message) const
Send a message buffer to a client synchronously.
Definition: TcpServer.cpp:126
core_lib::asio::tcp::TcpTypedServer::m_tcpServer
TcpServer m_tcpServer
General purpose TCP server object.
Definition: TcpTypedServer.h:534
core_lib::asio::tcp::TcpServer::CloseAcceptor
void CloseAcceptor()
Manually close the acceptor.
Definition: TcpServer.cpp:97
core_lib::asio::defs::check_bytes_left_to_read_t
std::function< size_t(const char_buffer_t &)> check_bytes_left_to_read_t
Typedef to bytes left to reading checking utility function object.
Definition: AsioDefines.h:288
core_lib::asio::tcp::TcpTypedServer::SendMessageToAllClients
bool SendMessageToAllClients(int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION) const
Send a header-only message to all clients asynchronously.
Definition: TcpTypedServer.h:240
core_lib::asio::tcp::TcpTypedServer::SendMessageToClientSync
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.
Definition: TcpTypedServer.h:215
core_lib::asio::tcp::TcpTypedServer::SendMessageToClientAsync
bool SendMessageToClientAsync(const defs::connection_t &client, const defs::char_buffer_t &message) const
Send a message buffer to a client asynchronously.
Definition: TcpTypedServer.h:460
core_lib::asio::tcp::TcpTypedServer::IsConnected
bool IsConnected(const defs::connection_t &client) const
Tells if a given client is currently connected to the server.
Definition: TcpTypedServer.h:523
core_lib::asio::tcp::TcpTypedServer::SendMessageToClientSync
bool SendMessageToClientSync(const defs::connection_t &client, const defs::char_buffer_t &message) const
Send a message buffer to a client synchronously.
Definition: TcpTypedServer.h:479
core_lib::asio::tcp::TcpServer::NumberOfClients
size_t NumberOfClients() const
Retrieve this server&#39;s number of clients.
Definition: TcpServer.cpp:92
core_lib::asio::defs::message_received_handler_t
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
core_lib::asio::tcp::TcpTypedServer::CloseAcceptor
void CloseAcceptor()
Manually close the acceptor.
Definition: TcpTypedServer.h:170
core_lib::asio::tcp::TcpServer::NumberOfUnsentAsyncMessages
size_t NumberOfUnsentAsyncMessages(const defs::connection_t &client) const
Get number of unsent async messages.
Definition: TcpServer.cpp:137
core_lib::asio::tcp::TcpServer::SendMessageToAllClients
bool SendMessageToAllClients(const defs::char_buffer_t &message) const
Send a message buffer to all clients asynchronously.
Definition: TcpServer.cpp:132
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::tcp::TcpTypedServer::SendMessageToClientAsync
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.
Definition: TcpTypedServer.h:270
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::TcpServer::GetServerDetailsForClient
defs::connection_t GetServerDetailsForClient(const defs::connection_t &client) const
Retrieve this server&#39;s connection details for a given client.
Definition: TcpServer.cpp:80
core_lib::asio::tcp::TcpServer::IsConnected
bool IsConnected(const defs::connection_t &client) const
Tells if a given client is currently connected to the server.
Definition: TcpServer.cpp:142
core_lib::asio::tcp::eSendOption::nagleOn
nagleOn - Send when possible.
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
core_lib::asio::tcp::TcpTypedServer::SendMessageToAllClients
bool SendMessageToAllClients(const defs::char_buffer_t &message) const
Send a message buffer to all clients asynchronously.
Definition: TcpTypedServer.h:496
core_lib::asio::tcp::TcpTypedServer::TcpTypedServer
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.
Definition: TcpTypedServer.h:80
core_lib::asio::tcp::TcpServer
A bi-directional TCP server.
Definition: TcpServer.h:53
core_lib::asio::tcp::TcpTypedServer::~TcpTypedServer
~TcpTypedServer()=default
Default destructor.
core_lib::asio::tcp::TcpTypedServer::TcpTypedServer
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.
Definition: TcpTypedServer.h:113
core_lib::asio::tcp::TcpTypedServer::SendMessageToAllClients
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.
Definition: TcpTypedServer.h:331
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::tcp::TcpServer::OpenAcceptor
void OpenAcceptor()
Manually open the acceptor.
Definition: TcpServer.cpp:108
core_lib::asio::tcp::TcpTypedServer::operator=
TcpTypedServer & operator=(const TcpTypedServer &)=delete
Copy assignment operator - deleted.
core_lib::asio::tcp::TcpTypedServer::NumberOfUnsentAsyncMessages
size_t NumberOfUnsentAsyncMessages(const defs::connection_t &client) const
Get number of unsent async messages.
Definition: TcpTypedServer.h:513
core_lib::asio::tcp::TcpTypedServer::m_sendMutex
std::mutex m_sendMutex
Send message mutex.
Definition: TcpTypedServer.h:530
MessageUtils.h
File containing message utils declaration.
core_lib::asio::tcp::TcpTypedServer::OpenAcceptor
void OpenAcceptor()
Manually open the acceptor.
Definition: TcpTypedServer.h:175
core_lib::asio::tcp::TcpTypedServer::NumberOfClients
size_t NumberOfClients() const
Retrieve this server&#39;s number of clients.
Definition: TcpTypedServer.h:162
core_lib::asio::tcp::TcpServer::ListenPort
uint16_t ListenPort() const
Retrieve this server&#39;s listen port.
Definition: TcpServer.cpp:87
core_lib::asio::messages::BuildMessage
defs::char_buffer_t const & BuildMessage(int32_t messageId, const defs::connection_t &responseAddress, const defs::connection_t &fallbackResponseAddress, const MsgBldr &messageBuilder)
Message builder wrapper function for header only messages.
Definition: MessageUtils.h:412
core_lib::asio::tcp::TcpTypedServer::GetServerDetailsForClient
defs::connection_t GetServerDetailsForClient(const defs::connection_t &client) const
Retrieve this server&#39;s connection details for a given client.
Definition: TcpTypedServer.h:146
core_lib::asio::tcp::TcpTypedServer::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: TcpTypedServer.h:364
TcpServer.h
File containing TCP server class declaration.
core_lib::asio::tcp::TcpTypedServer::ListenPort
uint16_t ListenPort() const
Retrieve this server&#39;s listen port.
Definition: TcpTypedServer.h:154
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::TcpServer::SendMessageToClientAsync
bool SendMessageToClientAsync(const defs::connection_t &client, const defs::char_buffer_t &message) const
Send a message buffer to a client asynchronously.
Definition: TcpServer.cpp:120
core_lib::asio::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).
core_lib::asio::tcp::TcpTypedServer::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: TcpTypedServer.h:431