30#ifndef HORIZON_NETWORKING_CONNECTSOCKETMGR_HPP
31#define HORIZON_NETWORKING_CONNECTSOCKETMGR_HPP
43template <
class SocketType,
class NetworkThreadType>
46 typedef std::map<std::string, std::shared_ptr<SocketType>>
ConnectionMap;
59 virtual std::shared_ptr<Connector>
start(std::string
const &connection_name,
Server *server, std::string
const &connect_ip, uint16_t port, uint32_t connections = 1,
bool minimal =
false)
61 std::shared_ptr<Connector> connector;
63 if (!(connector = std::make_shared<Horizon::Networking::Connector>(connection_name, server, connect_ip, port))) {
64 HLog(error) <<
"ConnectSocketMgr::Start: " << connection_name <<
" failed to connect to tcp::" << connect_ip <<
"@" << port;
69 HLog(error) <<
"ConnectSocketMgr::Start failed to start network threads.";
77 connector->connect_with_callback(
79 this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), connections);
102 void on_socket_open(std::string
const &conn_name, std::shared_ptr<tcp::socket>
const &tcp_socket, uint32_t thread_index)
121 }
catch (std::out_of_range &) {
122 return std::shared_ptr<SocketType>();
133 socket->second->delayed_close_socket();
#define HLog(type)
Definition: Logger.hpp:122
Socket manager that handles sockets that were created by the connector.
Definition: ConnectSocketMgr.hpp:45
ConnectionMap _connection_map
Definition: ConnectSocketMgr.hpp:141
std::shared_ptr< SocketType > get_socket_from_connections(std::string const &conn_name)
Definition: ConnectSocketMgr.hpp:115
std::map< std::string, std::shared_ptr< SocketType > > ConnectionMap
Definition: ConnectSocketMgr.hpp:46
virtual bool stop_network() override
Stop the Connector network and clear the connection pool.
Definition: ConnectSocketMgr.hpp:87
void remove_socket_from_connections(std::string const &conn_name)
Definition: ConnectSocketMgr.hpp:126
SocketMgr< SocketType, NetworkThreadType > BaseSocketMgr
Definition: ConnectSocketMgr.hpp:47
void on_socket_open(std::string const &conn_name, std::shared_ptr< tcp::socket > const &tcp_socket, uint32_t thread_index)
On Server Type Socket Open / Start Routine.
Definition: ConnectSocketMgr.hpp:102
void add_socket_to_connections(std::string const &conn_name, std::shared_ptr< SocketType > sock)
Definition: ConnectSocketMgr.hpp:108
std::shared_mutex _connection_map_mtx
Definition: ConnectSocketMgr.hpp:140
virtual std::shared_ptr< Connector > start(std::string const &connection_name, Server *server, std::string const &connect_ip, uint16_t port, uint32_t connections=1, bool minimal=false)
Initialize and start connecting synchronously.
Definition: ConnectSocketMgr.hpp:59
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
Definition: Server.hpp:554
Definition: Element.hpp:7