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

CRTP base class for managing asynchronous input operations with protocol processing. More...

#include <io.h>

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

Public Types

using base_io_t = input<_Derived>
 Base I/O type alias for CRTP.

Public Member Functions

 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.
template<typename _Protocol, typename... _Args>
_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.

Static Public Attributes

static constexpr const bool has_server = false
 Indicates this component is not inherently a server (e.g., an acceptor).

Protected Member Functions

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.

Additional Inherited Members

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>
class qb::io::async::input< _Derived >

CRTP base class for managing asynchronous input operations with protocol processing.

This template class provides functionality for asynchronous input. It uses an event::io watcher to monitor a file descriptor for read readiness. Data read from the transport (provided by _Derived::transport()) is processed using an AProtocol instance.

Template Parameters
_DerivedThe derived class type (CRTP pattern) that provides the transport (e.g., a socket wrapper) and handles protocol messages and I/O events.

Constructor & Destructor Documentation

◆ input() [1/2]

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

Default constructor.

Initializes the input component without a specific protocol. A protocol should be set using switch_protocol() before starting operations that require one.

◆ input() [2/2]

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

Constructor with an initial protocol instance.

Parameters
protocolPointer to an existing protocol instance. This input component will use it and take ownership by adding it to its internal list of protocols for cleanup.
Note
If the protocol is managed externally, consider setting it via set_protocol_no_ownership (if such a method existed) or ensure clear_protocols is not called if it would delete an externally managed protocol. Currently, this constructor implies ownership.

◆ ~input()

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

Destructor.

Calls clear_protocols() to clean up all protocol instances owned by this component. The base class destructor will handle unregistering the event::io watcher.

Member Function Documentation

◆ switch_protocol()

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

Switches to a new protocol for processing input, taking ownership of the new protocol.

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 newly created and activated protocol instance if successful (protocol's ok() returns true), otherwise nullptr (and the created protocol instance is deleted).

The new protocol instance is added to an internal list and will be cleaned up by this input component's destructor. The current active protocol is set to this new instance. Previous protocols in the list are not deleted by this call.

◆ clear_protocols()

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

Clears all owned protocol instances.

Deletes all protocol instances stored in the internal _protocol_list and resets the current _protocol pointer to nullptr. This is called automatically by the destructor.

◆ protocol()

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

Gets a pointer to the current active protocol instance.

Returns
Pointer to the IProtocol currently in use for message parsing, or nullptr if no protocol is set.

◆ start()

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

Starts asynchronous input operations.

Sets the underlying transport (obtained via _Derived::transport()) to non-blocking mode and starts the event::io watcher to listen for read events (EV_READ). Resets any previous disconnection reason (_reason = 0).

◆ ready_to_read()

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

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

If the internal event::io watcher (from the base class) is not currently set to listen for EV_READ, this method reconfigures and restarts it to include EV_READ in its watched events. This is useful if read operations were temporarily paused.

◆ disconnect()

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

Initiates a graceful disconnection of the input component.

Parameters
reasonAn optional integer code indicating the reason for disconnection (e.g., user action, protocol error). This value will be passed in the event::disconnected if that event is handled by the derived class.

Sets an internal flag (_reason) with the provided reason and feeds an EV_UNDEF event to the listener. This typically causes the on(event::io&) handler to enter its error path during the next event loop cycle, leading to the invocation of the dispose() method for cleanup.

◆ dispose()

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

Disposes of resources and finalizes disconnection for the input component.

This method is called internally when an I/O error occurs or when disconnect() is explicitly initiated. It ensures cleanup happens only once by checking the _is_disposed flag. If _Derived implements on(event::disconnected&), this method is called with the stored _reason. If _Derived::has_server is true (typically for server-side sessions), it notifies the server of the disconnection. Otherwise, if _Derived implements on(event::dispose&), that method is called as a final cleanup hook. The base class (async::base) destructor will handle unregistering the event::io watcher.