qb
2.0.0.0
C++17 Actor Framework
|
Asynchronous TCP server implementation. More...
#include <server.h>
Public Member Functions | |
server ()=default | |
Default constructor. | |
void | on (typename acceptor_type::accepted_socket_type &&new_io) |
Handler for new accepted connections. | |
void | on (event::disconnected &&) |
Handler for server disconnection events. | |
Public Member Functions inherited from qb::io::async::tcp::acceptor< server< _Derived, _Session, _Prot >, _Prot > | |
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. | |
IProtocol * | protocol () |
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. | |
Public Member Functions inherited from qb::io::async::io_handler< _Derived, _Session > | |
io_handler ()=default | |
Default constructor. | |
~io_handler ()=default | |
Default destructor. | |
session_map_t & | sessions () |
Get the map of active sessions. | |
std::shared_ptr< _Session > | session (uuid id) |
Get a session by its UUID. | |
template<typename... Args> | |
_Session & | registerSession (typename _Session::transport_io_type &&new_io, Args &&...args) |
Register a new session. | |
void | unregisterSession (uuid const &ident) |
Unregister a session. | |
std::pair< typename _Session::transport_io_type, bool > | extractSession (uuid const &ident) |
Extract a session's IO object. | |
template<typename... _Args> | |
_Derived & | stream (_Args &&...args) |
Broadcast data to all sessions. | |
template<typename _Func, typename... _Args> | |
_Derived & | stream_if (_Func const &func, _Args &&...args) |
Broadcast data to selected sessions. |
Additional Inherited Members | |
Public Types inherited from qb::io::async::tcp::acceptor< server< _Derived, _Session, _Prot >, _Prot > | |
using | accepted_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 Types inherited from qb::io::async::io_handler< _Derived, _Session > | |
using | session_map_t = qb::unordered_map<uuid, std::shared_ptr<_Session>> |
Type alias for the map of sessions. | |
using | IOSession = _Session |
Type alias for the session class. | |
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. |
Asynchronous TCP server implementation.
This template class implements a complete asynchronous TCP server. It combines the TCP acceptor for handling incoming connections with the IO handler for managing client sessions. New connections are automatically accepted and registered as sessions.
_Derived | The derived class type (CRTP pattern) |
_Session | The session class type for handling client connections |
_Prot | The protocol class for parsing messages |
|
inline |
Handler for new accepted connections.
This method is called when a new connection is accepted by the acceptor. It registers the new connection as a session.
new_io | The new socket for the accepted connection |
|
inline |
Handler for server disconnection events.
This method is called when the server is disconnected. The default implementation does nothing, but it can be overridden in derived classes to handle server disconnection.
Disconnected | event |