VOX
A little voxel engine
Loading...
Searching...
No Matches
IPacket Class Referenceabstract

an abstract class that servers as a base for all network packets More...

#include <IPacket.hpp>

Inheritance diagram for IPacket:
Inheritance graph
Collaboration diagram for IPacket:
Collaboration graph

Public Types

enum class  Type : uint32_t {
  CONNECTION , PLAYER_CONNECTED , PLAYER_MOVE , DISCONNECT ,
  BLOCK_ACTION , PING , PLAYER_LIST , CHUNK ,
  CHUNK_REQUEST , CHUNK_UNLOAD , LOAD_DISTANCE , ENUM_MAX ,
  ENUM_MIN = 0
}
 

Public Member Functions

virtual ~IPacket ()
 
 IPacket (const IPacket &)
 
IPacketoperator= (const IPacket &)
 
 IPacket (IPacket &&)
 
IPacketoperator= (IPacket &&)
 
virtual void Serialize (uint8_t *buffer) const =0
 will write the packet in the buffer, the buffer must at least be of size Size() More...
 
void ExtractMessage (Connection &connection)
 Extracts the implemented packet from the connection. More...
 
virtual uint32_t Size () const =0
 return the size of the packet in bytes, usually used to reserve the right amount of memory in the send buffer More...
 
virtual bool HasDynamicSize () const =0
 Tell if the packet has a dynamic size. More...
 
virtual enum Type GetType () const =0
 Get the Type of the packet. More...
 
virtual std::shared_ptr< IPacketClone () const =0
 Create a new instance of the packet. More...
 
uint64_t GetConnectionId () const
 Get the Connection Id ( eg source of the packet or destination if it's a response) More...
 
void SetConnectionId (uint64_t connection_id)
 Set the Connection Id ( eg source of the packet or destination if it's a response) More...
 

Static Public Attributes

static const uint32_t STATIC_HEADER_SIZE = sizeof(Type)
 
static const uint32_t DYNAMIC_HEADER_SIZE = sizeof(Type) + sizeof(size_t)
 

Protected Member Functions

virtual void Deserialize (const uint8_t *buffer)=0
 read the packet from the buffer, the buffer must at least be of size Size() More...
 
size_t SerializeHeader (uint8_t *buffer) const
 util function to serialize the header of the packet will serialize the type and the size of the packet if it has a dynamic size More...
 
 IPacket ()
 

Protected Attributes

uint64_t m_connection_id = 0
 

Detailed Description

an abstract class that servers as a base for all network packets

This class is used to define the interface of a packet, It has an enum for packet types, pure virtual methods to serialize and deserialize the packet, pure virtual methods to return the size of the packet and if that packet has a dynamic size

#Packet header A typical packet header in memory would look like this

[Type][Size ( if dynamic size)]

a whole packet in memory would look like this

[Header][Data]

The size returned by the Size() method is the size of the whole packet including the header

Some packet have dynamic size, for example a packet that contains a list of players, the size of the packet will depend on the number of players connected this add some obligations to the implementation and manipulation of the packet

  1. The Size() Method is no longer valid to call on an empty instance of the packet, it will only be called by a sender that wants to know the size he has to reserve in his send buffer
  2. The HasDynamicSize() method will return true
  3. The Serialize() method will have to write the size of the packet in the buffer
  4. The Deserialize() method will have to read the size of the packet from the buffer

Member Enumeration Documentation

◆ Type

enum class IPacket::Type : uint32_t
strong
Enumerator
CONNECTION 
PLAYER_CONNECTED 
PLAYER_MOVE 
DISCONNECT 
BLOCK_ACTION 
PING 
PLAYER_LIST 
CHUNK 
CHUNK_REQUEST 
CHUNK_UNLOAD 
LOAD_DISTANCE 
ENUM_MAX 
ENUM_MIN 

Constructor & Destructor Documentation

◆ ~IPacket()

IPacket::~IPacket ( )
virtual

◆ IPacket() [1/3]

IPacket::IPacket ( const IPacket other)

◆ IPacket() [2/3]

IPacket::IPacket ( IPacket &&  other)

◆ IPacket() [3/3]

IPacket::IPacket ( )
protected

Member Function Documentation

◆ Clone()

virtual std::shared_ptr< IPacket > IPacket::Clone ( ) const
pure virtual

◆ Deserialize()

virtual void IPacket::Deserialize ( const uint8_t *  buffer)
protectedpure virtual

read the packet from the buffer, the buffer must at least be of size Size()

Note
this is protected because the method the user should call is ExtractMessage
Parameters
buffer

Implemented in BlockActionPacket, ChunkPacket, ChunkRequestPacket, ChunkUnloadPacket, ConnectionPacket, DisconnectPacket, LoadDistancePacket, PingPacket, PlayerConnectedPacket, PlayerListPacket, and PlayerMovePacket.

◆ ExtractMessage()

void IPacket::ExtractMessage ( Connection connection)

Extracts the implemented packet from the connection.

Warning
the connection MUST have the complete packet in its read buffer this method should only be called by the PacketFactory

moreover, the connection's read Mutex MUST be locked by the caller

Parameters
connection

◆ GetConnectionId()

uint64_t IPacket::GetConnectionId ( ) const
inline

Get the Connection Id ( eg source of the packet or destination if it's a response)

Returns
uint64_t

◆ GetType()

virtual enum Type IPacket::GetType ( ) const
pure virtual

◆ HasDynamicSize()

virtual bool IPacket::HasDynamicSize ( ) const
pure virtual

◆ operator=() [1/2]

IPacket & IPacket::operator= ( const IPacket other)

◆ operator=() [2/2]

IPacket & IPacket::operator= ( IPacket &&  other)

◆ Serialize()

virtual void IPacket::Serialize ( uint8_t *  buffer) const
pure virtual

will write the packet in the buffer, the buffer must at least be of size Size()

Parameters
buffer

Implemented in BlockActionPacket, ChunkPacket, ChunkRequestPacket, ChunkUnloadPacket, ConnectionPacket, DisconnectPacket, LoadDistancePacket, PingPacket, PlayerConnectedPacket, PlayerListPacket, and PlayerMovePacket.

◆ SerializeHeader()

size_t IPacket::SerializeHeader ( uint8_t *  buffer) const
protected

util function to serialize the header of the packet will serialize the type and the size of the packet if it has a dynamic size

Parameters
buffer
Returns
size_t the number of bytes written

◆ SetConnectionId()

void IPacket::SetConnectionId ( uint64_t  connection_id)
inline

Set the Connection Id ( eg source of the packet or destination if it's a response)

Parameters
connection_id

◆ Size()

virtual uint32_t IPacket::Size ( ) const
pure virtual

return the size of the packet in bytes, usually used to reserve the right amount of memory in the send buffer

or to check if a connection contains a complete packet

Warning
if the packet has a dynamic size YOU CANNOT call this method to check if the packet is complete, you MUST read the size from the packet header.

However you can still use it on an instance of a packet that has some data in it to estimate the size of the packet

Returns
uint32_t

Implemented in BlockActionPacket, ChunkPacket, ChunkRequestPacket, ChunkUnloadPacket, ConnectionPacket, DisconnectPacket, LoadDistancePacket, PingPacket, PlayerConnectedPacket, PlayerListPacket, and PlayerMovePacket.

Member Data Documentation

◆ DYNAMIC_HEADER_SIZE

const uint32_t IPacket::DYNAMIC_HEADER_SIZE = sizeof(Type) + sizeof(size_t)
inlinestatic

◆ m_connection_id

uint64_t IPacket::m_connection_id = 0
protected

◆ STATIC_HEADER_SIZE

const uint32_t IPacket::STATIC_HEADER_SIZE = sizeof(Type)
inlinestatic

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