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

CRTP base class for managing bidirectional asynchronous I/O operations with protocol processing. More...

#include <io.h>

Inheritance diagram for qb::io::async::io< _Derived >:
Collaboration diagram for qb::io::async::io< _Derived >:

Public Types

typedef io< _Derived > base_io_t
 Base I/O type alias for CRTP.

Public Member Functions

 io ()=default
 Default constructor.
 io (IProtocol *protocol) noexcept
 Constructor with an initial protocol instance.
 io (io const &)=delete
 Deleted copy constructor.
 ~io () noexcept
 Destructor.
template<typename _Protocol, typename... _Args>
_Protocol * switch_protocol (_Args &&...args)
 Switches to a new protocol for I/O processing, taking ownership.
void clear_protocols ()
 Clears all owned protocol instances.
IProtocolprotocol ()
 Gets a pointer to the current active protocol instance.
void start () noexcept
 Starts bidirectional asynchronous I/O operations.
void ready_to_read () noexcept
 Ensures the I/O watcher is listening for read events (EV_READ).
void ready_to_write () noexcept
 Ensures the I/O watcher is listening for write events (EV_WRITE).
void close_after_deliver () const noexcept
 Requests connection closure after all pending output data is delivered.
template<typename... _Args>
auto & publish (_Args &&...args) noexcept
 Publishes data to the output buffer and ensures write readiness.
template<typename T>
auto & operator<< (T &&data)
 Stream operator for publishing data.
void disconnect (int reason=1)
 Initiates a graceful disconnection.

Static Public Attributes

static constexpr const bool has_server = false
 Indicates this component is not inherently a server.

Protected Member Functions

void dispose ()
 Disposes of resources and finalizes disconnection for the I/O component.
Protected Member Functions inherited from qb::io::async::base< io< _Derived >, event::io >
 base ()
 Constructor that registers the event watcher with the current listener.
 ~base ()
 Destructor that unregisters the event watcher.

Additional Inherited Members

Protected Attributes inherited from qb::io::async::base< io< _Derived >, event::io >
event::io & _async_event
 Reference to the registered libev-based event watcher.

Detailed Description

template<typename _Derived>
class qb::io::async::io< _Derived >

CRTP base class for managing bidirectional asynchronous I/O operations with protocol processing.

This template class combines input and output capabilities. It uses a single event::io watcher to monitor a file descriptor for both read and write readiness. Data is read into an input buffer and processed by a protocol, while outgoing data is buffered and written to the transport when ready.

Template Parameters
_DerivedThe derived class type (CRTP pattern) that provides the transport and handles protocol messages and I/O events.

Constructor & Destructor Documentation

◆ io() [1/2]

template<typename _Derived>
qb::io::async::io< _Derived >::io ( )
default

Default constructor.

Initializes the I/O component without a specific protocol.

◆ io() [2/2]

template<typename _Derived>
qb::io::async::io< _Derived >::io ( IProtocol * protocol)
inlinenoexcept

Constructor with an initial protocol instance.

Parameters
protocolPointer to an existing protocol instance. This io component will use it and take ownership by adding it to its internal list of protocols.

◆ ~io()

template<typename _Derived>
qb::io::async::io< _Derived >::~io ( )
inlinenoexcept

Destructor.

Cleans up owned protocols via clear_protocols(). Base class handles watcher unregistration.

Member Function Documentation

◆ switch_protocol()

template<typename _Derived>
template<typename _Protocol, typename... _Args>
_Protocol * qb::io::async::io< _Derived >::switch_protocol ( _Args &&... args)
inline

Switches to a new protocol for I/O processing, taking ownership.

Template Parameters
_ProtocolThe concrete AProtocol type to instantiate.
_ArgsArgument types for the _Protocol constructor.
Parameters
argsArguments to forward to the _Protocol constructor.
Returns
Pointer to the new protocol if successful, nullptr otherwise.

◆ clear_protocols()

template<typename _Derived>
void qb::io::async::io< _Derived >::clear_protocols ( )
inline

Clears all owned protocol instances.

Deletes protocols in _protocol_list and resets _protocol pointer to nullptr.

◆ protocol()

template<typename _Derived>
IProtocol * qb::io::async::io< _Derived >::protocol ( )
inline

Gets a pointer to the current active protocol instance.

Returns
Pointer to the IProtocol for message parsing/formatting.

◆ start()

template<typename _Derived>
void qb::io::async::io< _Derived >::start ( )
inlinenoexcept

Starts bidirectional asynchronous I/O operations.

Sets the transport to non-blocking and starts listening for read events (EV_READ). Resets disconnection reason.

◆ ready_to_read()

template<typename _Derived>
void qb::io::async::io< _Derived >::ready_to_read ( )
inlinenoexcept

Ensures the I/O watcher is listening for read events (EV_READ).

Modifies the event watcher flags to include EV_READ if not already set.

◆ ready_to_write()

template<typename _Derived>
void qb::io::async::io< _Derived >::ready_to_write ( )
inlinenoexcept

Ensures the I/O watcher is listening for write events (EV_WRITE).

Modifies the event watcher flags to include EV_WRITE if not already set.

◆ close_after_deliver()

template<typename _Derived>
void qb::io::async::io< _Derived >::close_after_deliver ( ) const
inlinenoexcept

Requests connection closure after all pending output data is delivered.

Marks the current protocol as invalid (_protocol->not_ok()). The on(event::io&) handler will then initiate dispose() after successfully writing any remaining buffered output if the protocol is not ok and output buffer is empty.

◆ publish()

template<typename _Derived>
template<typename... _Args>
auto & qb::io::async::io< _Derived >::publish ( _Args &&... args)
inlinenoexcept

Publishes data to the output buffer and ensures write readiness.

Template Parameters
_ArgsTypes of data to publish.
Parameters
argsData arguments to stream into _Derived::out().
Returns
Reference to _Derived::out() buffer.

◆ operator<<()

template<typename _Derived>
template<typename T>
auto & qb::io::async::io< _Derived >::operator<< ( T && data)
inline

Stream operator for publishing data.

Template Parameters
TType of data.
Parameters
dataData to publish.
Returns
Reference to _Derived::out() buffer.

◆ disconnect()

template<typename _Derived>
void qb::io::async::io< _Derived >::disconnect ( int reason = 1)
inline

Initiates a graceful disconnection.

Parameters
reasonOptional reason code for disconnection.

Triggers the dispose() mechanism via an EV_UNDEF event sent to the event loop.

◆ dispose()

template<typename _Derived>
void qb::io::async::io< _Derived >::dispose ( )
inlineprotected

Disposes of resources and finalizes disconnection for the I/O component.

Ensures cleanup happens only once (via _is_disposed). Triggers _Derived::on(event::disconnected&) (with _reason) or _Derived::on(event::dispose&) based on derived class capabilities and server association. This is the primary cleanup point before the async::base destructor unregisters the watcher.