30#ifndef HORIZON_NETWORKING_ACCEPTSOCKETMGR_HPP
31#define HORIZON_NETWORKING_ACCEPTSOCKETMGR_HPP
47template <
class SocketType,
class NetworkThreadType>
50 typedef std::map<uint32_t, std::shared_ptr<SocketType>>
SocketMap;
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)
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();
72 HLog(error) <<
"AcceptSocketMgr failed to start network threads.";
81 HLog(info) <<
"Networking initialized, listening on " << listen_ip <<
"@" << port <<
".";
82 HLog(info) <<
"Maximum Network Threads: " << threads;
110 void on_socket_open(std::shared_ptr<tcp::socket>
const &socket, uint32_t thread_index)
139 std::shared_ptr<std::pair<bool, std::shared_ptr<SocketType>>> sock_buf;
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());
150 _socket_map.emplace(socket->get_socket_id(), socket);
#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