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

Asynchronous acceptor for sockets. More...

#include <AsyncAcceptor.hpp>

Public Types

typedef std::function< void(std::shared_ptr< tcp::socket >, uint32_t thread_index)> AcceptCallback
 

Public Member Functions

 ~AsyncAcceptor ()
 
 AsyncAcceptor (boost::asio::io_context &io_context, std::string const &listen_ip, uint16_t port)
 Constructor of the AsyncAcceptor object. More...
 
template<class T >
void AsyncAccept ()
 
void async_accept_with_callback (AcceptCallback callback)
 Asynchronously accepts sockets and executes a callback function. More...
 
bool bind ()
 Binds this instance to an endpoint, to listen for connections. More...
 
void close ()
 Closes the acceptor across all threads. More...
 
bool is_open ()
 
void set_socket_factory (std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> &&func)
 Sets the socket factory for the acceptor. More...
 

Private Member Functions

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

Private Attributes

tcp::endpoint _endpoint
 
std::shared_ptr< tcp::acceptor > _acceptor
 
std::shared_ptr< tcp::socket > _socket
 
std::atomic< bool > _closed
 
std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> _socket_factory
 

Detailed Description

Asynchronous acceptor for sockets.

Member Typedef Documentation

◆ AcceptCallback

typedef std::function<void(std::shared_ptr<tcp::socket>, uint32_t thread_index)> Horizon::Networking::AsyncAcceptor::AcceptCallback

Constructor & Destructor Documentation

◆ ~AsyncAcceptor()

Horizon::Networking::AsyncAcceptor::~AsyncAcceptor ( )
inline
57 {
58 _acceptor.reset();
59 _socket.reset();
60 }
std::shared_ptr< tcp::acceptor > _acceptor
Definition: AsyncAcceptor.hpp:171
std::shared_ptr< tcp::socket > _socket
Definition: AsyncAcceptor.hpp:172

References _acceptor, and _socket.

◆ AsyncAcceptor()

Horizon::Networking::AsyncAcceptor::AsyncAcceptor ( boost::asio::io_context &  io_context,
std::string const &  listen_ip,
uint16_t  port 
)
inline

Constructor of the AsyncAcceptor object.

Parameters
[in|out]io_context reference to the io_context object.
[in]listen_ipconst reference to the ip address string for the acceptor to bind on.
[in]portport number for the acceptor to bind on.
69 : _endpoint(boost::asio::ip::address::from_string(listen_ip), port),
70 _acceptor(std::make_shared<tcp::acceptor>(io_context, tcp::endpoint(boost::asio::ip::address::from_string(listen_ip), port))),
71 _socket(std::make_shared<tcp::socket>(io_context)), _closed(false), _socket_factory(std::bind(&AsyncAcceptor::default_socket_factory, this))
72 {
73 }
std::pair< std::shared_ptr< tcp::socket >, uint32_t > default_socket_factory()
Definition: AsyncAcceptor.hpp:168
std::function< std::pair< std::shared_ptr< tcp::socket >, uint32_t >()> _socket_factory
Definition: AsyncAcceptor.hpp:174
tcp::endpoint _endpoint
Definition: AsyncAcceptor.hpp:170
std::atomic< bool > _closed
Definition: AsyncAcceptor.hpp:173

Member Function Documentation

◆ async_accept_with_callback()

void Horizon::Networking::AsyncAcceptor::async_accept_with_callback ( AcceptCallback  callback)
inline

Asynchronously accepts sockets and executes a callback function.

This function is responsible for - 1) Listening for new connections on the main thread. 2) Executing the callback provided to this method whilst moving its ownership. 3) Execute recursively on successful acceptances until closed.

Parameters
[in]callbackthe callback function to execute (
See also
type AcceptCallback)
86 {
87 std::shared_ptr<tcp::socket> socket;
88
89 uint32_t thread_index;
90
91 std::tie(socket, thread_index) = _socket_factory();
92
93 _acceptor->async_accept(*socket, [this, socket, callback, thread_index] (boost::system::error_code error)
94 {
95 if (!error) {
96 try {
97 socket->non_blocking(true);
98 callback(std::move(socket), thread_index);
99 } catch (boost::system::system_error const &err) {
100 HLog(error) << "Networking: AsyncAcceptor failed to initialize client's socket :" << err.what();
101 }
102 }
103
104 if (!_closed)
105 this->async_accept_with_callback(callback);
106 });
107 }
#define HLog(type)
Definition: Logger.hpp:122
void async_accept_with_callback(AcceptCallback callback)
Asynchronously accepts sockets and executes a callback function.
Definition: AsyncAcceptor.hpp:85

References _acceptor, _closed, _socket_factory, async_accept_with_callback(), and HLog.

Referenced by async_accept_with_callback().

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

◆ AsyncAccept()

template<class T >
void Horizon::Networking::AsyncAcceptor::AsyncAccept
179{
180 _acceptor->async_accept(_socket, [this] (boost::system::error_code error)
181 {
182 if (!error) {
183 try {
184 std::make_shared<T>(std::move(this->_socket))->start();
185 } catch (boost::system::system_error const &err) {
186 std::cerr << "Network Error: failed to retrieve client's remote address " << err.what() << std::endl;
187 }
188 }
189
190 if (!_closed)
191 this->AsyncAccept<T>();
192 });
193}

References _acceptor, _closed, and _socket.

◆ bind()

bool Horizon::Networking::AsyncAcceptor::bind ( )
inline

Binds this instance to an endpoint, to listen for connections.

113 {
114 boost::system::error_code errorCode;
115
116 _acceptor->open(_endpoint.protocol(), errorCode);
117
118 if (errorCode) {
119 HLog(error) << "Failed to open acceptor " << errorCode.message().c_str();
120 return false;
121 }
122
123 _acceptor->bind(_endpoint, errorCode);
124
125 if (errorCode) {
126 HLog(error) << "Could not bind to " << _endpoint.address().to_string().c_str() << ":" << _endpoint.port() << " - "
127 << errorCode.message().c_str();
128 return false;
129 }
130
131 _acceptor->listen(boost::asio::socket_base::max_connections, errorCode);
132
133 if (errorCode) {
134 HLog(error) << "Failed to start listening on " << _endpoint.address().to_string().c_str() << ":" << _endpoint.port() << " " << errorCode.message().c_str();
135 return false;
136 }
137
138 return true;
139 }

References _acceptor, _endpoint, and HLog.

◆ close()

void Horizon::Networking::AsyncAcceptor::close ( )
inline

Closes the acceptor across all threads.

145 {
146 if (_closed.exchange(true))
147 return;
148
149 _socket->close();
150
151 boost::system::error_code error;
152 _acceptor->close(error);
153
154 _acceptor.reset();
155 _socket.reset();
156 if (error)
157 HLog(error) << "Failed to close acceptor: " << error.message().c_str();
158 }

References _acceptor, _closed, _socket, and HLog.

◆ default_socket_factory()

std::pair< std::shared_ptr< tcp::socket >, uint32_t > Horizon::Networking::AsyncAcceptor::default_socket_factory ( )
inlineprivate
168{ return std::make_pair(_socket, 0); }

References _socket.

◆ is_open()

bool Horizon::Networking::AsyncAcceptor::is_open ( )
inline
160{ return !_closed.load(); }

References _closed.

◆ set_socket_factory()

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

Sets the socket factory for the acceptor.

165{ _socket_factory = func; }

References _socket_factory.

Member Data Documentation

◆ _acceptor

std::shared_ptr<tcp::acceptor> Horizon::Networking::AsyncAcceptor::_acceptor
private

◆ _closed

std::atomic<bool> Horizon::Networking::AsyncAcceptor::_closed
private

◆ _endpoint

tcp::endpoint Horizon::Networking::AsyncAcceptor::_endpoint
private

Referenced by bind().

◆ _socket

std::shared_ptr<tcp::socket> Horizon::Networking::AsyncAcceptor::_socket
private

◆ _socket_factory

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

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