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

Socket Manager for Accepted Sockets. More...

#include <AcceptSocketMgr.hpp>

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

Public Member Functions

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. More...
 
virtual bool stop_network () override
 Stop the Acceptor network and clear the client socket map. More...
 
void on_socket_open (std::shared_ptr< tcp::socket > const &socket, uint32_t thread_index)
 On Socket Open / Start Event. More...
 
void set_socket_for_removal (std::weak_ptr< SocketType > sock)
 Sets a socket for removal on the next session update call. More...
 
void set_socket_for_management (std::shared_ptr< SocketType > sock)
 
void manage_sockets (uint32_t time)
 Updates every session in the socket map and removes ones in the removal queue. More...
 
SocketMapget_sockets ()
 
- Public Member Functions inherited from Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >
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::map< uint32_t, std::shared_ptr< SocketType > > SocketMap
 
typedef SocketMgr< SocketType, NetworkThreadType > BaseSocketMgr
 

Private Attributes

std::unique_ptr< AsyncAcceptor_acceptor
 unique pointer to an AsyncAcceptor object. More...
 
SocketMap _socket_map
 std::map of all connected and handled sockets. More...
 
ThreadSafeQueue< std::pair< bool, std::shared_ptr< SocketType > > > _socket_management_queue
 
std::atomic< bool > _is_initialized
 

Detailed Description

template<class SocketType, class NetworkThreadType>
class Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >

Socket Manager for Accepted Sockets.

Deals with client sockets that were accepted by the server.

Member Typedef Documentation

◆ BaseSocketMgr

template<class SocketType , class NetworkThreadType >
typedef SocketMgr<SocketType, NetworkThreadType> Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::BaseSocketMgr
private

◆ SocketMap

template<class SocketType , class NetworkThreadType >
typedef std::map<uint32_t, std::shared_ptr<SocketType> > Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::SocketMap
private

Member Function Documentation

◆ get_sockets()

template<class SocketType , class NetworkThreadType >
SocketMap & Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::get_sockets ( )
inline
155{ return _socket_map; }
SocketMap _socket_map
std::map of all connected and handled sockets.
Definition: AcceptSocketMgr.hpp:159

References Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_socket_map.

◆ manage_sockets()

template<class SocketType , class NetworkThreadType >
void Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::manage_sockets ( uint32_t  time)
inline

Updates every session in the socket map and removes ones in the removal queue.

  • Called from the main thread only.
    Parameters
    [in]diff
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 }
ThreadSafeQueue< std::pair< bool, std::shared_ptr< SocketType > > > _socket_management_queue
Definition: AcceptSocketMgr.hpp:160
std::shared_ptr< T > try_pop()
Definition: ThreadSafeQueue.hpp:84

References Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_socket_management_queue, Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_socket_map, and ThreadSafeQueue< T >::try_pop().

+ Here is the call graph for this function:

◆ on_socket_open()

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

On Socket Open / Start Event.

Called as a callback from an Acceptor on successful socket acceptance. Moves the socket ownership from the caller to a network thread.

  • Called from global I/O thread.
    Parameters
    [in]socketshared pointer.
    [in]thread_indexindex of the thread that the socket is being moved from.
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 }
void set_socket_for_management(std::shared_ptr< SocketType > sock)
Definition: AcceptSocketMgr.hpp:127
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

References Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::on_socket_open(), and Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::set_socket_for_management().

+ Here is the call graph for this function:

◆ set_socket_for_management()

template<class SocketType , class NetworkThreadType >
void Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::set_socket_for_management ( std::shared_ptr< SocketType >  sock)
inline
128 {
129 _socket_management_queue.push(std::make_pair(true, sock));
130 }
void push(T &&new_value)
Definition: ThreadSafeQueue.hpp:90

References Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_socket_management_queue, and ThreadSafeQueue< T >::push().

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

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

◆ set_socket_for_removal()

template<class SocketType , class NetworkThreadType >
void Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::set_socket_for_removal ( std::weak_ptr< SocketType >  sock)
inline

Sets a socket for removal on the next session update call.

  • Thread safe.
    Parameters
    [in]socksocket to be removed.
123 {
124 _socket_management_queue.push(std::make_pair(false, sock.lock()));
125 }

References Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_socket_management_queue, and ThreadSafeQueue< T >::push().

+ Here is the call graph for this function:

◆ start()

template<class SocketType , class NetworkThreadType >
virtual bool Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::start ( boost::asio::io_context &  io_context,
std::string const &  listen_ip,
uint16_t  port,
uint32_t  threads = 1,
bool  minimal = false 
)
inlinevirtual

Initialize and start accepting connections asynchronously.

This method also starts the networking threads for accepted sockets.

Parameters
[in|out]&io_context const reference to the io_context object.
[in|out]&listen_ip const reference to the ip_address string for the acceptor to bind on.
[in]portport number for the acceptor to bind on.
[in]threadsnumber of network acceptor threads to start and run.
Returns
true on success, false on failure.

Reimplemented in Horizon::Zone::ClientSocketMgr, Horizon::Auth::ClientSocketMgr, and Horizon::Char::ClientSocketMgr.

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 }
#define HLog(type)
Definition: Logger.hpp:122
std::unique_ptr< AsyncAcceptor > _acceptor
unique pointer to an AsyncAcceptor object.
Definition: AcceptSocketMgr.hpp:158
void on_socket_open(std::shared_ptr< tcp::socket > const &socket, uint32_t thread_index)
On Socket Open / Start Event.
Definition: AcceptSocketMgr.hpp:110
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

References Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_acceptor, Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::get_new_socket(), HLog, and Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::StartNetworkThreads().

+ Here is the call graph for this function:

◆ stop_network()

template<class SocketType , class NetworkThreadType >
virtual bool Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::stop_network ( )
inlineoverridevirtual

Stop the Acceptor network and clear the client socket map.

  • Called from the main thread only.

Reimplemented from Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >.

92 {
94
95 if (_acceptor->is_open()) {
96 _acceptor->close();
97 }
98
99 _acceptor.reset();
100 return true;
101 }
virtual bool stop_network()
Stops network threads and clears the thread map.
Definition: SocketMgr.hpp:85

References Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_acceptor, and Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::stop_network().

+ Here is the call graph for this function:

Member Data Documentation

◆ _acceptor

template<class SocketType , class NetworkThreadType >
std::unique_ptr<AsyncAcceptor> Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_acceptor
private

◆ _is_initialized

template<class SocketType , class NetworkThreadType >
std::atomic<bool> Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_is_initialized
private

◆ _socket_management_queue

◆ _socket_map

template<class SocketType , class NetworkThreadType >
SocketMap Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::_socket_map
private

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