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

Session manager for asynchronous IO. More...

#include <io_handler.h>

Inheritance diagram for qb::io::async::io_handler< _Derived, _Session >:

Public Types

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.

Public Member Functions

 io_handler ()=default
 Default constructor.
 ~io_handler ()=default
 Default destructor.
session_map_tsessions ()
 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.

Detailed Description

template<typename _Derived, typename _Session>
class qb::io::async::io_handler< _Derived, _Session >

Session manager for asynchronous IO.

This template class manages sessions for asynchronous IO operations. It provides methods for registering, tracking, and unregistering sessions, as well as utilities for broadcasting data to all or selected sessions.

Template Parameters
_DerivedThe derived class type (CRTP pattern)
_SessionThe session class type

Member Typedef Documentation

◆ session_map_t

template<typename _Derived, typename _Session>
using qb::io::async::io_handler< _Derived, _Session >::session_map_t = qb::unordered_map<uuid, std::shared_ptr<_Session>>

Type alias for the map of sessions.

Maps session UUIDs to shared pointers of session objects.

Member Function Documentation

◆ sessions()

template<typename _Derived, typename _Session>
session_map_t & qb::io::async::io_handler< _Derived, _Session >::sessions ( )
inline

Get the map of active sessions.

Returns
Reference to the sessions map

◆ session()

template<typename _Derived, typename _Session>
std::shared_ptr< _Session > qb::io::async::io_handler< _Derived, _Session >::session ( uuid id)
inline

Get a session by its UUID.

Parameters
idThe UUID of the session to retrieve
Returns
Shared pointer to the session, or nullptr if not found

◆ registerSession()

template<typename _Derived, typename _Session>
template<typename... Args>
_Session & qb::io::async::io_handler< _Derived, _Session >::registerSession ( typename _Session::transport_io_type && new_io,
Args &&... args )
inline

Register a new session.

Creates and registers a new session with the given IO object and additional arguments. The session is started immediately after registration.

Template Parameters
ArgsTypes of additional arguments for session construction
Parameters
new_ioThe IO object for the new session
argsAdditional arguments for session construction
Returns
Reference to the newly created session

◆ unregisterSession()

template<typename _Derived, typename _Session>
void qb::io::async::io_handler< _Derived, _Session >::unregisterSession ( uuid const & ident)
inline

Unregister a session.

Disconnects and eventually removes the session with the given UUID.

Parameters
identThe UUID of the session to unregister

◆ extractSession()

template<typename _Derived, typename _Session>
std::pair< typename _Session::transport_io_type, bool > qb::io::async::io_handler< _Derived, _Session >::extractSession ( uuid const & ident)
inlinenodiscard

Extract a session's IO object.

Removes the session with the given UUID and returns its IO object.

Parameters
identThe UUID of the session to extract
Returns
A pair containing the IO object and a boolean indicating success

◆ stream()

template<typename _Derived, typename _Session>
template<typename... _Args>
_Derived & qb::io::async::io_handler< _Derived, _Session >::stream ( _Args &&... args)
inline

Broadcast data to all sessions.

Sends the provided data to all active sessions.

Template Parameters
_ArgsTypes of data to send
Parameters
argsData to send to all sessions
Returns
Reference to the derived object for method chaining

◆ stream_if()

template<typename _Derived, typename _Session>
template<typename _Func, typename... _Args>
_Derived & qb::io::async::io_handler< _Derived, _Session >::stream_if ( _Func const & func,
_Args &&... args )
inline

Broadcast data to selected sessions.

Sends the provided data to sessions that match the given predicate.

Template Parameters
_FuncType of the selection predicate
_ArgsTypes of data to send
Parameters
funcPredicate to select sessions to receive the data
argsData to send to selected sessions
Returns
Reference to the derived object for method chaining