Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
TcpConnections.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 
27 #ifndef TCPCONNECTIONS
28 #define TCPCONNECTIONS
29 
30 #include "AsioDefines.h"
31 #include <map>
32 #include <mutex>
33 
35 namespace core_lib
36 {
38 namespace asio
39 {
41 namespace tcp
42 {
43 
45 class TcpConnection;
46 
52 class CORE_LIBRARY_DLL_SHARED_API TcpConnections final
53 {
54 public:
56  TcpConnections() = default;
58  ~TcpConnections() = default;
60  TcpConnections(const TcpConnections&) = delete;
62  TcpConnections& operator=(const TcpConnections&) = delete;
64  TcpConnections(TcpConnections&&) = delete;
66  TcpConnections& operator=(TcpConnections&&) = delete;
71  void Add(defs::tcp_conn_ptr_t const& connection);
76  void Remove(defs::tcp_conn_ptr_t const& connection);
81  size_t Size() const;
86  bool Empty() const;
88  void CloseConnections();
95  bool SendMessageAsync(const defs::connection_t& target,
96  const defs::char_buffer_t& message) const;
103  bool SendMessageSync(const defs::connection_t& target,
104  const defs::char_buffer_t& message) const;
110  bool SendMessageToAll(const defs::char_buffer_t& message) const;
118  defs::connection_t GetLocalEndForRemoteEnd(const defs::connection_t& remoteEnd) const;
124  size_t NumberOfUnsentAsyncMessages(const defs::connection_t& target) const;
125 
131  bool IsConnected(const defs::connection_t& client) const;
132 
133 private:
135  mutable std::mutex m_mutex;
137  using tcp_conn_map = std::map<defs::connection_t, defs::tcp_conn_ptr_t>;
139  tcp_conn_map m_connections{};
140 };
141 
142 } // namespace tcp
143 } // namespace asio
144 } // namespace core_lib
145 
146 #endif // TCPCONNECTIONS
File containing useful definitions.
The core_lib namespace.
Definition: AsioDefines.h:59
std::map< defs::connection_t, defs::tcp_conn_ptr_t > tcp_conn_map
Typedef to our connection map type.
Definition: TcpConnections.h:137
TCP connections class to manage the TcpConnection objects.
Definition: TcpConnections.h:52
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
std::mutex m_mutex
Access mutex for thread safety.
Definition: TcpConnections.h:135
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
std::shared_ptr< tcp::TcpConnection > tcp_conn_ptr_t
Typedef describing shared_ptr to a TcpConnection object.
Definition: AsioDefines.h:156