Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
TcpTypedClient.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 TCPTYPEDCLIENT
29 #define TCPTYPEDCLIENT
30 
31 #include <mutex>
32 #include "TcpClient.h"
33 #include "MessageUtils.h"
34 
36 namespace core_lib
37 {
39 namespace asio
40 {
42 namespace tcp
43 {
44 
57 template <typename MsgBldr> class TcpTypedClient final
58 {
59 public:
61  TcpTypedClient() = delete;
83  size_t minAmountToRead,
84  const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
85  const defs::message_received_handler_t& messageReceivedHandler,
86  const MsgBldr& messageBuilder, eSendOption sendOption = eSendOption::nagleOn,
87  size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT)
88  : m_messageBuilder{messageBuilder}
89  , m_tcpClient{ioContext,
90  server,
91  minAmountToRead,
92  checkBytesLeftToRead,
93  messageReceivedHandler,
94  sendOption,
95  maxAllowedUnsentAsyncMessages}
96  {
97  }
116  TcpTypedClient(const defs::connection_t& server, size_t minAmountToRead,
117  const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
118  const defs::message_received_handler_t& messageReceivedHandler,
119  const MsgBldr& messageBuilder, eSendOption sendOption = eSendOption::nagleOn,
120  size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT)
121  : m_messageBuilder{messageBuilder}
122  , m_tcpClient{server,
123  minAmountToRead,
124  checkBytesLeftToRead,
125  messageReceivedHandler,
126  sendOption,
127  maxAllowedUnsentAsyncMessages}
128  {
129  }
131  ~TcpTypedClient() = default;
133  TcpTypedClient(const TcpTypedClient&) = delete;
135  TcpTypedClient& operator=(const TcpTypedClient&) = delete;
137  TcpTypedClient(TcpTypedClient&&) = delete;
145  {
146  return m_tcpClient.ServerConnection();
147  }
152  bool Connected() const
153  {
154  return m_tcpClient.Connected();
155  }
163  {
166  }
172  {
174  }
182  bool SendMessageToServerAsync(int32_t messageId,
183  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
184  {
185  std::lock_guard<std::mutex> lock(m_sendMutex);
186 
187  try
188  {
189  auto const& messageBuffer = messages::BuildMessage(
190  messageId, responseAddress, GetClientDetailsForServer(), m_messageBuilder);
191 
192  return m_tcpClient.SendMessageToServerAsync(messageBuffer);
193  }
194  catch (...)
195  {
196  // Do nothing.
197  return false;
198  }
199  }
207  bool SendMessageToServerSync(int32_t messageId,
208  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
209  {
210  std::lock_guard<std::mutex> lock(m_sendMutex);
211 
212  try
213  {
214  auto const& messageBuffer = messages::BuildMessage(
215  messageId, responseAddress, GetClientDetailsForServer(), m_messageBuilder);
216  return m_tcpClient.SendMessageToServerSync(messageBuffer);
217  }
218  catch (...)
219  {
220  return false;
221  }
222  }
231  bool SendMessageToServerAsync(const defs::char_buffer_t& message, int32_t messageId,
232  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
233  {
234  std::lock_guard<std::mutex> lock(m_sendMutex);
235 
236  try
237  {
238  auto const& messageBuffer = messages::BuildMessage(
239  message, messageId, responseAddress, GetClientDetailsForServer(), m_messageBuilder);
240 
241  return m_tcpClient.SendMessageToServerAsync(messageBuffer);
242  }
243  catch (...)
244  {
245  // Do nothing.
246  return false;
247  }
248  }
257  bool SendMessageToServerSync(const defs::char_buffer_t& message, int32_t messageId,
258  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
259  {
260  std::lock_guard<std::mutex> lock(m_sendMutex);
261 
262  try
263  {
264  auto const& messageBuffer = messages::BuildMessage(
265  message, messageId, responseAddress, GetClientDetailsForServer(), m_messageBuilder);
266  return m_tcpClient.SendMessageToServerSync(messageBuffer);
267  }
268  catch (...)
269  {
270  return false;
271  }
272  }
282  template <typename T, typename A = serialize::archives::out_port_bin_t>
283  bool SendMessageToServerAsync(const T& message, int32_t messageId,
284  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
285  {
286  std::lock_guard<std::mutex> lock(m_sendMutex);
287 
288  try
289  {
290  auto const& messageBuffer = messages::BuildMessage<T, A, MsgBldr>(
291  message, messageId, responseAddress, GetClientDetailsForServer(), m_messageBuilder);
292 
293  return m_tcpClient.SendMessageToServerAsync(messageBuffer);
294  }
295  catch (...)
296  {
297  // Do nothing.
298  return false;
299  }
300  }
310  template <typename T, typename A = serialize::archives::out_port_bin_t>
311  bool SendMessageToServerSync(const T& message, int32_t messageId,
312  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
313  {
314  std::lock_guard<std::mutex> lock(m_sendMutex);
315 
316  try
317  {
318  auto const& messageBuffer = messages::BuildMessage<T, A, MsgBldr>(
319  message, messageId, responseAddress, GetClientDetailsForServer(), m_messageBuilder);
320  return m_tcpClient.SendMessageToServerSync(messageBuffer);
321  }
322  catch (...)
323  {
324  return false;
325  }
326  }
333  {
334  try
335  {
336  return m_tcpClient.SendMessageToServerAsync(message);
337  }
338  catch (...)
339  {
340  // Do nothing.
341  return false;
342  }
343  }
350  {
351  try
352  {
353  return m_tcpClient.SendMessageToServerSync(message);
354  }
355  catch (...)
356  {
357  return false;
358  }
359  }
365  {
367  }
368 
369 private:
371  mutable std::mutex m_sendMutex;
373  const MsgBldr& m_messageBuilder;
376 };
377 
378 } // namespace tcp
379 } // namespace asio
380 } // namespace core_lib
381 
382 #endif // TCPTYPEDCLIENT
A bi-directional TCP client.
Definition: TcpClient.h:52
bool SendMessageToServerAsync(int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header-only message to the server asynchronously.
Definition: TcpTypedClient.h:182
size_t NumberOfUnsentAsyncMessages() const
Get number of unsent async messages.
Definition: TcpTypedClient.h:364
~TcpTypedClient()=default
Default destructor.
size_t NumberOfUnsentAsyncMessages() const
Get number of unsent async messages.
Definition: TcpClient.cpp:117
void CloseConnection()
Manually close the connection.
Definition: TcpTypedClient.h:171
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
File containing TCP client class declaration.
bool SendMessageToServerAsync(const defs::char_buffer_t &message)
Send a message buffer to the server asynchronously.
Definition: TcpClient.cpp:97
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
defs::connection_t ServerConnection() const
Retrieve server connection details.
Definition: TcpTypedClient.h:144
bool CheckAndCreateConnection()
Check connection and create if required.
Definition: TcpClient.cpp:146
bool SendMessageToServerSync(int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header-only message to the server synchronously.
Definition: TcpTypedClient.h:207
bool SendMessageToServerSync(const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header plus message buffer to the server synchronously.
Definition: TcpTypedClient.h:257
The core_lib namespace.
Definition: AsioDefines.h:59
A generic bi-directional TCP client.
Definition: TcpTypedClient.h:57
bool Connected() const
Check if the client is connected to the server.
Definition: TcpTypedClient.h:152
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
bool SendMessageToServerSync(const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server synchronously.
Definition: TcpTypedClient.h:311
bool SendMessageToServerSync(const defs::char_buffer_t &message)
Send a message buffer to the server synchronously.
Definition: TcpClient.cpp:107
bool SendMessageToServerSync(const defs::char_buffer_t &message)
Send a message buffer to the server synchronously.
Definition: TcpTypedClient.h:349
nagleOn - Send when possible.
defs::connection_t GetClientDetailsForServer()
Retrieve this client&#39;s connection details.
Definition: TcpTypedClient.h:162
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
TcpTypedClient & operator=(const TcpTypedClient &)=delete
Copy assignment operator - deleted.
bool Connected() const
Check if the client is connected to the server.
Definition: TcpClient.cpp:82
bool SendMessageToServerAsync(const defs::char_buffer_t &message)
Send a message buffer to the server asynchronously.
Definition: TcpTypedClient.h:332
TcpClient m_tcpClient
General purpose TCP client object.
Definition: TcpTypedClient.h:375
void CloseConnection()
Manually close the connection.
Definition: TcpClient.cpp:92
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
bool SendMessageToServerAsync(const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server asynchronously.
Definition: TcpTypedClient.h:283
std::mutex m_sendMutex
Send message mutex.
Definition: TcpTypedClient.h:371
defs::connection_t ServerConnection() const
Retrieve server connection details.
Definition: TcpClient.cpp:77
bool SendMessageToServerAsync(const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header plus message buffer to the server asynchronously.
Definition: TcpTypedClient.h:231
File containing message utils declaration.
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
TcpTypedClient()=delete
Default constructor - deleted.
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
TcpTypedClient(boost_iocontext_t &ioContext, const defs::connection_t &server, 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: TcpTypedClient.h:82
defs::connection_t GetClientDetailsForServer() const
Retrieve this client&#39;s connection details.
Definition: TcpClient.cpp:87
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).
TcpTypedClient(const defs::connection_t &server, 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: TcpTypedClient.h:116
const MsgBldr & m_messageBuilder
Referece to our message builder object.
Definition: TcpTypedClient.h:373