qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::io::async::tcp::acceptor< _Derived, _Prot > Class Template Reference

Handles accepting incoming TCP connections asynchronously. More...

#include <acceptor.h>

Inheritance diagram for qb::io::async::tcp::acceptor< _Derived, _Prot >:
Collaboration diagram for qb::io::async::tcp::acceptor< _Derived, _Prot >:

Public Types

using accepted_socket_type = typename _Prot::socket_type
 Type of socket created for accepted connections.
Public Types inherited from qb::io::async::input< acceptor< _Derived, _Prot > >
using base_io_t
 Base I/O type alias for CRTP.

Public Member Functions

void on (event::disconnected &&e)
 Handler for disconnection events.
 acceptor () noexcept
 Constructor.
 ~acceptor ()=default
 Destructor.
void on (typename Protocol::message &&new_socket)
 Handler for new connections.
bool listen (qb::io::uri uri, std::filesystem::path cert_file={}, std::filesystem::path key_file={}, std::vector< std::string > alpn_protocols={})
 Listen for incoming connections on a given URI.
Public Member Functions inherited from qb::io::async::input< acceptor< _Derived, _Prot > >
 input ()=default
 Default constructor.
 input (IProtocol *protocol) noexcept
 Constructor with an initial protocol instance.
 input (input const &)=delete
 Deleted copy constructor to prevent unintended copying of I/O state and resources.
 ~input () noexcept
 Destructor.
_Protocol * switch_protocol (_Args &&...args)
 Switches to a new protocol for processing input, taking ownership of the new protocol.
void clear_protocols ()
 Clears all owned protocol instances.
IProtocolprotocol ()
 Gets a pointer to the current active protocol instance.
void start () noexcept
 Starts asynchronous input operations.
void ready_to_read () noexcept
 Ensures the I/O watcher is listening for read events (EV_READ).
void disconnect (int reason=1)
 Initiates a graceful disconnection of the input component.

Additional Inherited Members

Static Public Attributes inherited from qb::io::async::input< acceptor< _Derived, _Prot > >
static constexpr const bool has_server
 Indicates this component is not inherently a server (e.g., an acceptor).
Protected Member Functions inherited from qb::io::async::input< acceptor< _Derived, _Prot > >
void dispose ()
 Disposes of resources and finalizes disconnection for the input component.
Protected Member Functions inherited from qb::io::async::base< input< _Derived >, event::io >
 base ()
 Constructor that registers the event watcher with the current listener.
 ~base ()
 Destructor that unregisters the event watcher.
Protected Attributes inherited from qb::io::async::base< input< _Derived >, event::io >
event::io & _async_event
 Reference to the registered libev-based event watcher.

Detailed Description

template<typename _Derived, typename _Prot>
class qb::io::async::tcp::acceptor< _Derived, _Prot >

Handles accepting incoming TCP connections asynchronously.

This template class provides functionality for accepting incoming TCP connections in an asynchronous manner. It uses the input class for asynchronous operations and a protocol for accepting and processing incoming connections. When a new connection is accepted, it passes the new socket to the derived class for handling.

Template Parameters
_DerivedThe derived class type (CRTP pattern)
_ProtThe protocol class type to use with the acceptor

Constructor & Destructor Documentation

◆ acceptor()

template<typename _Derived, typename _Prot>
qb::io::async::tcp::acceptor< _Derived, _Prot >::acceptor ( )
inlinenoexcept

Constructor.

Creates a new acceptor with the accept protocol.

Member Function Documentation

◆ on() [1/2]

template<typename _Derived, typename _Prot>
void qb::io::async::tcp::acceptor< _Derived, _Prot >::on ( event::disconnected && e)
inline

Handler for disconnection events.

This method is called when the acceptor is disconnected. If the derived class has a handler for disconnection events, it forwards the event to that handler. Otherwise, it throws a runtime error.

Parameters
eThe disconnection event
Exceptions
std::runtime_errorIf the derived class doesn't handle disconnection

◆ on() [2/2]

template<typename _Derived, typename _Prot>
void qb::io::async::tcp::acceptor< _Derived, _Prot >::on ( typename Protocol::message && new_socket)
inline

Handler for new connections.

This method is called when a new connection is accepted. It forwards the new socket to the derived class for handling.

Parameters
new_socketThe new socket for the accepted connection

◆ listen()

template<typename _Derived, typename _Prot>
bool qb::io::async::tcp::acceptor< _Derived, _Prot >::listen ( qb::io::uri uri,
std::filesystem::path cert_file = {},
std::filesystem::path key_file = {},
std::vector< std::string > alpn_protocols = {} )
inline

Listen for incoming connections on a given URI.

Parameters
uriThe URI to listen on.
cert_fileThe path to the certificate file.
key_fileThe path to the key file.
alpn_protocolsThe ALPN protocols to support.
Returns
True if the server is listening, false otherwise.