StormByte C++ Library: Network module 0.0.9999
StormByte-Network is a StormByte library module for handling and create network connections
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
StormByte::Network::Transport::Packet Class Referenceabstract

Base class for wire-level packets. More...

#include <packet.hxx>

Public Types

using OpcodeType = unsigned short
 The type of the opcode.
 

Public Member Functions

 Packet (const Packet &other)=default
 Copy constructor.
 
 Packet (Packet &&other) noexcept=default
 Move constructor.
 
virtual ~Packet () noexcept=default
 Virtual destructor.
 
Packetoperator= (const Packet &other)=default
 Copy assignment operator.
 
Packetoperator= (Packet &&other) noexcept=default
 Move assignment operator.
 
const OpcodeTypeOpcode () const noexcept
 Retrieve the packet opcode.
 
Buffer::FIFO Serialize () const noexcept
 Serialize the packet to a byte buffer.
 

Static Public Attributes

static constexpr unsigned short PROCESS_THRESHOLD = 10
 Threshold for processing packets.
 

Protected Member Functions

constexpr Packet (const OpcodeType &opcode) noexcept
 Protected ctor.
 
virtual Buffer::DataType DoSerialize () const noexcept=0
 Packet-specific serialization hook.
 

Protected Attributes

OpcodeType m_opcode
 The opcode of the packet.
 

Detailed Description

Base class for wire-level packets.

Packet provides a minimal, polymorphic representation of an application protocol packet. It stores an opcode value (see OpcodeType) and exposes a protected serialization hook that derived classes override to append their payload bytes. The base Serialize() implementation serializes the opcode and then appends the result of DoSerialize().

Usage
  • Implementers should store packet-specific fields as members in their derived class and override DoSerialize() to return the on-wire representation of the packet payload (excluding the opcode).
  • Callers should use Serialize() to obtain the complete byte buffer for transport.
Note
  • OpcodeType is the integral type used to store opcode values (currently unsigned short). When constructing derived packets you may use your own enum types, provided their values are convertible to OpcodeType and fall within its non-negative representable range (no negative values and not greater than the maximum representable value of OpcodeType, e.g. 65535 for unsigned short). Avoid targeting this base's OpcodeType with signed enums that may contain negative values.
  • If you require runtime polymorphism across different opcode enum types (for example storing packets whose opcodes come from distinct enums in the same container), introduce a non-templated PacketBase interface in your project and have your Packet-derived types inherit from it. That interface should declare the non-templated virtual API you need (for example Serialize() / DoSerialize() returning Buffer::FIFO).

Constructor & Destructor Documentation

◆ Packet() [1/3]

StormByte::Network::Transport::Packet::Packet ( const Packet other)
default

Copy constructor.

Performs a member-wise copy of the opcode and any other members provided by derived classes. If a derived class contains non-copyable resources it should provide its own copy constructor.

◆ Packet() [2/3]

StormByte::Network::Transport::Packet::Packet ( Packet &&  other)
defaultnoexcept

Move constructor.

Move-constructs members. Marked noexcept to allow efficient moves in containers; override this if derived classes require different move semantics.

◆ ~Packet()

virtual StormByte::Network::Transport::Packet::~Packet ( )
virtualdefaultnoexcept

Virtual destructor.

Virtual to allow derived types to be destroyed through base pointers. The class remains abstract because DoSerialize() is pure virtual.

◆ Packet() [3/3]

constexpr StormByte::Network::Transport::Packet::Packet ( const OpcodeType opcode)
inlineconstexprprotectednoexcept

Protected ctor.

Construct a packet with the given opcode. Protected so that only derived classes may create concrete packets.

Parameters
opcodeThe packet opcode.

Member Function Documentation

◆ DoSerialize()

virtual Buffer::DataType StormByte::Network::Transport::Packet::DoSerialize ( ) const
protectedpure virtualnoexcept

Packet-specific serialization hook.

Derived classes must override this method to provide their specific on-wire payload serialization (excluding the opcode). The base Serialize() implementation calls this method and appends its result after serializing the opcode.

Returns
A Buffer::DataType containing the serialized payload.

◆ Opcode()

const OpcodeType & StormByte::Network::Transport::Packet::Opcode ( ) const
inlinenoexcept

Retrieve the packet opcode.

Returns a reference to the stored opcode. The value is stable for the lifetime of the object.

Returns
Constant reference to the opcode.

◆ operator=() [1/2]

Packet & StormByte::Network::Transport::Packet::operator= ( const Packet other)
default

Copy assignment operator.

Defaulted; performs member-wise assignment. Derived types that manage non-copyable state should provide their own assignment operator.

◆ operator=() [2/2]

Packet & StormByte::Network::Transport::Packet::operator= ( Packet &&  other)
defaultnoexcept

Move assignment operator.

Defaulted and marked noexcept; moves member state where possible. Override in derived classes if different semantics are required.

◆ Serialize()

Buffer::FIFO StormByte::Network::Transport::Packet::Serialize ( ) const
noexcept

Serialize the packet to a byte buffer.

Serializes the packet by first writing the opcode as OpcodeType, followed by the result of DoSerialize().

Returns
A Buffer::FIFO containing the serialized packet.

Member Data Documentation

◆ PROCESS_THRESHOLD

constexpr unsigned short StormByte::Network::Transport::Packet::PROCESS_THRESHOLD = 10
staticconstexpr

Threshold for processing packets.

Packets with payload sizes equal to or above this threshold will be processed (e.g., compressed/encrypted) when being serialized into frames. Packets with smaller payloads will be transmitted unprocessed. The threshold for processing packets.


The documentation for this class was generated from the following file: