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

SimpleTcpClientList.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21
26 #ifndef SIMPLETCPCLIENTLIST_H
27 #define SIMPLETCPCLIENTLIST_H
28
29 #include <map>
30 #include <mutex>
31 #include "Asio/SimpleTcpClient.h"
32
34 namespace core_lib
35 {
37 namespace asio
38 {
40 namespace tcp
41 {
42
44 class CORE_LIBRARY_DLL_SHARED_API SimpleTcpClientList final
45 {
46 using client_ptr_t = std::shared_ptr<SimpleTcpClient>;
47 using client_map_t = std::map<defs::connection_t, client_ptr_t>;
48
49 public:
51 SimpleTcpClientList() = delete;
53 SimpleTcpClientList(SimpleTcpClientList const&) = delete;
55 SimpleTcpClientList& operator=(SimpleTcpClientList const&) = delete;
57 SimpleTcpClientList(SimpleTcpClientList&&) = delete;
59 SimpleTcpClientList& operator=(SimpleTcpClientList&&) = delete;
82 SimpleTcpClientList(boost_iocontext_t& ioContext,
83 defs::default_message_dispatcher_t const& messageDispatcher,
84 eSendOption sendOption = eSendOption::nagleOn,
85 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
86 size_t memPoolMsgCount = 0);
108 SimpleTcpClientList(defs::default_message_dispatcher_t const& messageDispatcher,
109 eSendOption sendOption = eSendOption::nagleOn,
110 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
111 size_t memPoolMsgCount = 0);
113 ~SimpleTcpClientList();
119 defs::connection_t ServerConnection(defs::connection_t const& clientConn) const;
125 bool Connected(defs::connection_t const& server) const;
133 defs::connection_t GetClientDetailsForServer(defs::connection_t const& server) const;
140 void CloseConnection(defs::connection_t const& server) const;
146 void CloseConnections() const;
148 void ClearConnections();
158 bool
159 SendMessageToServerAsync(defs::connection_t const& server, int32_t messageId,
160 defs::connection_t const& responseAddress = defs::NULL_CONNECTION);
172 bool SendMessageToServerSync(defs::connection_t const& server, int32_t messageId,
173 defs::connection_t const& responseAddress = defs::NULL_CONNECTION);
184 bool
185 SendMessageToServerAsync(defs::connection_t const& server, const defs::char_buffer_t& message,
186 int32_t messageId,
187 defs::connection_t const& responseAddress = defs::NULL_CONNECTION);
197 bool SendMessageToServerSync(defs::connection_t const& server,
198 const defs::char_buffer_t& message, int32_t messageId,
199 defs::connection_t const& responseAddress = defs::NULL_CONNECTION);
210 template <typename T, typename A = serialize::archives::out_port_bin_t>
211 bool SendMessageToServerAsync(defs::connection_t const& server, T const& message,
212 int32_t messageId,
213 defs::connection_t const& responseAddress = defs::NULL_CONNECTION)
214 {
215 std::lock_guard<std::mutex> lock(m_mutex);
216
217 auto clientPtr = FindTcpClient(server);
218
219 if (!clientPtr)
220 {
221 clientPtr = CreateTcpClient(server);
222 }
223
224 if (clientPtr)
225 {
226 return clientPtr->SendMessageToServerAsync<T, A>(message, messageId, responseAddress);
227 }
228
229 return false;
230 }
241 template <typename T, typename A = serialize::archives::out_port_bin_t>
242 bool SendMessageToServerSync(defs::connection_t const& server, T const& message,
243 int32_t messageId,
244 defs::connection_t const& responseAddress = defs::NULL_CONNECTION)
245 {
246 std::lock_guard<std::mutex> lock(m_mutex);
247
248 bool success = false;
249 auto clientPtr = FindTcpClient(server);
250
251 if (!clientPtr)
252 {
253 clientPtr = CreateTcpClient(server);
254 }
255
256 if (clientPtr)
257 {
258 success = clientPtr->SendMessageToServerSync<T, A>(message, messageId, responseAddress);
259 }
260
261 return success;
262 }
269 bool SendMessageToServerAsync(defs::connection_t const& server,
270 const defs::char_buffer_t& message);
277 bool SendMessageToServerSync(defs::connection_t const& server,
278 const defs::char_buffer_t& message);
280 void ClearList();
285 std::vector<defs::connection_t> GetServerList() const;
291 size_t NumberOfUnsentAsyncMessages(const defs::connection_t& server) const;
292
293 private:
299 client_ptr_t CreateTcpClient(defs::connection_t const& server);
305 client_ptr_t FindTcpClient(defs::connection_t const& server) const;
306
307 private:
309 mutable std::mutex m_mutex;
311 boost_iocontext_t* m_ioContextPtr{nullptr};
314 defs::default_message_dispatcher_t m_messageDispatcher{};
316 eSendOption m_sendOption{eSendOption::nagleOn};
318 size_t m_maxAllowedUnsentAsyncMessages{MAX_UNSENT_ASYNC_MSG_COUNT};
320 size_t m_memPoolMsgCount{0};
322 client_map_t m_clientMap{};
323 };
324
325 } // namespace tcp
326 } // namespace asio
327 } // namespace core_lib
328
329 #endif // SIMPLETCPCLIENTLIST_H
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::tcp::SimpleTcpClientList::SendMessageToServerAsync
bool SendMessageToServerAsync(defs::connection_t const &server, T const &message, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server asynchronously.
Definition: SimpleTcpClientList.h:211
core_lib::asio::tcp::SimpleTcpClientList
A class implementing a collection of bi-directional simple TCP clients.
Definition: SimpleTcpClientList.h:44
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::tcp::SimpleTcpClientList::m_mutex
std::mutex m_mutex
Mutex to make access to map thread safe.
Definition: SimpleTcpClientList.h:309
SimpleTcpClient.h
File containing the simple TCP client class declaration.
core_lib::asio::tcp::eSendOption
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
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
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::tcp::SimpleTcpClientList::SendMessageToServerSync
bool SendMessageToServerSync(defs::connection_t const &server, T const &message, int32_t messageId, defs::connection_t const &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server synchronously.
Definition: SimpleTcpClientList.h:242
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::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).