]> git.localhorst.tv Git - blank.git/blob - src/net/net.cpp
get rid of angular velocity
[blank.git] / src / net / net.cpp
1 #include "Connection.hpp"
2 #include "ConnectionHandler.hpp"
3 #include "io.hpp"
4 #include "Packet.hpp"
5
6 #include "../app/init.hpp"
7 #include "../model/Model.hpp"
8 #include "../world/Entity.hpp"
9 #include "../world/EntityState.hpp"
10
11 #include <cstring>
12
13 using namespace std;
14
15
16 namespace blank {
17
18 constexpr size_t Packet::Ping::MAX_LEN;
19 constexpr size_t Packet::Login::MAX_LEN;
20 constexpr size_t Packet::Join::MAX_LEN;
21 constexpr size_t Packet::Part::MAX_LEN;
22 constexpr size_t Packet::PlayerUpdate::MAX_LEN;
23 constexpr size_t Packet::SpawnEntity::MAX_LEN;
24 constexpr size_t Packet::DespawnEntity::MAX_LEN;
25 constexpr size_t Packet::EntityUpdate::MAX_LEN;
26 constexpr size_t Packet::PlayerCorrection::MAX_LEN;
27 constexpr size_t Packet::ChunkBegin::MAX_LEN;
28 constexpr size_t Packet::ChunkData::MAX_LEN;
29 constexpr size_t Packet::BlockUpdate::MAX_LEN;
30 constexpr size_t Packet::Message::MAX_LEN;
31 constexpr size_t Packet::Message::MAX_MESSAGE_LEN;
32
33 Connection::Connection(const IPaddress &addr)
34 : handler(nullptr)
35 , addr(addr)
36 , send_timer(500)
37 , recv_timer(10000)
38 , ctrl_out{ 0, 0xFFFF, 0xFFFFFFFF }
39 , ctrl_in{ 0, 0xFFFF, 0xFFFFFFFF }
40 , closed(false) {
41         send_timer.Start();
42         recv_timer.Start();
43 }
44
45 bool Connection::Matches(const IPaddress &remote) const noexcept {
46         return memcmp(&addr, &remote, sizeof(IPaddress)) == 0;
47 }
48
49 void Connection::FlagSend() noexcept {
50         send_timer.Reset();
51 }
52
53 void Connection::FlagRecv() noexcept {
54         recv_timer.Reset();
55 }
56
57 bool Connection::ShouldPing() const noexcept {
58         return !closed && send_timer.HitOnce();
59 }
60
61 bool Connection::TimedOut() const noexcept {
62         return recv_timer.HitOnce();
63 }
64
65 void Connection::Update(int dt) {
66         send_timer.Update(dt);
67         recv_timer.Update(dt);
68         if (TimedOut()) {
69                 Close();
70                 if (HasHandler()) {
71                         Handler().OnTimeout();
72                 }
73         }
74 }
75
76
77 uint16_t Connection::Send(UDPpacket &udp_pack, UDPsocket sock) {
78         Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
79         pack.header.ctrl = ctrl_out;
80         uint16_t seq = ctrl_out.seq++;
81
82         udp_pack.address = addr;
83         if (SDLNet_UDP_Send(sock, -1, &udp_pack) == 0) {
84                 throw NetError("SDLNet_UDP_Send");
85         }
86
87         FlagSend();
88         return seq;
89 }
90
91 void Connection::Received(const UDPpacket &udp_pack) {
92         Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
93
94         // ack to the remote
95         int16_t diff = int16_t(pack.header.ctrl.seq) - int16_t(ctrl_out.ack);
96         if (diff > 0) {
97                 if (diff >= 32) {
98                         ctrl_out.hist = 0;
99                 } else {
100                         ctrl_out.hist <<= diff;
101                         ctrl_out.hist |= 1 << (diff - 1);
102                 }
103         } else if (diff < 0 && diff >= -32) {
104                 ctrl_out.hist |= 1 << (-diff - 1);
105         }
106         ctrl_out.ack = pack.header.ctrl.seq;
107         FlagRecv();
108
109         if (!HasHandler()) {
110                 return;
111         }
112
113         Packet::TControl ctrl_new = pack.header.ctrl;
114         Handler().Handle(udp_pack);
115
116         if (diff > 0) {
117                 // if the packet holds more recent information
118                 // check if remote failed to ack one of our packets
119                 diff = int16_t(ctrl_new.ack) - int16_t(ctrl_in.ack);
120                 // should always be true, but you never know…
121                 if (diff > 0) {
122                         for (int i = 0; i < diff; ++i) {
123                                 if (i > 32 || (i < 32 && (ctrl_in.hist & (1 << (31 - i))) == 0)) {
124                                         Handler().PacketLost(ctrl_in.ack - 32 + i);
125                                 }
126                         }
127                 }
128                 // check for newly ack'd packets
129                 for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); --s) {
130                         if (ctrl_new.Acks(s) && !ctrl_in.Acks(s)) {
131                                 Handler().PacketReceived(s);
132                         }
133                 }
134                 ctrl_in = ctrl_new;
135         }
136 }
137
138 bool Packet::TControl::Acks(uint16_t s) const noexcept {
139         int16_t diff = int16_t(ack) - int16_t(s);
140         if (diff == 0) return true;
141         if (diff < 0 || diff > 32) return false;
142         return (hist & (1 << (diff - 1))) != 0;
143 }
144
145 uint16_t Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) {
146         Packet::Make<Packet::Ping>(udp_pack);
147         return Send(udp_pack, sock);
148 }
149
150
151 ConnectionHandler::ConnectionHandler()
152 : packets_lost(0)
153 , packets_received(0)
154 , packet_loss(0.0f) {
155
156 }
157
158 void ConnectionHandler::PacketLost(uint16_t seq) {
159         OnPacketLost(seq);
160         ++packets_lost;
161         UpdatePacketLoss();
162 }
163
164 void ConnectionHandler::PacketReceived(uint16_t seq) {
165         OnPacketReceived(seq);
166         ++packets_received;
167         UpdatePacketLoss();
168 }
169
170 void ConnectionHandler::UpdatePacketLoss() noexcept {
171         unsigned int packets_total = packets_lost + packets_received;
172         if (packets_total >= 256) {
173                 packet_loss = float(packets_lost) / float(packets_total);
174                 packets_lost = 0;
175                 packets_received = 0;
176         }
177 }
178
179
180 ostream &operator <<(ostream &out, const IPaddress &addr) {
181         const unsigned char *host = reinterpret_cast<const unsigned char *>(&addr.host);
182         out << int(host[0])
183                 << '.' << int(host[1])
184                 << '.' << int(host[2])
185                 << '.' << int(host[3]);
186         if (addr.port) {
187                 out << ':' << SDLNet_Read16(&addr.port);
188         }
189         return out;
190 }
191
192
193 const char *Packet::Type2String(uint8_t t) noexcept {
194         switch (t) {
195                 case Ping::TYPE:
196                         return "Ping";
197                 case Login::TYPE:
198                         return "Login";
199                 case Join::TYPE:
200                         return "Join";
201                 case Part::TYPE:
202                         return "Part";
203                 case PlayerUpdate::TYPE:
204                         return "PlayerUpdate";
205                 case SpawnEntity::TYPE:
206                         return "SpawnEntity";
207                 case DespawnEntity::TYPE:
208                         return "DespawnEntity";
209                 case EntityUpdate::TYPE:
210                         return "EntityUpdate";
211                 case PlayerCorrection::TYPE:
212                         return "PlayerCorrection";
213                 case ChunkBegin::TYPE:
214                         return "ChunkBegin";
215                 case ChunkData::TYPE:
216                         return "ChunkData";
217                 case BlockUpdate::TYPE:
218                         return "BlockUpdate";
219                 case Message::TYPE:
220                         return "Message";
221                 default:
222                         return "Unknown";
223         }
224 }
225
226 template<class T>
227 void Packet::Payload::Write(const T &src, size_t off) noexcept {
228         if ((length - off) < sizeof(T)) {
229                 // dismiss out of bounds write
230                 return;
231         }
232         *reinterpret_cast<T *>(&data[off]) = src;
233 }
234
235 template<class T>
236 void Packet::Payload::Read(T &dst, size_t off) const noexcept {
237         if ((length - off) < sizeof(T)) {
238                 // dismiss out of bounds read
239                 return;
240         }
241         dst = *reinterpret_cast<T *>(&data[off]);
242 }
243
244 void Packet::Payload::WriteString(const string &src, size_t off, size_t maxlen) noexcept {
245         uint8_t *dst = &data[off];
246         size_t len = min(maxlen, length - off);
247         if (src.size() < len) {
248                 memset(dst, '\0', len);
249                 memcpy(dst, src.c_str(), src.size());
250         } else {
251                 memcpy(dst, src.c_str(), len);
252         }
253 }
254
255 void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const noexcept {
256         size_t len = min(maxlen, length - off);
257         dst.clear();
258         dst.reserve(len);
259         for (size_t i = 0; i < len && data[off + i] != '\0'; ++i) {
260                 dst.push_back(data[off + i]);
261         }
262 }
263
264 void Packet::Payload::Write(const glm::quat &val, size_t off) noexcept {
265         WritePackN(val.w, off);
266         WritePackN(val.x, off + 2);
267         WritePackN(val.y, off + 4);
268         WritePackN(val.z, off + 6);
269 }
270
271 void Packet::Payload::Read(glm::quat &val, size_t off) const noexcept {
272         ReadPackN(val.w, off);
273         ReadPackN(val.x, off + 2);
274         ReadPackN(val.y, off + 4);
275         ReadPackN(val.z, off + 6);
276         val = normalize(val);
277 }
278
279 void Packet::Payload::Write(const EntityState &state, size_t off) noexcept {
280         Write(state.chunk_pos, off);
281         WritePackU(state.block_pos * (1.0f / 16.0f), off + 12);
282         Write(state.velocity, off + 18);
283         Write(state.orient, off + 30);
284 }
285
286 void Packet::Payload::Read(EntityState &state, size_t off) const noexcept {
287         Read(state.chunk_pos, off);
288         ReadPackU(state.block_pos, off + 12);
289         Read(state.velocity, off + 18);
290         Read(state.orient, off + 30);
291         state.block_pos *= 16.0f;
292 }
293
294 void Packet::Payload::Write(const EntityState &state, const glm::ivec3 &base, size_t off) noexcept {
295         WritePackB(state.chunk_pos - base, off);
296         WritePackU(state.block_pos * (1.0f / 16.0f), off + 3);
297         Write(state.velocity, off + 9);
298         Write(state.orient, off + 21);
299 }
300
301 void Packet::Payload::Read(EntityState &state, const glm::ivec3 &base, size_t off) const noexcept {
302         ReadPackB(state.chunk_pos, off);
303         ReadPackU(state.block_pos, off + 3);
304         Read(state.velocity, off + 9);
305         Read(state.orient, off + 21);
306         state.chunk_pos += base;
307         state.block_pos *= 16.0f;
308 }
309
310 void Packet::Payload::WritePackN(float val, size_t off) noexcept {
311         int16_t raw = glm::clamp(glm::round(val * 32767.0f), -32767.0f, 32767.0f);
312         Write(raw, off);
313 }
314
315 void Packet::Payload::WritePackB(const glm::ivec3 &val, size_t off) noexcept {
316         Write(int8_t(val.x), off);
317         Write(int8_t(val.y), off + 1);
318         Write(int8_t(val.z), off + 2);
319 }
320
321 void Packet::Payload::ReadPackB(glm::ivec3 &val, size_t off) const noexcept {
322         int8_t conv = 0;
323         Read(conv, off);
324         val.x = conv;
325         Read(conv, off + 1);
326         val.y = conv;
327         Read(conv, off + 2);
328         val.z = conv;
329 }
330
331 void Packet::Payload::ReadPackN(float &val, size_t off) const noexcept {
332         int16_t raw = 0;
333         Read(raw, off);
334         val = raw * (1.0f/32767.0f);
335 }
336
337 void Packet::Payload::WritePackN(const glm::vec3 &val, size_t off) noexcept {
338         WritePackN(val.x, off);
339         WritePackN(val.y, off + 2);
340         WritePackN(val.z, off + 4);
341 }
342
343 void Packet::Payload::ReadPackN(glm::vec3 &val, size_t off) const noexcept {
344         ReadPackN(val.x, off);
345         ReadPackN(val.y, off + 2);
346         ReadPackN(val.z, off + 4);
347 }
348
349 void Packet::Payload::WritePackU(float val, size_t off) noexcept {
350         uint16_t raw = glm::clamp(glm::round(val * 65535.0f), 0.0f, 65535.0f);
351         Write(raw, off);
352 }
353
354 void Packet::Payload::ReadPackU(float &val, size_t off) const noexcept {
355         uint16_t raw = 0;
356         Read(raw, off);
357         val = raw * (1.0f/65535.0f);
358 }
359
360 void Packet::Payload::WritePackU(const glm::vec3 &val, size_t off) noexcept {
361         WritePackU(val.x, off);
362         WritePackU(val.y, off + 2);
363         WritePackU(val.z, off + 4);
364 }
365
366 void Packet::Payload::ReadPackU(glm::vec3 &val, size_t off) const noexcept {
367         ReadPackU(val.x, off);
368         ReadPackU(val.y, off + 2);
369         ReadPackU(val.z, off + 4);
370 }
371
372
373 void Packet::Login::WritePlayerName(const string &name) noexcept {
374         WriteString(name, 0, 32);
375 }
376
377 void Packet::Login::ReadPlayerName(string &name) const noexcept {
378         ReadString(name, 0, 32);
379 }
380
381 void Packet::Join::WritePlayer(const Entity &player) noexcept {
382         Write(player.ID(), 0);
383         Write(player.GetState(), 4);
384 }
385
386 void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept {
387         Read(id, 0);
388 }
389
390 void Packet::Join::ReadPlayerState(EntityState &state) const noexcept {
391         Read(state, 4);
392 }
393
394 void Packet::Join::WriteWorldName(const string &name) noexcept {
395         WriteString(name, 54, 32);
396 }
397
398 void Packet::Join::ReadWorldName(string &name) const noexcept {
399         ReadString(name, 54, 32);
400 }
401
402 void Packet::PlayerUpdate::WritePredictedState(const EntityState &state) noexcept {
403         Write(state, 0);
404 }
405
406 void Packet::PlayerUpdate::ReadPredictedState(EntityState &state) const noexcept {
407         Read(state, 0);
408 }
409
410 void Packet::PlayerUpdate::WriteMovement(const glm::vec3 &mov) noexcept {
411         WritePackN(mov, 50);
412 }
413
414 void Packet::PlayerUpdate::ReadMovement(glm::vec3 &mov) const noexcept {
415         ReadPackN(mov, 50);
416 }
417
418 void Packet::PlayerUpdate::WritePitch(float pitch) noexcept {
419         float conv = pitch * PI_0p5_inv;
420         WritePackN(conv, 56);
421 }
422
423 void Packet::PlayerUpdate::ReadPitch(float &pitch) const noexcept {
424         ReadPackN(pitch, 56);
425         pitch *= PI_0p5;
426 }
427
428 void Packet::PlayerUpdate::WriteYaw(float yaw) noexcept {
429         float conv = yaw * PI_inv;
430         WritePackN(conv, 58);
431 }
432
433 void Packet::PlayerUpdate::ReadYaw(float &yaw) const noexcept {
434         ReadPackN(yaw, 58);
435         yaw *= PI;
436 }
437
438 void Packet::PlayerUpdate::WriteActions(uint8_t actions) noexcept {
439         Write(actions, 60);
440 }
441
442 void Packet::PlayerUpdate::ReadActions(uint8_t &actions) const noexcept {
443         Read(actions, 60);
444 }
445
446 void Packet::PlayerUpdate::WriteSlot(uint8_t slot) noexcept {
447         Write(slot, 61);
448 }
449
450 void Packet::PlayerUpdate::ReadSlot(uint8_t &slot) const noexcept {
451         Read(slot, 61);
452 }
453
454 void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept {
455         Write(e.ID(), 0);
456         if (e.GetModel()) {
457                 Write(e.GetModel().GetModel().ID(), 4);
458         } else {
459                 Write(uint32_t(0), 4);
460         }
461         Write(e.GetState(), 8);
462         Write(e.Bounds(), 58);
463         uint32_t flags = 0;
464         if (e.WorldCollidable()) {
465                 flags |= 1;
466         }
467         Write(flags, 82);
468         WriteString(e.Name(), 86, 32);
469 }
470
471 void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept {
472         Read(id, 0);
473 }
474
475 void Packet::SpawnEntity::ReadModelID(uint32_t &id) const noexcept {
476         Read(id, 4);
477 }
478
479 void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept {
480         EntityState state;
481         AABB bounds;
482         uint32_t flags = 0;
483         string name;
484
485         Read(state, 8);
486         Read(bounds, 58);
487         Read(flags, 82);
488         ReadString(name, 86, 32);
489
490         e.SetState(state);
491         e.Bounds(bounds);
492         e.WorldCollidable(flags & 1);
493         e.Name(name);
494 }
495
496 void Packet::DespawnEntity::WriteEntityID(uint32_t id) noexcept {
497         Write(id, 0);
498 }
499
500 void Packet::DespawnEntity::ReadEntityID(uint32_t &id) const noexcept {
501         Read(id, 0);
502 }
503
504 void Packet::EntityUpdate::WriteEntityCount(uint32_t count) noexcept {
505         Write(count, 0);
506 }
507
508 void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept {
509         Read(count, 0);
510 }
511
512 void Packet::EntityUpdate::WriteChunkBase(const glm::ivec3 &base) noexcept {
513         Write(base, 4);
514 }
515
516 void Packet::EntityUpdate::ReadChunkBase(glm::ivec3 &base) const noexcept {
517         Read(base, 4);
518 }
519
520 void Packet::EntityUpdate::WriteEntity(const Entity &entity, const glm::ivec3 &base, uint32_t num) noexcept {
521         uint32_t off = GetSize(num);
522
523         Write(entity.ID(), off);
524         Write(entity.GetState(), base, off + 4);
525 }
526
527 void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept {
528         uint32_t off = GetSize(num);
529         Read(id, off);
530 }
531
532 void Packet::EntityUpdate::ReadEntityState(EntityState &state, const glm::ivec3 &base, uint32_t num) const noexcept {
533         uint32_t off = GetSize(num);
534         Read(state, base, off + 4);
535 }
536
537 void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept {
538         Write(s, 0);
539 }
540
541 void Packet::PlayerCorrection::ReadPacketSeq(std::uint16_t &s) const noexcept {
542         Read(s, 0);
543 }
544
545 void Packet::PlayerCorrection::WritePlayer(const Entity &player) noexcept {
546         Write(player.GetState(), 2);
547 }
548
549 void Packet::PlayerCorrection::ReadPlayerState(EntityState &state) const noexcept {
550         Read(state, 2);
551 }
552
553 void Packet::ChunkBegin::WriteTransmissionId(uint32_t id) noexcept {
554         Write(id, 0);
555 }
556
557 void Packet::ChunkBegin::ReadTransmissionId(uint32_t &id) const noexcept {
558         Read(id, 0);
559 }
560
561 void Packet::ChunkBegin::WriteFlags(uint32_t f) noexcept {
562         Write(f, 4);
563 }
564
565 void Packet::ChunkBegin::ReadFlags(uint32_t &f) const noexcept {
566         Read(f, 4);
567 }
568
569 void Packet::ChunkBegin::WriteChunkCoords(const glm::ivec3 &pos) noexcept {
570         Write(pos, 8);
571 }
572
573 void Packet::ChunkBegin::ReadChunkCoords(glm::ivec3 &pos) const noexcept {
574         Read(pos, 8);
575 }
576
577 void Packet::ChunkBegin::WriteDataSize(uint32_t s) noexcept {
578         Write(s, 20);
579 }
580
581 void Packet::ChunkBegin::ReadDataSize(uint32_t &s) const noexcept {
582         Read(s, 20);
583 }
584
585 void Packet::ChunkData::WriteTransmissionId(uint32_t id) noexcept {
586         Write(id, 0);
587 }
588
589 void Packet::ChunkData::ReadTransmissionId(uint32_t &id) const noexcept {
590         Read(id, 0);
591 }
592
593 void Packet::ChunkData::WriteDataOffset(uint32_t o) noexcept {
594         Write(o, 4);
595 }
596
597 void Packet::ChunkData::ReadDataOffset(uint32_t &o) const noexcept {
598         Read(o, 4);
599 }
600
601 void Packet::ChunkData::WriteDataSize(uint32_t s) noexcept {
602         Write(s, 8);
603 }
604
605 void Packet::ChunkData::ReadDataSize(uint32_t &s) const noexcept {
606         Read(s, 8);
607 }
608
609 void Packet::ChunkData::WriteData(const uint8_t *d, size_t l) noexcept {
610         size_t len = min(length - 12, l);
611         memcpy(&data[12], d, len);
612 }
613
614 void Packet::ChunkData::ReadData(uint8_t *d, size_t l) const noexcept {
615         size_t len = min(length - 12, l);
616         memcpy(d, &data[12], len);
617 }
618
619 void Packet::BlockUpdate::WriteChunkCoords(const glm::ivec3 &coords) noexcept {
620         Write(coords, 0);
621 }
622
623 void Packet::BlockUpdate::ReadChunkCoords(glm::ivec3 &coords) const noexcept {
624         Read(coords, 0);
625 }
626
627 void Packet::BlockUpdate::WriteBlockCount(uint32_t count) noexcept {
628         Write(count, 12);
629 }
630
631 void Packet::BlockUpdate::ReadBlockCount(uint32_t &count) const noexcept {
632         Read(count, 12);
633 }
634
635 void Packet::BlockUpdate::WriteIndex(uint16_t index, uint32_t num) noexcept {
636         uint32_t off = GetSize(num);
637         Write(index, off);
638 }
639
640 void Packet::BlockUpdate::ReadIndex(uint16_t &index, uint32_t num) const noexcept {
641         uint32_t off = GetSize(num);
642         Read(index, off);
643 }
644
645 void Packet::BlockUpdate::WriteBlock(const Block &block, uint32_t num) noexcept {
646         uint32_t off = GetSize(num) + 2;
647         Write(block, off);
648 }
649
650 void Packet::BlockUpdate::ReadBlock(Block &block, uint32_t num) const noexcept {
651         uint32_t off = GetSize(num) + 2;
652         Read(block, off);
653 }
654
655 void Packet::Message::WriteType(uint8_t type) noexcept {
656         Write(type, 0);
657 }
658
659 void Packet::Message::ReadType(uint8_t &type) const noexcept {
660         Read(type, 0);
661 }
662
663 void Packet::Message::WriteReferral(uint32_t ref) noexcept {
664         Write(ref, 1);
665 }
666
667 void Packet::Message::ReadReferral(uint32_t &ref) const noexcept {
668         Read(ref, 1);
669 }
670
671 void Packet::Message::WriteMessage(const string &msg) noexcept {
672         WriteString(msg, 5, MAX_MESSAGE_LEN);
673 }
674
675 void Packet::Message::ReadMessage(string &msg) const noexcept {
676         ReadString(msg, 5, MAX_MESSAGE_LEN);
677 }
678
679
680 void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
681         const Packet &pack = *reinterpret_cast<const Packet *>(udp_pack.data);
682         switch (pack.Type()) {
683                 case Packet::Ping::TYPE:
684                         On(Packet::As<Packet::Ping>(udp_pack));
685                         break;
686                 case Packet::Login::TYPE:
687                         On(Packet::As<Packet::Login>(udp_pack));
688                         break;
689                 case Packet::Join::TYPE:
690                         On(Packet::As<Packet::Join>(udp_pack));
691                         break;
692                 case Packet::Part::TYPE:
693                         On(Packet::As<Packet::Part>(udp_pack));
694                         break;
695                 case Packet::PlayerUpdate::TYPE:
696                         On(Packet::As<Packet::PlayerUpdate>(udp_pack));
697                         break;
698                 case Packet::SpawnEntity::TYPE:
699                         On(Packet::As<Packet::SpawnEntity>(udp_pack));
700                         break;
701                 case Packet::DespawnEntity::TYPE:
702                         On(Packet::As<Packet::DespawnEntity>(udp_pack));
703                         break;
704                 case Packet::EntityUpdate::TYPE:
705                         On(Packet::As<Packet::EntityUpdate>(udp_pack));
706                         break;
707                 case Packet::PlayerCorrection::TYPE:
708                         On(Packet::As<Packet::PlayerCorrection>(udp_pack));
709                         break;
710                 case Packet::ChunkBegin::TYPE:
711                         On(Packet::As<Packet::ChunkBegin>(udp_pack));
712                         break;
713                 case Packet::ChunkData::TYPE:
714                         On(Packet::As<Packet::ChunkData>(udp_pack));
715                         break;
716                 case Packet::BlockUpdate::TYPE:
717                         On(Packet::As<Packet::BlockUpdate>(udp_pack));
718                         break;
719                 case Packet::Message::TYPE:
720                         On(Packet::As<Packet::Message>(udp_pack));
721                         break;
722                 default:
723                         // drop unknown or unhandled packets
724                         break;
725         }
726 }
727
728 }