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;
32 static constexpr std::size_t MAX_PAYLOAD_LEN = 500 - sizeof(Header);
34 std::uint8_t payload[MAX_PAYLOAD_LEN];
37 void Tag() noexcept { header.tag = TAG; }
39 void Type(std::uint8_t t) noexcept { header.type = t; }
40 std::uint8_t Type() const noexcept { return header.type; }
41 const char *TypeString() const noexcept { return Type2String(Type()); }
49 void Write(const T &, size_t off) noexcept;
51 void Read(T &, size_t off) const noexcept;
53 void WriteString(const std::string &src, std::size_t off, std::size_t maxlen) noexcept;
54 void ReadString(std::string &dst, std::size_t off, std::size_t maxlen) const noexcept;
57 struct Ping : public Payload {
58 static constexpr std::uint8_t TYPE = 0;
59 static constexpr std::size_t MAX_LEN = 0;
62 struct Login : public Payload {
63 static constexpr std::uint8_t TYPE = 1;
64 static constexpr std::size_t MAX_LEN = 32;
66 void WritePlayerName(const std::string &) noexcept;
67 void ReadPlayerName(std::string &) const noexcept;
70 struct Join : public Payload {
71 static constexpr std::uint8_t TYPE = 2;
72 static constexpr std::size_t MAX_LEN = 100;
74 void WritePlayer(const Entity &) noexcept;
75 void ReadPlayer(Entity &) const noexcept;
76 void WriteWorldName(const std::string &) noexcept;
77 void ReadWorldName(std::string &) const noexcept;
80 struct Part : public Payload {
81 static constexpr std::uint8_t TYPE = 3;
82 static constexpr std::size_t MAX_LEN = 0;
86 template<class PayloadType>
89 result.length = PayloadType::MAX_LEN;
90 result.data = &payload[0];
94 template<class PayloadType>
95 static PayloadType As(const UDPpacket &pack) {
97 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
98 result.data = pack.data + sizeof(Header);
102 template<class PayloadType>
103 static PayloadType Make(UDPpacket &udp_pack) {
104 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
106 pack.Type(PayloadType::TYPE);
108 udp_pack.len = sizeof(Header) + PayloadType::TYPE;
111 result.length = PayloadType::MAX_LEN;
112 result.data = pack.payload;