Horizon Official Technical Documentation
ConnectSocketMgr.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_NETWORKING_CONNECTSOCKETMGR_HPP
31#define HORIZON_NETWORKING_CONNECTSOCKETMGR_HPP
32
34
35namespace Horizon
36{
37namespace Networking
38{
39class Connector;
43template <class SocketType, class NetworkThreadType>
44class ConnectSocketMgr : public SocketMgr<SocketType, NetworkThreadType>
45{
46 typedef std::map<std::string, std::shared_ptr<SocketType>> ConnectionMap;
48public:
59 virtual std::shared_ptr<Connector> start(std::string const &connection_name, Server *server, std::string const &connect_ip, uint16_t port, uint32_t connections = 1, bool minimal = false)
60 {
61 std::shared_ptr<Connector> connector;
62
63 if (!(connector = std::make_shared<Horizon::Networking::Connector>(connection_name, server, connect_ip, port))) {
64 HLog(error) << "ConnectSocketMgr::Start: " << connection_name << " failed to connect to tcp::" << connect_ip << "@" << port;
65 return nullptr;
66 }
67
69 HLog(error) << "ConnectSocketMgr::Start failed to start network threads.";
70 return nullptr;
71 }
72
73 // Set the socket factory. & Start attempting to connect.
74 connector->set_socket_factory(std::bind(&BaseSocketMgr::get_new_socket, this));
75
76 if (minimal == false)
77 connector->connect_with_callback(
79 this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), connections);
80
81 return connector;
82 }
83
87 virtual bool stop_network() override
88 {
90 return false;
91
92 return true;
93 }
94
102 void on_socket_open(std::string const &conn_name, std::shared_ptr<tcp::socket> const &tcp_socket, uint32_t thread_index)
103 {
104 std::shared_ptr<SocketType> socket = SocketMgr<SocketType, NetworkThreadType>::on_socket_open(std::move(tcp_socket), thread_index);
105 add_socket_to_connections(conn_name, socket);
106 }
107
108 void add_socket_to_connections(std::string const &conn_name, std::shared_ptr<SocketType> sock)
109 {
110 std::unique_lock<std::shared_mutex> _connection_map_mtx;
111
112 _connection_map.emplace(conn_name, sock);
113 }
114
115 std::shared_ptr<SocketType> get_socket_from_connections(std::string const &conn_name)
116 {
117 std::shared_lock<std::shared_mutex> _connection_map_mtx;
118
119 try {
120 return _connection_map.at(conn_name);
121 } catch (std::out_of_range &/*e*/) {
122 return std::shared_ptr<SocketType>();
123 }
124 }
125
126 void remove_socket_from_connections(std::string const &conn_name)
127 {
128 std::unique_lock<std::shared_mutex> _connection_map_mtx;
129
130 auto socket = _connection_map.find(conn_name);
131
132 if (socket != _connection_map.end())
133 socket->second->delayed_close_socket();
134
135 _connection_map.erase(conn_name);
136 }
137
138
139private:
140 std::shared_mutex _connection_map_mtx;
142};
143}
144}
145#endif /* HORIZON_NETWORKING_CONNECTSOCKETMGR_HPP */
#define HLog(type)
Definition: Logger.hpp:122
Socket manager that handles sockets that were created by the connector.
Definition: ConnectSocketMgr.hpp:45
ConnectionMap _connection_map
Definition: ConnectSocketMgr.hpp:141
std::shared_ptr< SocketType > get_socket_from_connections(std::string const &conn_name)
Definition: ConnectSocketMgr.hpp:115
std::map< std::string, std::shared_ptr< SocketType > > ConnectionMap
Definition: ConnectSocketMgr.hpp:46
virtual bool stop_network() override
Stop the Connector network and clear the connection pool.
Definition: ConnectSocketMgr.hpp:87
void remove_socket_from_connections(std::string const &conn_name)
Definition: ConnectSocketMgr.hpp:126
SocketMgr< SocketType, NetworkThreadType > BaseSocketMgr
Definition: ConnectSocketMgr.hpp:47
void on_socket_open(std::string const &conn_name, std::shared_ptr< tcp::socket > const &tcp_socket, uint32_t thread_index)
On Server Type Socket Open / Start Routine.
Definition: ConnectSocketMgr.hpp:102
void add_socket_to_connections(std::string const &conn_name, std::shared_ptr< SocketType > sock)
Definition: ConnectSocketMgr.hpp:108
std::shared_mutex _connection_map_mtx
Definition: ConnectSocketMgr.hpp:140
virtual std::shared_ptr< Connector > start(std::string const &connection_name, Server *server, std::string const &connect_ip, uint16_t port, uint32_t connections=1, bool minimal=false)
Initialize and start connecting synchronously.
Definition: ConnectSocketMgr.hpp:59
Definition: SocketMgr.hpp:50
virtual bool StartNetworkThreads(uint32_t threads=1)
Main function that deals with network thread initiation.
Definition: SocketMgr.hpp:64
std::pair< std::shared_ptr< tcp::socket >, uint32_t > get_new_socket()
Get a socket from the thread for new server connection.
Definition: SocketMgr.hpp:149
std::shared_ptr< SocketType > on_socket_open(std::shared_ptr< tcp::socket > const &socket, uint32_t thread_index)
On Socket Open / Start Routine.
Definition: SocketMgr.hpp:131
Definition: Server.hpp:554
Definition: Element.hpp:7