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

CRTP base class for managing asynchronous output operations. More...

#include <io.h>

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

Public Types

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

Public Member Functions

 output ()=default
 Default constructor.
 output (output const &)=delete
 Deleted copy constructor to prevent unintended copying of I/O state and resources.
 ~output ()=default
 Destructor.
void start () noexcept
 Starts asynchronous output operations.
void ready_to_write () noexcept
 Ensures the I/O watcher is listening for write events (EV_WRITE).
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, equivalent to publish(std::forward<T>(data)).
void disconnect (int reason=1)
 Initiates a graceful disconnection of the output component.

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 output component.
Protected Member Functions inherited from qb::io::async::base< output< _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< output< _Derived >, event::io >
event::io & _async_event
 Reference to the registered libev-based event watcher.

Detailed Description

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

CRTP base class for managing asynchronous output operations.

This template class provides functionality for asynchronous output. It uses an event::io watcher to monitor a file descriptor for write readiness. Data to be sent is buffered and then written to the transport (provided by _Derived::transport()) when ready.

Template Parameters
_DerivedThe derived class type (CRTP pattern) that provides the transport and handles I/O events like eos (end of stream) or pending_write.

Constructor & Destructor Documentation

◆ ~output()

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

Destructor.

The base class destructor will handle unregistering the event::io watcher.

Member Function Documentation

◆ start()

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

Starts asynchronous output operations.

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

◆ ready_to_write()

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

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

If the internal event::io watcher (from the base class) is not currently set to listen for EV_WRITE, this method reconfigures it to include EV_WRITE in its watched events. This is often called implicitly by publish() or operator<< to signal that there is data ready to be written.

◆ publish()

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

Publishes data to the output buffer and ensures write readiness.

Template Parameters
_ArgsVariadic template arguments for the data to be published.
Parameters
argsData arguments to stream into _Derived::out() buffer (typically a qb::allocator::pipe<char>).
Returns
A reference to the _Derived::out() buffer after the data has been added.

Calls ready_to_write() to ensure the event loop is monitoring for write readiness, then streams all args into the output buffer provided by _Derived::out().

◆ operator<<()

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

Stream operator for publishing data, equivalent to publish(std::forward<T>(data)).

Template Parameters
TType of data to publish.
Parameters
dataData to publish.
Returns
A reference to the _Derived::out() buffer.
See also
publish()

◆ disconnect()

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

Initiates a graceful disconnection of the output component.

Parameters
reasonAn optional integer code indicating the reason for disconnection.

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

◆ dispose()

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

Disposes of resources and finalizes disconnection for the output 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 _is_disposed. If _Derived implements on(event::disconnected&), this method is called with the stored _reason. If _Derived::has_server is true, it notifies the server. Otherwise, if _Derived implements on(event::dispose&), that method is called for final cleanup. The base class (async::base) destructor handles unregistering the event::io watcher.