1 #ifndef BLANK_NET_PACKET_HPP_
2 #define BLANK_NET_PACKET_HPP_
18 static constexpr std::uint32_t TAG = 0xFB1AB1AF;
20 static const char *Type2String(std::uint8_t) noexcept;
27 // true if this contains an ack for given (remote) seq
28 bool Acks(std::uint16_t) const noexcept;
29 std::uint16_t AckBegin() const noexcept { return ack; }
30 std::uint16_t AckEnd() const noexcept { return ack - std::uint16_t(33); }
37 std::uint8_t reserved1;
38 std::uint8_t reserved2;
39 std::uint8_t reserved3;
42 static constexpr std::size_t MAX_PAYLOAD_LEN = 500 - sizeof(Header);
44 std::uint8_t payload[MAX_PAYLOAD_LEN];
47 void Tag() noexcept { header.tag = TAG; }
49 void Type(std::uint8_t t) noexcept { header.type = t; }
50 std::uint8_t Type() const noexcept { return header.type; }
51 const char *TypeString() const noexcept { return Type2String(Type()); }
58 std::uint16_t Seq() const noexcept {
59 return reinterpret_cast<const Packet *>(data - sizeof(Header))->header.ctrl.seq;
63 void Write(const T &, size_t off) noexcept;
65 void Read(T &, size_t off) const noexcept;
67 void WriteString(const std::string &src, std::size_t off, std::size_t maxlen) noexcept;
68 void ReadString(std::string &dst, std::size_t off, std::size_t maxlen) const noexcept;
71 struct Ping : public Payload {
72 static constexpr std::uint8_t TYPE = 0;
73 static constexpr std::size_t MAX_LEN = 0;
76 struct Login : public Payload {
77 static constexpr std::uint8_t TYPE = 1;
78 static constexpr std::size_t MAX_LEN = 32;
80 void WritePlayerName(const std::string &) noexcept;
81 void ReadPlayerName(std::string &) const noexcept;
84 struct Join : public Payload {
85 static constexpr std::uint8_t TYPE = 2;
86 static constexpr std::size_t MAX_LEN = 100;
88 void WritePlayer(const Entity &) noexcept;
89 void ReadPlayerID(std::uint32_t &) const noexcept;
90 void ReadPlayerState(EntityState &) const noexcept;
91 void WriteWorldName(const std::string &) noexcept;
92 void ReadWorldName(std::string &) const noexcept;
95 struct Part : public Payload {
96 static constexpr std::uint8_t TYPE = 3;
97 static constexpr std::size_t MAX_LEN = 0;
100 struct PlayerUpdate : public Payload {
101 static constexpr std::uint8_t TYPE = 4;
102 static constexpr std::size_t MAX_LEN = 64;
104 void WritePlayer(const Entity &) noexcept;
105 void ReadPlayerState(EntityState &) const noexcept;
108 struct SpawnEntity : public Payload {
109 static constexpr std::uint8_t TYPE = 5;
110 static constexpr std::size_t MAX_LEN = 132;
112 void WriteEntity(const Entity &) noexcept;
113 void ReadEntityID(std::uint32_t &) const noexcept;
114 void ReadSkeletonID(std::uint32_t &) const noexcept;
115 void ReadEntity(Entity &) const noexcept;
118 struct DespawnEntity : public Payload {
119 static constexpr std::uint8_t TYPE = 6;
120 static constexpr std::size_t MAX_LEN = 4;
122 void WriteEntityID(std::uint32_t) noexcept;
123 void ReadEntityID(std::uint32_t &) const noexcept;
126 struct EntityUpdate : public Payload {
127 static constexpr std::uint8_t TYPE = 7;
128 static constexpr std::size_t MAX_LEN = 452;
130 static constexpr std::uint32_t MAX_ENTITIES = 7;
131 static constexpr std::size_t GetSize(std::uint32_t num) noexcept {
132 return 4 + (num * 64);
135 void WriteEntityCount(std::uint32_t) noexcept;
136 void ReadEntityCount(std::uint32_t &) const noexcept;
138 void WriteEntity(const Entity &, std::uint32_t) noexcept;
139 void ReadEntityID(std::uint32_t &, std::uint32_t) const noexcept;
140 void ReadEntityState(EntityState &, std::uint32_t) const noexcept;
143 struct PlayerCorrection : public Payload {
144 static constexpr std::uint8_t TYPE = 8;
145 static constexpr std::size_t MAX_LEN = 66;
147 void WritePacketSeq(std::uint16_t) noexcept;
148 void ReadPacketSeq(std::uint16_t &) const noexcept;
149 void WritePlayer(const Entity &) noexcept;
150 void ReadPlayerState(EntityState &) const noexcept;
153 struct ChunkBegin : public Payload {
154 static constexpr std::uint8_t TYPE = 9;
155 static constexpr std::size_t MAX_LEN = 24;
157 void WriteTransmissionId(std::uint32_t) noexcept;
158 void ReadTransmissionId(std::uint32_t &) const noexcept;
159 void WriteFlags(std::uint32_t) noexcept;
160 void ReadFlags(std::uint32_t &) const noexcept;
161 void WriteChunkCoords(const glm::ivec3 &) noexcept;
162 void ReadChunkCoords(glm::ivec3 &) const noexcept;
163 void WriteDataSize(std::uint32_t) noexcept;
164 void ReadDataSize(std::uint32_t &) const noexcept;
167 struct ChunkData : public Payload {
168 static constexpr std::uint8_t TYPE = 10;
169 static constexpr std::size_t MAX_LEN = MAX_PAYLOAD_LEN;
170 static constexpr std::size_t MAX_DATA_LEN = MAX_LEN - 12;
172 void WriteTransmissionId(std::uint32_t) noexcept;
173 void ReadTransmissionId(std::uint32_t &) const noexcept;
174 void WriteDataOffset(std::uint32_t) noexcept;
175 void ReadDataOffset(std::uint32_t &) const noexcept;
176 void WriteDataSize(std::uint32_t) noexcept;
177 void ReadDataSize(std::uint32_t &) const noexcept;
178 void WriteData(const std::uint8_t *, std::size_t len) noexcept;
179 void ReadData(std::uint8_t *, std::size_t maxlen) const noexcept;
183 template<class PayloadType>
186 result.length = PayloadType::MAX_LEN;
187 result.data = &payload[0];
191 template<class PayloadType>
192 static PayloadType As(const UDPpacket &pack) {
194 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
195 result.data = pack.data + sizeof(Header);
199 template<class PayloadType>
200 static PayloadType Make(UDPpacket &udp_pack) {
201 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
203 pack.Type(PayloadType::TYPE);
205 udp_pack.len = sizeof(Header) + PayloadType::MAX_LEN;
208 result.length = PayloadType::MAX_LEN;
209 result.data = pack.payload;