VOX
A little voxel engine
Loading...
Searching...
No Matches
IPacket.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include "Connection.hpp"
6
7struct HandleArgs;
8
40{
41public:
42
43 //ENUM_MAX SHOULD always be last element of the enum
44 //it is used for iteration
45 //you MUST NOT put custom values in the enum
46 // eg (CONNECTION = 1) that is forbidden
47 enum class Type : uint32_t
48 {
54 PING,
56 CHUNK,
61 ENUM_MIN = 0
62 };
63 const static inline uint32_t STATIC_HEADER_SIZE = sizeof(Type);
64 const static inline uint32_t DYNAMIC_HEADER_SIZE = sizeof(Type) + sizeof(size_t);
65
66 virtual ~IPacket();
67
68 IPacket(const IPacket&);
69 IPacket& operator=(const IPacket&);
72
78 virtual void Serialize(uint8_t * buffer) const = 0;
79
89 void ExtractMessage(Connection & connection);
90
104 virtual uint32_t Size() const = 0;
105
112 virtual bool HasDynamicSize() const = 0;
113
119 virtual enum Type GetType() const = 0;
120
126 virtual std::shared_ptr<IPacket> Clone() const = 0;
127
133 uint64_t GetConnectionId() const { return m_connection_id; }
134
140 void SetConnectionId(uint64_t connection_id) { m_connection_id = connection_id; }
141protected:
149 virtual void Deserialize(const uint8_t * buffer) = 0;
150
158 size_t SerializeHeader(uint8_t * buffer) const;
159
160 IPacket();
161 uint64_t m_connection_id = 0;
162private:
163
164};
a bufferised and thread safe RAII wrapper for a socket representing a connection.
Definition: Connection.hpp:29
an abstract class that servers as a base for all network packets
Definition: IPacket.hpp:40
static const uint32_t STATIC_HEADER_SIZE
Definition: IPacket.hpp:63
uint64_t GetConnectionId() const
Get the Connection Id ( eg source of the packet or destination if it's a response)
Definition: IPacket.hpp:133
Type
Definition: IPacket.hpp:48
virtual bool HasDynamicSize() const =0
Tell if the packet has a dynamic size.
virtual void Deserialize(const uint8_t *buffer)=0
read the packet from the buffer, the buffer must at least be of size Size()
IPacket()
Definition: IPacket.cpp:3
static const uint32_t DYNAMIC_HEADER_SIZE
Definition: IPacket.hpp:64
void ExtractMessage(Connection &connection)
Extracts the implemented packet from the connection.
Definition: IPacket.cpp:33
IPacket & operator=(const IPacket &)
Definition: IPacket.cpp:12
virtual enum Type GetType() const =0
Get the Type of the packet.
virtual ~IPacket()
Definition: IPacket.cpp:29
virtual std::shared_ptr< IPacket > Clone() const =0
Create a new instance of the packet.
uint64_t m_connection_id
Definition: IPacket.hpp:161
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 sen...
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 packe...
Definition: IPacket.cpp:41
void SetConnectionId(uint64_t connection_id)
Set the Connection Id ( eg source of the packet or destination if it's a response)
Definition: IPacket.hpp:140
virtual void Serialize(uint8_t *buffer) const =0
will write the packet in the buffer, the buffer must at least be of size Size()