Horizon Official Technical Documentation
SocketMgr.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_SOCKETMGR_HPP
31#define HORIZON_NETWORKING_SOCKETMGR_HPP
32
37
38#include <boost/asio.hpp>
39#include <assert.h>
40#include <iostream>
41#include <memory>
42#include <boost/scoped_ptr.hpp>
43
44namespace Horizon
45{
46namespace Networking
47{
48template <class SocketType, class NetworkThreadType>
50{
51 typedef std::shared_ptr<NetworkThreadType> NetworkThreadPtr;
52 typedef std::unordered_map<uint32_t, NetworkThreadPtr> network_thread_map;
53public:
54 virtual ~SocketMgr()
55 {
56 assert(_thread_map.empty());
57 }
58
64 virtual bool StartNetworkThreads(uint32_t threads = 1)
65 {
66 for (uint32_t i = 0; i < threads; i++) {
67 NetworkThreadPtr network_thr = std::make_shared<NetworkThreadType>();
68
69 if (network_thr == nullptr) {
70 HLog(error) << "Networking: Error in creating threads, SocketMgr::StartThreadForNetworks.";
71 return false;
72 }
73
74 network_thr->start(i + 1);
75
76 _thread_map.insert(std::make_pair(i, network_thr));
77 }
78
79 return true;
80 }
81
85 virtual bool stop_network()
86 {
87 /* Clear the thread map. */
88 for (auto it = _thread_map.begin(); it != _thread_map.end();) {
89 NetworkThreadPtr thr = it->second;
90 thr->finalize();
91 // Wait for thread to finalize all sockets.
92 while (thr->is_finalizing())
93 {
94 std::this_thread::sleep_for(std::chrono::milliseconds(100));
95 };
96 thr->join();
97 it = _thread_map.erase(it);
98 }
99
100 return true;
101 }
102
107 uint32_t GetNetworkThreadCount() const { return (uint32_t) _thread_map.size(); }
108
114 {
115 uint32_t min_idx = 0;
116
117 for (auto &thr : _thread_map)
118 if (thr.second->connection_count() < NetworkThreadPtr(_thread_map.at(min_idx))->connection_count())
119 min_idx = thr.first;
120
121 return min_idx;
122 }
123
131 std::shared_ptr<SocketType> on_socket_open(std::shared_ptr<tcp::socket> const &socket, uint32_t thread_index)
132 {
133 std::shared_ptr<SocketType> new_socket = std::make_shared<SocketType>(++_last_socket_id, std::move(socket));
134
135 try {
136 // Add socket to thread.
137 NetworkThreadPtr(_thread_map.at(thread_index))->add_socket(new_socket);
138 } catch (boost::system::system_error const &error) {
139 HLog(error) << "Networking: Failed to retrieve client's remote address " << error.what();
140 }
141
142 return new_socket;
143 }
144
149 std::pair<std::shared_ptr<tcp::socket>, uint32_t> get_new_socket()
150 {
151 int min_idx = SelectThreadWithMinConnections();
152 return std::make_pair(NetworkThreadPtr(_thread_map.at(min_idx))->get_new_socket(), min_idx);
153 }
154
156
157private:
158 uint64_t _last_socket_id{0};
160};
161}
162}
163
164#endif /* HORIZON_NETWORKING_SOCKETMGR_HPP */
#define HLog(type)
Definition: Logger.hpp:122
Definition: SocketMgr.hpp:50
uint32_t SelectThreadWithMinConnections() const
Select the thread with the least number of connections, for new socket additions.
Definition: SocketMgr.hpp:113
virtual bool StartNetworkThreads(uint32_t threads=1)
Main function that deals with network thread initiation.
Definition: SocketMgr.hpp:64
uint32_t GetNetworkThreadCount() const
Get the current size of the thread map.
Definition: SocketMgr.hpp:107
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
virtual ~SocketMgr()
Definition: SocketMgr.hpp:54
network_thread_map _thread_map
Unordered map of threads with a unique integer as the key.
Definition: SocketMgr.hpp:159
std::unordered_map< uint32_t, NetworkThreadPtr > network_thread_map
Definition: SocketMgr.hpp:52
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
virtual bool stop_network()
Stops network threads and clears the thread map.
Definition: SocketMgr.hpp:85
uint64_t _last_socket_id
ID of the last socket connection. Used for new connection IDs.
Definition: SocketMgr.hpp:158
std::shared_ptr< NetworkThreadType > NetworkThreadPtr
Definition: SocketMgr.hpp:51
network_thread_map & get_thread_map()
Definition: SocketMgr.hpp:155
Definition: Element.hpp:7