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;
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  }
171  {
173  }
176  {
178  }
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,
252 
253  return m_tcpServer.SendMessageToAllClients(messageBuffer);
254  }
255  catch (...)
256  {
257  // Do nothing.
258  return false;
259  }
260  }
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,
283 
284  return m_tcpServer.SendMessageToClientAsync(client, messageBuffer);
285  }
286  catch (...)
287  {
288  // Do nothing.
289  return false;
290  }
291  }
301  bool
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,
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,
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>
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,
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,
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,
444 
445  return m_tcpServer.SendMessageToAllClients(messageBuffer);
446 #
447  }
448  catch (...)
449  {
450  // Do nothing.
451  return false;
452  }
453  }
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  }
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  }
514  {
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;
535 };
536 
537 } // namespace tcp
538 } // namespace asio
539 } // namespace core_lib
540 
541 #endif // TCPTYPEDCLIENT
TcpTypedServer()=delete
Default constructor - deleted.
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
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
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
const MsgBldr & m_messageBuilder
Referece to our message builder object.
Definition: TcpTypedServer.h:532
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
TcpServer m_tcpServer
General purpose TCP server object.
Definition: TcpTypedServer.h:534
void CloseAcceptor()
Manually close the acceptor.
Definition: TcpServer.cpp:97
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
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
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
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
bool IsConnected(const defs::connection_t &client) const
Tells if a given client is currently connected to the server.
Definition: TcpTypedServer.h:523
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
size_t NumberOfClients() const
Retrieve this server&#39;s number of clients.
Definition: TcpServer.cpp:92
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
void CloseAcceptor()
Manually close the acceptor.
Definition: TcpTypedServer.h:170
size_t NumberOfUnsentAsyncMessages(const defs::connection_t &client) const
Get number of unsent async messages.
Definition: TcpServer.cpp:137
bool SendMessageToAllClients(const defs::char_buffer_t &message) const
Send a message buffer to all clients asynchronously.
Definition: TcpServer.cpp:132
The core_lib namespace.
Definition: AsioDefines.h:59
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
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
A generic bi-directional TCP server.
Definition: TcpTypedServer.h:56
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
bool IsConnected(const defs::connection_t &client) const
Tells if a given client is currently connected to the server.
Definition: TcpServer.cpp:142
nagleOn - Send when possible.
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
bool SendMessageToAllClients(const defs::char_buffer_t &message) const
Send a message buffer to all clients asynchronously.
Definition: TcpTypedServer.h:496
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
A bi-directional TCP server.
Definition: TcpServer.h:53
~TcpTypedServer()=default
Default destructor.
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
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_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
void OpenAcceptor()
Manually open the acceptor.
Definition: TcpServer.cpp:108
TcpTypedServer & operator=(const TcpTypedServer &)=delete
Copy assignment operator - deleted.
size_t NumberOfUnsentAsyncMessages(const defs::connection_t &client) const
Get number of unsent async messages.
Definition: TcpTypedServer.h:513
std::mutex m_sendMutex
Send message mutex.
Definition: TcpTypedServer.h:530
File containing message utils declaration.
void OpenAcceptor()
Manually open the acceptor.
Definition: TcpTypedServer.h:175
size_t NumberOfClients() const
Retrieve this server&#39;s number of clients.
Definition: TcpTypedServer.h:162
uint16_t ListenPort() const
Retrieve this server&#39;s listen port.
Definition: TcpServer.cpp:87
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
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
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
File containing TCP server class declaration.
uint16_t ListenPort() const
Retrieve this server&#39;s listen port.
Definition: TcpTypedServer.h:154
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
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
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).
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