Horizon Official Technical Documentation
Horizon::Networking::Connector Class Reference

Connector object that allows connecting to remote endpoints. More...

#include <Connector.hpp>

+ Collaboration diagram for Horizon::Networking::Connector:

Public Types

typedef std::function< void(std::string &, std::shared_ptr< tcp::socket >, uint32_t)> ConnectorCallback
 

Public Member Functions

 Connector (std::string const &connection_name, Server *server, std::string const &connect_ip, uint16_t port)
 Connector contructor. More...
 
 Connector ()
 Destructor of the object that handles joining of all socket polling threads. More...
 
void connect_with_callback (ConnectorCallback callback, int connections=1)
 Attempts connections to the remote endpoint that the object was initiated with. More...
 
void set_socket_factory (std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> &&func)
 Sets a socket factory method that provides a socket for new connections. More...
 

Private Member Functions

std::pair< std::shared_ptr< tcp::socket >, uint32_t > default_socket_factory ()
 

Private Attributes

Serverserver
 
std::string _connection_name
 
tcp::endpoint _endpoint
 
std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> _socket_factory
 

Detailed Description

Connector object that allows connecting to remote endpoints.

Connections are passed to network connected threads that handle them separately from accepted threads.

Member Typedef Documentation

◆ ConnectorCallback

typedef std::function<void(std::string &, std::shared_ptr<tcp::socket>, uint32_t)> Horizon::Networking::Connector::ConnectorCallback

Constructor & Destructor Documentation

◆ Connector() [1/2]

Horizon::Networking::Connector::Connector ( std::string const &  connection_name,
Server server,
std::string const &  connect_ip,
uint16_t  port 
)
inline

Connector contructor.

Parameters
[in|out]connection_name constant reference to the name string of the connection being created.
[in|out]server pointer to the server object that is instantiating the connection.
[in|out]connect_ip constant reference to the ip string of the endpoint to connect to.
[in]portport number of the endpoint to connect to.
65 : server(server), _connection_name(connection_name), _endpoint(boost::asio::ip::make_address(connect_ip), port),
67 {
68 }
Server * server
Definition: Connector.hpp:123
std::pair< std::shared_ptr< tcp::socket >, uint32_t > default_socket_factory()
Definition: Connector.hpp:121
std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> _socket_factory
Definition: Connector.hpp:126
tcp::endpoint _endpoint
Definition: Connector.hpp:125
std::string _connection_name
Definition: Connector.hpp:124

◆ Connector() [2/2]

Horizon::Networking::Connector::Connector ( )
inline

Destructor of the object that handles joining of all socket polling threads.

74 {
75 }

Member Function Documentation

◆ connect_with_callback()

void Horizon::Networking::Connector::connect_with_callback ( ConnectorCallback  callback,
int  connections = 1 
)
inline

Attempts connections to the remote endpoint that the object was initiated with.

The responsibilities of this method are - 1) Attempt to open connections to an endpoint and retry on failure. 2) Execute callbacks to functions that handle the ownership of the connected socket. 3) Start threads that execute state-verification checks of each connection.

Parameters
[in]callbackthe callback function that handles socket ownership.
[in]connectionsthe number of connections to the endpoint that will be initiated and handled.
87 {
88 for (int i = 0; i < connections; i++) {
89 std::shared_ptr<tcp::socket> socket;
90 uint32_t network_thread_idx;
91 boost::system::error_code error;
92
93 // Get a new socket from a thread with the minimum connections.
94 std::tie(socket, network_thread_idx) = _socket_factory();
95
96 do {
97 HLog(info) << "Trying to establish connection for '" << _connection_name << "' at tcp://" << _endpoint.address().to_string() << ":" << _endpoint.port();
98
99 // Try connecting to the endpoint.
100 socket->connect(_endpoint, error);
101
102 if (error.value() != 0) {
103 HLog(info) << "Error connecting to '" << _connection_name << "' at tcp://" << _endpoint.address().to_string() << ":" << _endpoint.port();
104 std::this_thread::sleep_for(std::chrono::seconds(10));
105 socket->close();
106 error.clear();
107 } else {
108 callback(_connection_name, socket, network_thread_idx);
109 HLog(info) << "Successfully connected to '" << _connection_name << "' at tcp://" << _endpoint.address().to_string() << ":" << _endpoint.port();
110 }
111 } while (!socket->is_open());
112 }
113 }
#define HLog(type)
Definition: Logger.hpp:122

References _connection_name, _endpoint, _socket_factory, and HLog.

◆ default_socket_factory()

std::pair< std::shared_ptr< tcp::socket >, uint32_t > Horizon::Networking::Connector::default_socket_factory ( )
inlineprivate
121{ return std::make_pair(nullptr, 0); }

◆ set_socket_factory()

void Horizon::Networking::Connector::set_socket_factory ( std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> &&  func)
inline

Sets a socket factory method that provides a socket for new connections.

118{ _socket_factory = func; }

References _socket_factory.

Member Data Documentation

◆ _connection_name

std::string Horizon::Networking::Connector::_connection_name
private

Referenced by connect_with_callback().

◆ _endpoint

tcp::endpoint Horizon::Networking::Connector::_endpoint
private

Referenced by connect_with_callback().

◆ _socket_factory

std::function<std::pair<std::shared_ptr<tcp::socket>, uint32_t>()> Horizon::Networking::Connector::_socket_factory
private

◆ server

Server* Horizon::Networking::Connector::server
private

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