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

Protocol for parsing null-terminated JSON messages. More...

#include <json.h>

Inheritance diagram for qb::protocol::json< IO_ >:
Collaboration diagram for qb::protocol::json< IO_ >:

Classes

struct  message
 Structure representing a JSON message. More...

Public Member Functions

 json ()=delete
 Default constructor (deleted)
 json (IO_ &io) noexcept
 Constructor with I/O reference.
void onMessage (std::size_t size) noexcept final
 Process a received message.
Public Member Functions inherited from qb::protocol::base::byte_terminated< IO_, '\0'>
 byte_terminated ()=delete
 Default constructor (deleted)
virtual ~byte_terminated ()=default
 Virtual destructor.
std::size_t shiftSize (std::size_t const size) const noexcept
 Calculates the message size without the delimiter.
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.

Additional Inherited Members

Static Public Attributes inherited from qb::protocol::base::byte_terminated< IO_, '\0'>
static constexpr const std::size_t delimiter_size
 Delimiter size (1 byte)
static constexpr const char end
 End character.
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.
virtual ~AProtocol ()=default
 Virtual destructor.
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_>
class qb::protocol::json< IO_ >

Protocol for parsing null-terminated JSON messages.

This class implements a protocol to handle JSON messages that are expected to be terminated by a NULL character (‘’\0'). It uses the qb::protocol::base::byte_terminated protocol as its base and parses the received data (excluding the terminator) as a JSON object using nlohmann::json`.

The onMessage method, when invoked by the framework, will provide a message struct containing the raw data, its size, and the parsed nlohmann::json object to the associated I/O component's handler.

Template Parameters
IO_The I/O component type (e.g., a TCP session class) that will use this protocol. It must be compatible with base::byte_terminated.

Constructor & Destructor Documentation

◆ json()

template<typename IO_>
qb::protocol::json< IO_ >::json ( IO_ & io)
inlineexplicitnoexcept

Constructor with I/O reference.

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

Member Function Documentation

◆ onMessage()

template<typename IO_>
void qb::protocol::json< IO_ >::onMessage ( std::size_t size)
inlinefinalvirtualnoexcept

Process a received message.

This method is called when a complete message is received. It builds a message object with the parsed JSON and passes it to the I/O object.

Parameters
sizeMessage size with the delimiter

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