25#ifndef QB_IO_ASYNC_TCP_CONNECTOR_H
26#define QB_IO_ASYNC_TCP_CONNECTOR_H
29#include <qb/io/system/sys__socket.h>
34namespace qb::io::async::tcp {
48template <
typename Socket_,
typename Func_>
51 const double _timeout;
62 void _establish_connection() {
63 LOG_DEBUG(
"Started async connect to " << _remote.source());
64 auto ret = _socket.n_connect(_remote);
66 LOG_DEBUG(
"Connected directly to " << _remote.source());
67 _func(std::move(_socket));
68 }
else if (socket_no_error(qb::io::socket::get_last_errno())) {
70 .registerEvent<event::io>(*
this, _socket.native_handle(), EV_WRITE)
76 << _remote.source() <<
" err=" << qb::io::socket::get_last_errno());
96 : _func(std::forward<Func_>(func))
97 , _timeout(timeout > 0. ? ev_time() + timeout : 0.)
101 _establish_connection();
118 connector(Socket_&& existing_socket,
uri const &remote, Func_ &&func,
double timeout = 0.)
119 : _func(std::forward<Func_>(func))
120 , _timeout(timeout > 0. ? ev_time() + timeout : 0.)
121 , _socket(std::move(existing_socket))
123 LOG_DEBUG(
"Connector: Initializing with existing socket for " << remote.
source());
124 _establish_connection();
138 on(event::io
const &event) {
140 if (!(event._revents & EV_WRITE) ||
141 _socket.template get_optval<int>(SOL_SOCKET, SO_ERROR, err)) {
142 _socket.disconnect();
144 }
else if (err && (err != EISCONN) && (!_timeout || ev_time() < _timeout))
147 if (!err || err == EISCONN) {
148 LOG_DEBUG(
"Connected async to " << _remote.source());
150 _func(std::move(_socket));
152 LOG_DEBUG(
"Failed to connect to " << _remote.source() <<
" err="
153 << qb::io::socket::get_last_errno());
174template <
typename Socket_,
typename Func_>
176connect(
uri const &remote, Func_ &&func,
double timeout = 0.) {
196template <
typename Socket_,
typename Func_>
198connect(Socket_&& existing_socket,
uri const &remote, Func_ &&func,
double timeout = 0.) {
Core event loop manager for the asynchronous IO framework.
static thread_local listener current
Thread-local instance of the listener.
Definition listener.h:61
Handles asynchronous TCP connection establishment.
Definition connector.h:49
connector(uri const &remote, Func_ &&func, double timeout=0.)
Constructor.
Definition connector.h:95
connector(Socket_ &&existing_socket, uri const &remote, Func_ &&func, double timeout=0.)
Constructor with an existing socket.
Definition connector.h:118
void on(event::io const &event)
I/O event handler.
Definition connector.h:138
Class for parsing, manipulating, and representing URIs.
Definition uri.h:181
const auto & source() const
Returns the source string of this URI.
Definition uri.h:416
void connect(uri const &remote, Func_ &&func, double timeout=0.)
Initiates an asynchronous TCP connection.
Definition connector.h:176
Low-level I/O notification event for asynchronous operations.
Core I/O and logging utilities for the qb framework.
#define LOG_DEBUG(X)
Debug-level log macro (no-op if QB_STDOUT_LOG is not defined)
Definition io.h:215
URI parsing and manipulation utilities.