Horizon Official Technical Documentation
Horizon::Networking::SocketMgr< SocketType, NetworkThreadType > Class Template Reference

#include <SocketMgr.hpp>

+ Inheritance diagram for Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >:

Public Member Functions

virtual ~SocketMgr ()
 
virtual bool StartNetworkThreads (uint32_t threads=1)
 Main function that deals with network thread initiation. More...
 
virtual bool stop_network ()
 Stops network threads and clears the thread map. More...
 
uint32_t GetNetworkThreadCount () const
 Get the current size of the thread map. More...
 
uint32_t SelectThreadWithMinConnections () const
 Select the thread with the least number of connections, for new socket additions. More...
 
std::shared_ptr< SocketType > on_socket_open (std::shared_ptr< tcp::socket > const &socket, uint32_t thread_index)
 On Socket Open / Start Routine. More...
 
std::pair< std::shared_ptr< tcp::socket >, uint32_t > get_new_socket ()
 Get a socket from the thread for new server connection. More...
 
network_thread_mapget_thread_map ()
 

Private Types

typedef std::shared_ptr< NetworkThreadType > NetworkThreadPtr
 
typedef std::unordered_map< uint32_t, NetworkThreadPtrnetwork_thread_map
 

Private Attributes

uint64_t _last_socket_id {0}
 ID of the last socket connection. Used for new connection IDs. More...
 
network_thread_map _thread_map
 Unordered map of threads with a unique integer as the key. More...
 

Member Typedef Documentation

◆ network_thread_map

template<class SocketType , class NetworkThreadType >
typedef std::unordered_map<uint32_t, NetworkThreadPtr> Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::network_thread_map
private

◆ NetworkThreadPtr

template<class SocketType , class NetworkThreadType >
typedef std::shared_ptr<NetworkThreadType> Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::NetworkThreadPtr
private

Constructor & Destructor Documentation

◆ ~SocketMgr()

template<class SocketType , class NetworkThreadType >
virtual Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::~SocketMgr ( )
inlinevirtual
55 {
56 assert(_thread_map.empty());
57 }
network_thread_map _thread_map
Unordered map of threads with a unique integer as the key.
Definition: SocketMgr.hpp:159

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map.

Member Function Documentation

◆ get_new_socket()

template<class SocketType , class NetworkThreadType >
std::pair< std::shared_ptr< tcp::socket >, uint32_t > Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::get_new_socket ( )
inline

Get a socket from the thread for new server connection.

Returns
std::pair of the socket and index of the thread it was taken from.
150 {
151 int min_idx = SelectThreadWithMinConnections();
152 return std::make_pair(NetworkThreadPtr(_thread_map.at(min_idx))->get_new_socket(), min_idx);
153 }
uint32_t SelectThreadWithMinConnections() const
Select the thread with the least number of connections, for new socket additions.
Definition: SocketMgr.hpp:113
std::shared_ptr< NetworkThreadType > NetworkThreadPtr
Definition: SocketMgr.hpp:51

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map, and Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::SelectThreadWithMinConnections().

Referenced by Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::start(), and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::start().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_thread_map()

template<class SocketType , class NetworkThreadType >
network_thread_map & Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::get_thread_map ( )
inline

◆ GetNetworkThreadCount()

template<class SocketType , class NetworkThreadType >
uint32_t Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::GetNetworkThreadCount ( ) const
inline

Get the current size of the thread map.

Returns
size of the thread.
107{ return (uint32_t) _thread_map.size(); }

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map.

◆ on_socket_open()

template<class SocketType , class NetworkThreadType >
std::shared_ptr< SocketType > Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::on_socket_open ( std::shared_ptr< tcp::socket > const &  socket,
uint32_t  thread_index 
)
inline

On Socket Open / Start Routine.

  • Move the socket ownership from the caller to a new socket and add it to a thread.
    Parameters
    [in|out]conn_name as the name of the connection.
    [in]socketshared pointer.
    [in]thread_indexindex of the thread that the socket is being moved from.
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 }
#define HLog(type)
Definition: Logger.hpp:122
uint64_t _last_socket_id
ID of the last socket connection. Used for new connection IDs.
Definition: SocketMgr.hpp:158

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_last_socket_id, Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map, and HLog.

Referenced by Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::on_socket_open(), and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::on_socket_open().

+ Here is the caller graph for this function:

◆ SelectThreadWithMinConnections()

template<class SocketType , class NetworkThreadType >
uint32_t Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::SelectThreadWithMinConnections ( ) const
inline

Select the thread with the least number of connections, for new socket additions.

Returns
thread index.
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 }

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map.

Referenced by Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::get_new_socket().

+ Here is the caller graph for this function:

◆ StartNetworkThreads()

template<class SocketType , class NetworkThreadType >
virtual bool Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::StartNetworkThreads ( uint32_t  threads = 1)
inlinevirtual

Main function that deals with network thread initiation.

Parameters
[in]threadsNumber of threads to start.
Returns
true on success, false on failure.
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 }

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map, and HLog.

Referenced by Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::start(), and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::start().

+ Here is the caller graph for this function:

◆ stop_network()

template<class SocketType , class NetworkThreadType >
virtual bool Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::stop_network ( )
inlinevirtual

Stops network threads and clears the thread map.

Reimplemented in Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >, Horizon::Networking::AcceptSocketMgr< AuthSocket, AuthNetworkThread >, Horizon::Networking::AcceptSocketMgr< CharSocket, CharNetworkThread >, Horizon::Networking::AcceptSocketMgr< ZoneSocket, ZoneNetworkThread >, and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >.

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 }

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_thread_map.

Referenced by Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::stop_network().

+ Here is the caller graph for this function:

Member Data Documentation

◆ _last_socket_id

template<class SocketType , class NetworkThreadType >
uint64_t Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::_last_socket_id {0}
private

ID of the last socket connection. Used for new connection IDs.

Referenced by Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::on_socket_open().

◆ _thread_map


The documentation for this class was generated from the following file: