Horizon Official Technical Documentation
AcceptSocketMgr.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_ACCEPTSOCKETMGR_HPP
31#define HORIZON_NETWORKING_ACCEPTSOCKETMGR_HPP
32
35
36#include <mutex>
37
38namespace Horizon
39{
40namespace Networking
41{
42class AsyncAcceptor;
47template <class SocketType, class NetworkThreadType>
48class AcceptSocketMgr : public SocketMgr<SocketType, NetworkThreadType>
49{
50 typedef std::map<uint32_t, std::shared_ptr<SocketType>> SocketMap;
52public:
62 virtual bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads = 1, bool minimal = false)
63 {
64 try {
65 _acceptor = std::make_unique<AsyncAcceptor>(io_context, listen_ip, port);
66 } catch (boost::system::system_error const &error) {
67 HLog(error) << "Exception caught in AcceptSocketMgr::start (" << listen_ip.c_str() << ", " << port << ") " << error.what();
68 return false;
69 }
70
72 HLog(error) << "AcceptSocketMgr failed to start network threads.";
73 return false;
74 }
75
76 _acceptor->set_socket_factory(std::bind(&BaseSocketMgr::get_new_socket, this));
77
78 if (minimal == false)
79 _acceptor->async_accept_with_callback(std::bind(&AcceptSocketMgr<SocketType, NetworkThreadType>::on_socket_open, this, std::placeholders::_1, std::placeholders::_2));
80
81 HLog(info) << "Networking initialized, listening on " << listen_ip << "@" << port << ".";
82 HLog(info) << "Maximum Network Threads: " << threads;
83
84 return true;
85 }
86
91 virtual bool stop_network() override
92 {
94
95 if (_acceptor->is_open()) {
96 _acceptor->close();
97 }
98
99 _acceptor.reset();
100 return true;
101 }
102
110 void on_socket_open(std::shared_ptr<tcp::socket> const &socket, uint32_t thread_index)
111 {
112 std::shared_ptr<SocketType> new_socket = BaseSocketMgr::on_socket_open(std::move(socket), thread_index);
113
114 set_socket_for_management(new_socket);
115 }
116
122 void set_socket_for_removal(std::weak_ptr<SocketType> sock)
123 {
124 _socket_management_queue.push(std::make_pair(false, sock.lock()));
125 }
126
127 void set_socket_for_management(std::shared_ptr<SocketType> sock)
128 {
129 _socket_management_queue.push(std::make_pair(true, sock));
130 }
131
137 void manage_sockets(uint32_t time)
138 {
139 std::shared_ptr<std::pair<bool, std::shared_ptr<SocketType>>> sock_buf;
140
141 while ((sock_buf = _socket_management_queue.try_pop())) {
142 bool add = (*sock_buf).first;
143 std::shared_ptr<SocketType> socket = (*sock_buf).second;
144 auto socket_iter = _socket_map.find(socket->get_socket_id());
145
146 if (socket_iter != _socket_map.end()) {
147 if (!add)
148 _socket_map.erase(socket_iter);
149 } else if (add) {
150 _socket_map.emplace(socket->get_socket_id(), socket);
151 }
152 }
153 }
154
156
157private:
158 std::unique_ptr<AsyncAcceptor> _acceptor;
161 std::atomic<bool> _is_initialized;
162};
163}
164}
165
166#endif /* HORIZON_NETWORKING_ACCEPTSOCKETMGR_HPP */
#define HLog(type)
Definition: Logger.hpp:122
Socket Manager for Accepted Sockets.
Definition: AcceptSocketMgr.hpp:49
void manage_sockets(uint32_t time)
Updates every session in the socket map and removes ones in the removal queue.
Definition: AcceptSocketMgr.hpp:137
SocketMap _socket_map
std::map of all connected and handled sockets.
Definition: AcceptSocketMgr.hpp:159
SocketMgr< SocketType, NetworkThreadType > BaseSocketMgr
Definition: AcceptSocketMgr.hpp:51
std::unique_ptr< AsyncAcceptor > _acceptor
unique pointer to an AsyncAcceptor object.
Definition: AcceptSocketMgr.hpp:158
void set_socket_for_management(std::shared_ptr< SocketType > sock)
Definition: AcceptSocketMgr.hpp:127
ThreadSafeQueue< std::pair< bool, std::shared_ptr< SocketType > > > _socket_management_queue
Definition: AcceptSocketMgr.hpp:160
void on_socket_open(std::shared_ptr< tcp::socket > const &socket, uint32_t thread_index)
On Socket Open / Start Event.
Definition: AcceptSocketMgr.hpp:110
std::atomic< bool > _is_initialized
Definition: AcceptSocketMgr.hpp:161
SocketMap & get_sockets()
Definition: AcceptSocketMgr.hpp:155
std::map< uint32_t, std::shared_ptr< SocketType > > SocketMap
Definition: AcceptSocketMgr.hpp:50
virtual bool stop_network() override
Stop the Acceptor network and clear the client socket map.
Definition: AcceptSocketMgr.hpp:91
virtual bool start(boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port, uint32_t threads=1, bool minimal=false)
Initialize and start accepting connections asynchronously.
Definition: AcceptSocketMgr.hpp:62
void set_socket_for_removal(std::weak_ptr< SocketType > sock)
Sets a socket for removal on the next session update call.
Definition: AcceptSocketMgr.hpp:122
Asynchronous acceptor for sockets.
Definition: AsyncAcceptor.hpp:52
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
virtual bool stop_network()
Stops network threads and clears the thread map.
Definition: SocketMgr.hpp:85
Definition: ThreadSafeQueue.hpp:37
void push(T &&new_value)
Definition: ThreadSafeQueue.hpp:90
std::shared_ptr< T > try_pop()
Definition: ThreadSafeQueue.hpp:84
Definition: Element.hpp:7