-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPacket.h
More file actions
59 lines (53 loc) · 1.38 KB
/
Copy pathPacket.h
File metadata and controls
59 lines (53 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include "Export.h"
#include "Core/ISerializable.h"
#include "Core/Factory.hpp"
#include "Core/UUID.h"
#include <cstdint>
#define SPACKET(PacketClass, id)\
struct PacketRegister##PacketClass\
{\
PacketRegister##PacketClass()\
{\
auto factory = Packet::Factory::GetInstance();\
if (!factory->HasKey(id))\
{\
factory->Register(id, \
[]()->std::unique_ptr<Packet>\
{\
auto packet = std::make_unique<PacketClass>(); \
return packet; \
}\
); \
}\
}\
~PacketRegister##PacketClass()\
{\
auto factory = Packet::Factory::GetInstance(); \
factory->UnRegister(id);\
}\
static auto Get() -> PacketRegister##PacketClass*\
{\
static PacketRegister##PacketClass registry{};\
return ®istry;\
}\
};\
static inline PacketRegister##PacketClass* _packetRegister = PacketRegister##PacketClass::Get();
namespace sh::network
{
class Packet : public core::ISerializable
{
public:
using Factory = core::Factory<Packet, std::unique_ptr<Packet>, uint32_t>;
static constexpr uint32_t MAX_PACKET_SIZE = 1024;
public:
virtual auto GetId() const -> uint32_t = 0;
SH_NET_API Packet();
SH_NET_API virtual ~Packet();
SH_NET_API auto GetPacketUUID() const -> const core::UUID&;
SH_NET_API auto Serialize() const->core::Json override;
SH_NET_API void Deserialize(const core::Json& json) override;
private:
core::UUID packetUUID;
};
}//namespace