R-Type  2
Doom but in better
Loading...
Searching...
No Matches
GUI::Network::Packet Class Reference

Represents a packet for the custom binary UDP protocol. More...

#include <ProtocolHandler.hpp>

Inheritance diagram for GUI::Network::Packet:
Inheritance graph
Collaboration diagram for GUI::Network::Packet:
Collaboration graph

Public Member Functions

 Packet (MessageType type=MessageType::P_ERROR, const std::vector< uint8_t > &payload={})
 Constructs a Packet with a specified message type and payload.
 
 ~Packet ()=default
 Destructor for the Packet class.
 
MessageType getType () const
 Gets the message type of the packet.
 
uint16_t getSize () const
 Gets the size of the packet's payload.
 
const std::vector< uint8_t > & getPayload () const
 Gets the payload of the packet.
 
const std::string print () const
 Prints a string representation of the packet's contents, for debugging purposes.
 
- Public Member Functions inherited from GUI::ECS::EntityNode
 EntityNode (const size_t entityId=0)
 
virtual ~EntityNode ()=default
 
virtual size_t getEntityNodeId () const
 

Static Public Member Functions

static std::vector< uint8_t > serialize (const Packet &packet)
 Serializes a Packet object into a vector of bytes suitable for network transmission.
 
static Packet deserialize (const std::vector< uint8_t > &data)
 Deserializes a packet from a vector of bytes received over the network.
 

Additional Inherited Members

- Protected Attributes inherited from GUI::ECS::EntityNode
size_t _entityID
 

Detailed Description

Represents a packet for the custom binary UDP protocol.

The Packet class defines a network packet with a message type, payload, and size. It offers functions to serialize and deserialize packets to and from a byte array for easy transmission over UDP. The supported message types include connection, disconnection, movement, shooting, entity spawning, updates, and error messages.

Definition at line 45 of file ProtocolHandler.hpp.

Constructor & Destructor Documentation

◆ Packet()

GUI::Network::Packet::Packet ( MessageType type = MessageType::P_ERROR,
const std::vector< uint8_t > & payload = {} )

Constructs a Packet with a specified message type and payload.

Parameters
typeThe type of message for the packet (default is ERROR).
payloadThe data to be included in the packet as the payload (optional, defaults to empty).

Definition at line 15 of file ProtocolHandler.cpp.

◆ ~Packet()

GUI::Network::Packet::~Packet ( )
default

Destructor for the Packet class.

The destructor is defined as default, as there is no dynamic memory allocation needing special cleanup.

Member Function Documentation

◆ deserialize()

GUI::Network::Packet GUI::Network::Packet::deserialize ( const std::vector< uint8_t > & data)
static

Deserializes a packet from a vector of bytes received over the network.

This method reconstructs a Packet object from its binary representation, extracting the type, size, and payload from the byte array.

Parameters
dataThe vector of bytes representing a serialized packet.
Returns
A Packet object constructed from the deserialized data.
Exceptions
std::runtime_errorIf the packet is invalid (e.g., incorrect size).

Definition at line 46 of file ProtocolHandler.cpp.

◆ getPayload()

const std::vector< uint8_t > & GUI::Network::Packet::getPayload ( ) const

Gets the payload of the packet.

Returns
A reference to the vector of bytes representing the packet's payload.

Definition at line 30 of file ProtocolHandler.cpp.

◆ getSize()

uint16_t GUI::Network::Packet::getSize ( ) const

Gets the size of the packet's payload.

Returns
The size of the payload in bytes.

Definition at line 25 of file ProtocolHandler.cpp.

◆ getType()

GUI::Network::MessageType GUI::Network::Packet::getType ( ) const

Gets the message type of the packet.

Returns
The type of message this packet represents (e.g., CONNECT, DISCONNECT, etc.).

Definition at line 20 of file ProtocolHandler.cpp.

◆ print()

const std::string GUI::Network::Packet::print ( ) const

Prints a string representation of the packet's contents, for debugging purposes.

The printed output includes the packet's type, size, and a list of the payload bytes.

Returns
A string containing a human-readable description of the packet.

Definition at line 62 of file ProtocolHandler.cpp.

◆ serialize()

std::vector< uint8_t > GUI::Network::Packet::serialize ( const Packet & packet)
static

Serializes a Packet object into a vector of bytes suitable for network transmission.

The serialized format includes:

  • 1 byte for the message type.
  • 2 bytes for the payload size (high byte, low byte).
  • The payload itself (a sequence of bytes).
Parameters
packetThe packet to serialize.
Returns
A vector of bytes representing the serialized packet.

Definition at line 35 of file ProtocolHandler.cpp.


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