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