qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::protocol::base::size_as_header< _IO_, _Size > Class Template Reference

Protocol for messages where the payload is preceded by a fixed-size header indicating its length. More...

#include <base.h>

Inheritance diagram for qb::protocol::base::size_as_header< _IO_, _Size >:
Collaboration diagram for qb::protocol::base::size_as_header< _IO_, _Size >:

Public Member Functions

 size_as_header ()=delete
 Default constructor.
 size_as_header (_IO_ &io) noexcept
 Constructor with I/O reference.
std::size_t shiftSize () const noexcept
 Returns the size of the header.
std::size_t getMessageSize () noexcept final
 Determines the size of the next complete message.
void reset () noexcept final
 Resets the protocol state.
Public Member Functions inherited from qb::io::async::IProtocol
virtual ~IProtocol ()=default
 Virtual destructor.
bool ok () const noexcept
 Checks if the protocol is in a valid operational state.
void not_ok () noexcept
 Marks the protocol as being in an invalid or non-operational state.
void set_should_flush (bool should_flush) noexcept
 Sets the flag indicating whether the protocol should flush the input buffer after processing a message.
bool should_flush () const noexcept
 Gets the flag indicating whether the protocol should flush the input buffer after processing a message.

Static Public Member Functions

static _Size Header (std::size_t size) noexcept
 Creates a size header for a message.

Additional Inherited Members

Protected Member Functions inherited from qb::io::async::AProtocol< _IO_ >
 AProtocol ()=delete
 Default constructor is deleted to ensure an I/O component is always associated.
 AProtocol (_IO_ &io) noexcept
 Constructor that associates the protocol with an I/O component.
virtual ~AProtocol ()=default
 Virtual destructor.
virtual void onMessage (std::size_t size) noexcept=0
 Processes a complete message from the input buffer of the associated I/O component.
Protected Attributes inherited from qb::io::async::AProtocol< _IO_ >
_IO_ & _io
 Reference to the I/O component instance that this protocol is associated with.

Detailed Description

template<typename _IO_, typename _Size = uint16_t>
class qb::protocol::base::size_as_header< _IO_, _Size >

Protocol for messages where the payload is preceded by a fixed-size header indicating its length.

This class implements a protocol where each message payload is prefixed by an integer header (e.g., uint8_t, uint16_t, uint32_t) that specifies the size of the upcoming payload. It handles network byte order conversion (e.g., ntohs, ntohl) for 16-bit and 32-bit headers. It inherits from qb::io::async::AProtocol.

Template Parameters
_IO_The I/O component type that will use this protocol. It needs to provide an in() method for input buffer access.
_SizeThe integer type of the size header (e.g., uint16_t, uint32_t). Defaults to uint16_t.

Constructor & Destructor Documentation

◆ size_as_header()

template<typename _IO_, typename _Size = uint16_t>
qb::protocol::base::size_as_header< _IO_, _Size >::size_as_header ( _IO_ & io)
inlinenoexcept

Constructor with I/O reference.

Parameters
ioReference to the I/O object that uses this protocol

Member Function Documentation

◆ shiftSize()

template<typename _IO_, typename _Size = uint16_t>
std::size_t qb::protocol::base::size_as_header< _IO_, _Size >::shiftSize ( ) const
inlinenoexcept

Returns the size of the header.

Returns
Header size in bytes

◆ getMessageSize()

template<typename _IO_, typename _Size = uint16_t>
std::size_t qb::protocol::base::size_as_header< _IO_, _Size >::getMessageSize ( )
inlinefinalvirtualnoexcept

Determines the size of the next complete message.

First reads the header to determine the message size, then waits for the complete message to be available.

Returns
Size of the message if complete, 0 otherwise

Implements qb::io::async::AProtocol< _IO_ >.

◆ Header()

template<typename _IO_, typename _Size = uint16_t>
_Size qb::protocol::base::size_as_header< _IO_, _Size >::Header ( std::size_t size)
inlinestaticnoexcept

Creates a size header for a message.

Converts the message size into an appropriate header, with endianness conversion if necessary.

Parameters
sizeSize of the message
Returns
Formatted size header

◆ reset()

template<typename _IO_, typename _Size = uint16_t>
void qb::protocol::base::size_as_header< _IO_, _Size >::reset ( )
inlinefinalvirtualnoexcept

Resets the protocol state.

Implements qb::io::async::AProtocol< _IO_ >.