30#ifndef HORIZON_NETWORKING_SOCKETMGR_HPP
31#define HORIZON_NETWORKING_SOCKETMGR_HPP
38#include <boost/asio.hpp>
42#include <boost/scoped_ptr.hpp>
48template <
class SocketType,
class NetworkThreadType>
66 for (uint32_t i = 0; i < threads; i++) {
69 if (network_thr ==
nullptr) {
70 HLog(error) <<
"Networking: Error in creating threads, SocketMgr::StartThreadForNetworks.";
74 network_thr->start(i + 1);
92 while (thr->is_finalizing())
94 std::this_thread::sleep_for(std::chrono::milliseconds(100));
115 uint32_t min_idx = 0;
131 std::shared_ptr<SocketType>
on_socket_open(std::shared_ptr<tcp::socket>
const &socket, uint32_t thread_index)
133 std::shared_ptr<SocketType> new_socket = std::make_shared<SocketType>(++
_last_socket_id, std::move(socket));
138 }
catch (boost::system::system_error
const &error) {
139 HLog(error) <<
"Networking: Failed to retrieve client's remote address " << error.what();
#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