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

Socket manager that handles sockets that were created by the connector. More...

#include <ConnectSocketMgr.hpp>

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

Public Member Functions

virtual std::shared_ptr< Connectorstart (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. More...
 
virtual bool stop_network () override
 Stop the Connector network and clear the connection pool. More...
 
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. More...
 
void add_socket_to_connections (std::string const &conn_name, std::shared_ptr< SocketType > sock)
 
std::shared_ptr< SocketType > get_socket_from_connections (std::string const &conn_name)
 
void remove_socket_from_connections (std::string const &conn_name)
 
- 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< std::string, std::shared_ptr< SocketType > > ConnectionMap
 
typedef SocketMgr< SocketType, NetworkThreadType > BaseSocketMgr
 

Private Attributes

std::shared_mutex _connection_map_mtx
 
ConnectionMap _connection_map
 

Detailed Description

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

Socket manager that handles sockets that were created by the connector.

Member Typedef Documentation

◆ BaseSocketMgr

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

◆ ConnectionMap

template<class SocketType , class NetworkThreadType >
typedef std::map<std::string, std::shared_ptr<SocketType> > Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::ConnectionMap
private

Member Function Documentation

◆ add_socket_to_connections()

template<class SocketType , class NetworkThreadType >
void Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::add_socket_to_connections ( std::string const &  conn_name,
std::shared_ptr< SocketType >  sock 
)
inline
109 {
110 std::unique_lock<std::shared_mutex> _connection_map_mtx;
111
112 _connection_map.emplace(conn_name, sock);
113 }
ConnectionMap _connection_map
Definition: ConnectSocketMgr.hpp:141
std::shared_mutex _connection_map_mtx
Definition: ConnectSocketMgr.hpp:140

References Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::_connection_map, and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::_connection_map_mtx.

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

+ Here is the caller graph for this function:

◆ get_socket_from_connections()

template<class SocketType , class NetworkThreadType >
std::shared_ptr< SocketType > Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::get_socket_from_connections ( std::string const &  conn_name)
inline
116 {
117 std::shared_lock<std::shared_mutex> _connection_map_mtx;
118
119 try {
120 return _connection_map.at(conn_name);
121 } catch (std::out_of_range &/*e*/) {
122 return std::shared_ptr<SocketType>();
123 }
124 }

References Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::_connection_map, and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::_connection_map_mtx.

◆ on_socket_open()

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

On Server Type Socket Open / Start Routine.

  • Move the socket ownership from the caller to a new socket and add it to a thread.
    Parameters
    [in]conn_nameas the name of the connection.
    [in]socketshared pointer.
    [in]thread_indexindex of the thread that the socket is being moved from.
103 {
104 std::shared_ptr<SocketType> socket = SocketMgr<SocketType, NetworkThreadType>::on_socket_open(std::move(tcp_socket), thread_index);
105 add_socket_to_connections(conn_name, socket);
106 }
void add_socket_to_connections(std::string const &conn_name, std::shared_ptr< SocketType > sock)
Definition: ConnectSocketMgr.hpp:108
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::ConnectSocketMgr< SocketType, NetworkThreadType >::add_socket_to_connections(), and Horizon::Networking::SocketMgr< SocketType, NetworkThreadType >::on_socket_open().

+ Here is the call graph for this function:

◆ remove_socket_from_connections()

template<class SocketType , class NetworkThreadType >
void Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::remove_socket_from_connections ( std::string const &  conn_name)
inline
127 {
128 std::unique_lock<std::shared_mutex> _connection_map_mtx;
129
130 auto socket = _connection_map.find(conn_name);
131
132 if (socket != _connection_map.end())
133 socket->second->delayed_close_socket();
134
135 _connection_map.erase(conn_name);
136 }

References Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::_connection_map, and Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::_connection_map_mtx.

◆ start()

template<class SocketType , class NetworkThreadType >
virtual std::shared_ptr< Connector > Horizon::Networking::ConnectSocketMgr< SocketType, NetworkThreadType >::start ( std::string const &  connection_name,
Server server,
std::string const &  connect_ip,
uint16_t  port,
uint32_t  connections = 1,
bool  minimal = false 
)
inlinevirtual

Initialize and start connecting synchronously.

This method also starts the networking threads for connected sockets.

Parameters
[in|out]connection_name const reference to the connection name string.
[in|out]server pointer to the server instance that issued the connection.
[in|out]connect_ip const reference to the ip_address string for the connection endpoint.
[in]portport number for the connection endpoint.
[in]connectionsnumber of connections to create and handle.
Returns
true on success, false on failure.
60 {
61 std::shared_ptr<Connector> connector;
62
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;
65 return nullptr;
66 }
67
69 HLog(error) << "ConnectSocketMgr::Start failed to start network threads.";
70 return nullptr;
71 }
72
73 // Set the socket factory. & Start attempting to connect.
74 connector->set_socket_factory(std::bind(&BaseSocketMgr::get_new_socket, this));
75
76 if (minimal == false)
77 connector->connect_with_callback(
79 this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), connections);
80
81 return connector;
82 }
#define HLog(type)
Definition: Logger.hpp:122
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
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::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::ConnectSocketMgr< SocketType, NetworkThreadType >::stop_network ( )
inlineoverridevirtual

Stop the Connector network and clear the connection pool.

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

88 {
90 return false;
91
92 return true;
93 }
virtual bool stop_network()
Stops network threads and clears the thread map.
Definition: SocketMgr.hpp:85

Member Data Documentation

◆ _connection_map

◆ _connection_map_mtx


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