Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
TcpClientList.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 TCPCLIENTLIST_H
27 #define TCPCLIENTLIST_H
28 
29 #include <map>
30 #include <mutex>
31 #include "AsioDefines.h"
32 
34 namespace core_lib
35 {
37 namespace asio
38 {
40 namespace tcp
41 {
42 
44 class TcpClient;
45 
47 class CORE_LIBRARY_DLL_SHARED_API TcpClientList final
48 {
49  using client_ptr_t = std::shared_ptr<TcpClient>;
50  using client_map_t = std::map<defs::connection_t, client_ptr_t>;
51 
52 public:
54  TcpClientList() = delete;
56  TcpClientList(TcpClientList const&) = delete;
58  TcpClientList& operator=(TcpClientList const&) = delete;
60  TcpClientList(TcpClientList&&) = delete;
62  TcpClientList& operator=(TcpClientList&&) = delete;
81  TcpClientList(boost_iocontext_t& ioContext, size_t minAmountToRead,
82  defs::check_bytes_left_to_read_t const& checkBytesLeftToRead,
83  defs::message_received_handler_t const& messageReceivedHandler,
84  eSendOption sendOption = eSendOption::nagleOn,
85  size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT);
103  TcpClientList(size_t minAmountToRead,
104  defs::check_bytes_left_to_read_t const& checkBytesLeftToRead,
105  defs::message_received_handler_t const& messageReceivedHandler,
106  eSendOption sendOption = eSendOption::nagleOn,
107  size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT);
109  ~TcpClientList();
115  defs::connection_t ServerConnection(defs::connection_t const& clientConn) const;
121  bool Connected(defs::connection_t const& server) const;
129  defs::connection_t GetClientDetailsForServer(defs::connection_t const& server) const;
136  void CloseConnection(defs::connection_t const& server) const;
142  void CloseConnections() const;
144  void ClearConnections();
151  bool SendMessageToServerAsync(defs::connection_t const& server,
152  defs::char_buffer_t const& message);
159  bool SendMessageToServerSync(defs::connection_t const& server,
160  defs::char_buffer_t const& message);
162  void ClearList();
167  std::vector<defs::connection_t> GetServerList() const;
173  size_t NumberOfUnsentAsyncMessages(const defs::connection_t& server) const;
174 
175 private:
181  client_ptr_t CreateTcpClient(defs::connection_t const& server);
187  client_ptr_t FindTcpClient(defs::connection_t const& server) const;
188 
189 private:
191  mutable std::mutex m_mutex;
193  boost_iocontext_t* m_ioContextPtr{nullptr};
195  size_t m_minAmountToRead{0};
198  defs::check_bytes_left_to_read_t m_checkBytesLeftToRead{};
201  defs::message_received_handler_t m_messageReceivedHandler{};
205  size_t m_maxAllowedUnsentAsyncMessages{MAX_UNSENT_ASYNC_MSG_COUNT};
207  client_map_t m_clientMap{};
208 };
209 
210 } // namespace tcp
211 } // namespace asio
212 } // namespace core_lib
213 
214 #endif // TCPCLIENTLIST_H
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 useful definitions.
std::mutex m_mutex
Mutex to make access to map thread safe.
Definition: TcpClientList.h:191
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
A class implementing a collection of bi-directional TCP clients.
Definition: TcpClientList.h:47
The core_lib namespace.
Definition: AsioDefines.h:59
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
nagleOn - Send when possible.
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239