1 #ifndef BLANK_NET_PACKET_HPP_
2 #define BLANK_NET_PACKET_HPP_
19 static constexpr std::uint32_t TAG = 0xFB1AB1AF;
21 static const char *Type2String(std::uint8_t) noexcept;
28 // true if this contains an ack for given (remote) seq
29 bool Acks(std::uint16_t) const noexcept;
30 std::uint16_t AckBegin() const noexcept { return ack; }
31 std::uint16_t AckEnd() const noexcept { return ack - std::uint16_t(33); }
38 std::uint8_t reserved1;
39 std::uint8_t reserved2;
40 std::uint8_t reserved3;
43 static constexpr std::size_t MAX_PAYLOAD_LEN = 500 - sizeof(Header);
45 std::uint8_t payload[MAX_PAYLOAD_LEN];
48 void Tag() noexcept { header.tag = TAG; }
50 void Type(std::uint8_t t) noexcept { header.type = t; }
51 std::uint8_t Type() const noexcept { return header.type; }
52 const char *TypeString() const noexcept { return Type2String(Type()); }
59 /// WARNING: do not use these if the data doesn not
60 /// point into a real packet's payload
61 const Packet &GetPacket() const noexcept {
62 return *reinterpret_cast<const Packet *>(data - sizeof(Header));
64 const Header &GetHeader() const noexcept {
65 return GetPacket().header;
67 std::uint16_t Seq() const noexcept {
68 return GetHeader().ctrl.seq;
72 void Write(const T &, size_t off) noexcept;
74 void Read(T &, size_t off) const noexcept;
76 void WriteString(const std::string &src, std::size_t off, std::size_t maxlen) noexcept;
77 void ReadString(std::string &dst, std::size_t off, std::size_t maxlen) const noexcept;
80 struct Ping : public Payload {
81 static constexpr std::uint8_t TYPE = 0;
82 static constexpr std::size_t MAX_LEN = 0;
85 struct Login : public Payload {
86 static constexpr std::uint8_t TYPE = 1;
87 static constexpr std::size_t MAX_LEN = 32;
89 void WritePlayerName(const std::string &) noexcept;
90 void ReadPlayerName(std::string &) const noexcept;
93 struct Join : public Payload {
94 static constexpr std::uint8_t TYPE = 2;
95 static constexpr std::size_t MAX_LEN = 100;
97 void WritePlayer(const Entity &) noexcept;
98 void ReadPlayerID(std::uint32_t &) const noexcept;
99 void ReadPlayerState(EntityState &) const noexcept;
100 void WriteWorldName(const std::string &) noexcept;
101 void ReadWorldName(std::string &) const noexcept;
104 struct Part : public Payload {
105 static constexpr std::uint8_t TYPE = 3;
106 static constexpr std::size_t MAX_LEN = 0;
109 struct PlayerUpdate : public Payload {
110 static constexpr std::uint8_t TYPE = 4;
111 static constexpr std::size_t MAX_LEN = 76;
113 void WritePredictedState(const EntityState &) noexcept;
114 void ReadPredictedState(EntityState &) const noexcept;
115 void WriteMovement(const glm::vec3 &) noexcept;
116 void ReadMovement(glm::vec3 &) const noexcept;
117 void WritePitch(float) noexcept;
118 void ReadPitch(float &) const noexcept;
119 void WriteYaw(float) noexcept;
120 void ReadYaw(float &) const noexcept;
121 void WriteActions(std::uint8_t) noexcept;
122 void ReadActions(std::uint8_t &) const noexcept;
123 void WriteSlot(std::uint8_t) noexcept;
124 void ReadSlot(std::uint8_t &) const noexcept;
127 struct SpawnEntity : public Payload {
128 static constexpr std::uint8_t TYPE = 5;
129 static constexpr std::size_t MAX_LEN = 132;
131 void WriteEntity(const Entity &) noexcept;
132 void ReadEntityID(std::uint32_t &) const noexcept;
133 void ReadSkeletonID(std::uint32_t &) const noexcept;
134 void ReadEntity(Entity &) const noexcept;
137 struct DespawnEntity : public Payload {
138 static constexpr std::uint8_t TYPE = 6;
139 static constexpr std::size_t MAX_LEN = 4;
141 void WriteEntityID(std::uint32_t) noexcept;
142 void ReadEntityID(std::uint32_t &) const noexcept;
145 struct EntityUpdate : public Payload {
146 static constexpr std::uint8_t TYPE = 7;
147 static constexpr std::size_t MAX_LEN = 480;
149 static constexpr std::uint32_t MAX_ENTITIES = 7;
150 static constexpr std::size_t GetSize(std::uint32_t num) noexcept {
151 return 4 + (num * 68);
154 void WriteEntityCount(std::uint32_t) noexcept;
155 void ReadEntityCount(std::uint32_t &) const noexcept;
157 void WriteEntity(const Entity &, std::uint32_t) noexcept;
158 void ReadEntityID(std::uint32_t &, std::uint32_t) const noexcept;
159 void ReadEntityState(EntityState &, std::uint32_t) const noexcept;
162 struct PlayerCorrection : public Payload {
163 static constexpr std::uint8_t TYPE = 8;
164 static constexpr std::size_t MAX_LEN = 66;
166 void WritePacketSeq(std::uint16_t) noexcept;
167 void ReadPacketSeq(std::uint16_t &) const noexcept;
168 void WritePlayer(const Entity &) noexcept;
169 void ReadPlayerState(EntityState &) const noexcept;
172 struct ChunkBegin : public Payload {
173 static constexpr std::uint8_t TYPE = 9;
174 static constexpr std::size_t MAX_LEN = 24;
176 void WriteTransmissionId(std::uint32_t) noexcept;
177 void ReadTransmissionId(std::uint32_t &) const noexcept;
178 void WriteFlags(std::uint32_t) noexcept;
179 void ReadFlags(std::uint32_t &) const noexcept;
180 void WriteChunkCoords(const glm::ivec3 &) noexcept;
181 void ReadChunkCoords(glm::ivec3 &) const noexcept;
182 void WriteDataSize(std::uint32_t) noexcept;
183 void ReadDataSize(std::uint32_t &) const noexcept;
186 struct ChunkData : public Payload {
187 static constexpr std::uint8_t TYPE = 10;
188 static constexpr std::size_t MAX_LEN = MAX_PAYLOAD_LEN;
189 static constexpr std::size_t MAX_DATA_LEN = MAX_LEN - 12;
191 void WriteTransmissionId(std::uint32_t) noexcept;
192 void ReadTransmissionId(std::uint32_t &) const noexcept;
193 void WriteDataOffset(std::uint32_t) noexcept;
194 void ReadDataOffset(std::uint32_t &) const noexcept;
195 void WriteDataSize(std::uint32_t) noexcept;
196 void ReadDataSize(std::uint32_t &) const noexcept;
197 void WriteData(const std::uint8_t *, std::size_t len) noexcept;
198 void ReadData(std::uint8_t *, std::size_t maxlen) const noexcept;
201 struct BlockUpdate : public Payload {
202 static constexpr std::uint8_t TYPE = 11;
203 static constexpr std::size_t MAX_LEN = 484;
205 static constexpr std::uint32_t MAX_BLOCKS = 78;
206 static constexpr std::size_t GetSize(std::uint32_t num) noexcept {
207 return 16 + (num * 6);
210 void WriteChunkCoords(const glm::ivec3 &) noexcept;
211 void ReadChunkCoords(glm::ivec3 &) const noexcept;
212 void WriteBlockCount(std::uint32_t) noexcept;
213 void ReadBlockCount(std::uint32_t &) const noexcept;
215 void WriteIndex(std::uint16_t, std::uint32_t) noexcept;
216 void ReadIndex(std::uint16_t &, std::uint32_t) const noexcept;
217 void WriteBlock(const Block &, std::uint32_t) noexcept;
218 void ReadBlock(Block &, std::uint32_t) const noexcept;
222 template<class PayloadType>
225 result.length = PayloadType::MAX_LEN;
226 result.data = &payload[0];
230 template<class PayloadType>
231 static PayloadType As(const UDPpacket &pack) {
233 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
234 result.data = pack.data + sizeof(Header);
238 template<class PayloadType>
239 static PayloadType Make(UDPpacket &udp_pack) {
240 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
242 pack.Type(PayloadType::TYPE);
244 udp_pack.len = sizeof(Header) + PayloadType::MAX_LEN;
247 result.length = PayloadType::MAX_LEN;
248 result.data = pack.payload;