1 #ifndef BLANK_NET_PACKET_HPP_
2 #define BLANK_NET_PACKET_HPP_
16 static constexpr std::uint32_t TAG = 0xFB1AB1AF;
18 static const char *Type2String(std::uint8_t) noexcept;
30 std::uint8_t reserved1;
31 std::uint8_t reserved2;
32 std::uint8_t reserved3;
35 static constexpr std::size_t MAX_PAYLOAD_LEN = 500 - sizeof(Header);
37 std::uint8_t payload[MAX_PAYLOAD_LEN];
40 void Tag() noexcept { header.tag = TAG; }
42 void Type(std::uint8_t t) noexcept { header.type = t; }
43 std::uint8_t Type() const noexcept { return header.type; }
44 const char *TypeString() const noexcept { return Type2String(Type()); }
52 void Write(const T &, size_t off) noexcept;
54 void Read(T &, size_t off) const noexcept;
56 void WriteString(const std::string &src, std::size_t off, std::size_t maxlen) noexcept;
57 void ReadString(std::string &dst, std::size_t off, std::size_t maxlen) const noexcept;
60 struct Ping : public Payload {
61 static constexpr std::uint8_t TYPE = 0;
62 static constexpr std::size_t MAX_LEN = 0;
65 struct Login : public Payload {
66 static constexpr std::uint8_t TYPE = 1;
67 static constexpr std::size_t MAX_LEN = 32;
69 void WritePlayerName(const std::string &) noexcept;
70 void ReadPlayerName(std::string &) const noexcept;
73 struct Join : public Payload {
74 static constexpr std::uint8_t TYPE = 2;
75 static constexpr std::size_t MAX_LEN = 100;
77 void WritePlayer(const Entity &) noexcept;
78 void ReadPlayerID(std::uint32_t &) const noexcept;
79 void ReadPlayer(Entity &) const noexcept;
80 void WriteWorldName(const std::string &) noexcept;
81 void ReadWorldName(std::string &) const noexcept;
84 struct Part : public Payload {
85 static constexpr std::uint8_t TYPE = 3;
86 static constexpr std::size_t MAX_LEN = 0;
89 struct PlayerUpdate : public Payload {
90 static constexpr std::uint8_t TYPE = 4;
91 static constexpr std::size_t MAX_LEN = 64;
93 void WritePlayer(const Entity &) noexcept;
94 void ReadPlayer(Entity &) const noexcept;
98 template<class PayloadType>
101 result.length = PayloadType::MAX_LEN;
102 result.data = &payload[0];
106 template<class PayloadType>
107 static PayloadType As(const UDPpacket &pack) {
109 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
110 result.data = pack.data + sizeof(Header);
114 template<class PayloadType>
115 static PayloadType Make(UDPpacket &udp_pack) {
116 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
118 pack.Type(PayloadType::TYPE);
120 udp_pack.len = sizeof(Header) + PayloadType::MAX_LEN;
123 result.length = PayloadType::MAX_LEN;
124 result.data = pack.payload;