1 #include "Connection.hpp"
2 #include "ConnectionHandler.hpp"
6 #include "../app/init.hpp"
7 #include "../model/CompositeModel.hpp"
8 #include "../world/Entity.hpp"
9 #include "../world/EntityState.hpp"
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;
30 Connection::Connection(const IPaddress &addr)
35 , ctrl_out{ 0, 0xFFFF, 0xFFFFFFFF }
36 , ctrl_in{ 0, 0xFFFF, 0xFFFFFFFF }
42 bool Connection::Matches(const IPaddress &remote) const noexcept {
43 return memcmp(&addr, &remote, sizeof(IPaddress)) == 0;
46 void Connection::FlagSend() noexcept {
50 void Connection::FlagRecv() noexcept {
54 bool Connection::ShouldPing() const noexcept {
55 return !closed && send_timer.HitOnce();
58 bool Connection::TimedOut() const noexcept {
59 return recv_timer.HitOnce();
62 void Connection::Update(int dt) {
63 send_timer.Update(dt);
64 recv_timer.Update(dt);
68 Handler().OnTimeout();
74 uint16_t Connection::Send(UDPpacket &udp_pack, UDPsocket sock) {
75 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
76 pack.header.ctrl = ctrl_out;
77 uint16_t seq = ctrl_out.seq++;
79 udp_pack.address = addr;
80 if (SDLNet_UDP_Send(sock, -1, &udp_pack) == 0) {
81 throw NetError("SDLNet_UDP_Send");
88 void Connection::Received(const UDPpacket &udp_pack) {
89 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
92 int16_t diff = int16_t(pack.header.ctrl.seq) - int16_t(ctrl_out.ack);
97 ctrl_out.hist <<= diff;
98 ctrl_out.hist |= 1 << (diff - 1);
100 } else if (diff < 0 && diff >= -32) {
101 ctrl_out.hist |= 1 << (-diff - 1);
103 ctrl_out.ack = pack.header.ctrl.seq;
110 Packet::TControl ctrl_new = pack.header.ctrl;
111 Handler().Handle(udp_pack);
114 // if the packet holds more recent information
115 // check if remote failed to ack one of our packets
116 diff = int16_t(ctrl_new.ack) - int16_t(ctrl_in.ack);
117 // should always be true, but you never know…
119 for (int i = 0; i < diff; ++i) {
120 if (i > 32 || (i < 32 && (ctrl_in.hist & (1 << (31 - i))) == 0)) {
121 Handler().PacketLost(ctrl_in.ack - 32 + i);
125 // check for newly ack'd packets
126 for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); --s) {
127 if (ctrl_new.Acks(s) && !ctrl_in.Acks(s)) {
128 Handler().PacketReceived(s);
135 bool Packet::TControl::Acks(uint16_t s) const noexcept {
136 int16_t diff = int16_t(ack) - int16_t(s);
137 if (diff == 0) return true;
138 if (diff < 0 || diff > 32) return false;
139 return (hist & (1 << (diff - 1))) != 0;
142 uint16_t Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) {
143 Packet::Make<Packet::Ping>(udp_pack);
144 return Send(udp_pack, sock);
148 ConnectionHandler::ConnectionHandler()
150 , packets_received(0)
151 , packet_loss(0.0f) {
155 void ConnectionHandler::PacketLost(uint16_t seq) {
161 void ConnectionHandler::PacketReceived(uint16_t seq) {
162 OnPacketReceived(seq);
167 void ConnectionHandler::UpdatePacketLoss() noexcept {
168 unsigned int packets_total = packets_lost + packets_received;
169 if (packets_total >= 256) {
170 packet_loss = float(packets_lost) / float(packets_total);
172 packets_received = 0;
177 ostream &operator <<(ostream &out, const IPaddress &addr) {
178 const unsigned char *host = reinterpret_cast<const unsigned char *>(&addr.host);
180 << '.' << int(host[1])
181 << '.' << int(host[2])
182 << '.' << int(host[3]);
184 out << ':' << SDLNet_Read16(&addr.port);
190 const char *Packet::Type2String(uint8_t t) noexcept {
200 case PlayerUpdate::TYPE:
201 return "PlayerUpdate";
202 case SpawnEntity::TYPE:
203 return "SpawnEntity";
204 case DespawnEntity::TYPE:
205 return "DespawnEntity";
206 case EntityUpdate::TYPE:
207 return "EntityUpdate";
208 case PlayerCorrection::TYPE:
209 return "PlayerCorrection";
210 case ChunkBegin::TYPE:
212 case ChunkData::TYPE:
220 void Packet::Payload::Write(const T &src, size_t off) noexcept {
221 if ((length - off) < sizeof(T)) {
222 // dismiss out of bounds write
225 *reinterpret_cast<T *>(&data[off]) = src;
229 void Packet::Payload::Read(T &dst, size_t off) const noexcept {
230 if ((length - off) < sizeof(T)) {
231 // dismiss out of bounds read
234 dst = *reinterpret_cast<T *>(&data[off]);
237 void Packet::Payload::WriteString(const string &src, size_t off, size_t maxlen) noexcept {
238 uint8_t *dst = &data[off];
239 size_t len = min(maxlen, length - off);
240 if (src.size() < len) {
241 memset(dst, '\0', len);
242 memcpy(dst, src.c_str(), src.size());
244 memcpy(dst, src.c_str(), len);
248 void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const noexcept {
249 size_t len = min(maxlen, length - off);
252 for (size_t i = 0; i < len && data[off + i] != '\0'; ++i) {
253 dst.push_back(data[off + i]);
258 void Packet::Login::WritePlayerName(const string &name) noexcept {
259 WriteString(name, 0, 32);
262 void Packet::Login::ReadPlayerName(string &name) const noexcept {
263 ReadString(name, 0, 32);
266 void Packet::Join::WritePlayer(const Entity &player) noexcept {
267 Write(player.ID(), 0);
268 Write(player.GetState(), 4);
271 void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept {
275 void Packet::Join::ReadPlayerState(EntityState &state) const noexcept {
279 void Packet::Join::WriteWorldName(const string &name) noexcept {
280 WriteString(name, 68, 32);
283 void Packet::Join::ReadWorldName(string &name) const noexcept {
284 ReadString(name, 68, 32);
287 void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept {
288 Write(player.GetState(), 0);
291 void Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept {
295 void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept {
298 Write(e.GetModel().GetModel().ID(), 4);
300 Write(uint32_t(0), 4);
302 Write(e.GetState(), 8);
303 Write(e.Bounds(), 72);
305 if (e.WorldCollidable()) {
309 WriteString(e.Name(), 100, 32);
312 void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept {
316 void Packet::SpawnEntity::ReadSkeletonID(uint32_t &id) const noexcept {
320 void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept {
329 ReadString(name, 100, 32);
333 e.WorldCollidable(flags & 1);
337 void Packet::DespawnEntity::WriteEntityID(uint32_t id) noexcept {
341 void Packet::DespawnEntity::ReadEntityID(uint32_t &id) const noexcept {
345 void Packet::EntityUpdate::WriteEntityCount(uint32_t count) noexcept {
349 void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept {
353 void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexcept {
354 uint32_t off = GetSize(num);;
356 Write(entity.ID(), off);
357 Write(entity.GetState(), off + 4);
360 void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept {
361 uint32_t off = GetSize(num);;
365 void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept {
366 uint32_t off = GetSize(num);;
367 Read(state, off + 4);
370 void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept {
374 void Packet::PlayerCorrection::ReadPacketSeq(std::uint16_t &s) const noexcept {
378 void Packet::PlayerCorrection::WritePlayer(const Entity &player) noexcept {
379 Write(player.GetState(), 2);
382 void Packet::PlayerCorrection::ReadPlayerState(EntityState &state) const noexcept {
386 void Packet::ChunkBegin::WriteTransmissionId(uint32_t id) noexcept {
390 void Packet::ChunkBegin::ReadTransmissionId(uint32_t &id) const noexcept {
394 void Packet::ChunkBegin::WriteFlags(uint32_t f) noexcept {
398 void Packet::ChunkBegin::ReadFlags(uint32_t &f) const noexcept {
402 void Packet::ChunkBegin::WriteChunkCoords(const glm::ivec3 &pos) noexcept {
406 void Packet::ChunkBegin::ReadChunkCoords(glm::ivec3 &pos) const noexcept {
410 void Packet::ChunkBegin::WriteDataSize(uint32_t s) noexcept {
414 void Packet::ChunkBegin::ReadDataSize(uint32_t &s) const noexcept {
418 void Packet::ChunkData::WriteTransmissionId(uint32_t id) noexcept {
422 void Packet::ChunkData::ReadTransmissionId(uint32_t &id) const noexcept {
426 void Packet::ChunkData::WriteDataOffset(uint32_t o) noexcept {
430 void Packet::ChunkData::ReadDataOffset(uint32_t &o) const noexcept {
434 void Packet::ChunkData::WriteDataSize(uint32_t s) noexcept {
438 void Packet::ChunkData::ReadDataSize(uint32_t &s) const noexcept {
442 void Packet::ChunkData::WriteData(const uint8_t *d, size_t l) noexcept {
443 size_t len = min(length - 12, l);
444 memcpy(&data[12], d, len);
447 void Packet::ChunkData::ReadData(uint8_t *d, size_t l) const noexcept {
448 size_t len = min(length - 12, l);
449 memcpy(d, &data[12], len);
453 void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
454 const Packet &pack = *reinterpret_cast<const Packet *>(udp_pack.data);
455 switch (pack.Type()) {
456 case Packet::Ping::TYPE:
457 On(Packet::As<Packet::Ping>(udp_pack));
459 case Packet::Login::TYPE:
460 On(Packet::As<Packet::Login>(udp_pack));
462 case Packet::Join::TYPE:
463 On(Packet::As<Packet::Join>(udp_pack));
465 case Packet::Part::TYPE:
466 On(Packet::As<Packet::Part>(udp_pack));
468 case Packet::PlayerUpdate::TYPE:
469 On(Packet::As<Packet::PlayerUpdate>(udp_pack));
471 case Packet::SpawnEntity::TYPE:
472 On(Packet::As<Packet::SpawnEntity>(udp_pack));
474 case Packet::DespawnEntity::TYPE:
475 On(Packet::As<Packet::DespawnEntity>(udp_pack));
477 case Packet::EntityUpdate::TYPE:
478 On(Packet::As<Packet::EntityUpdate>(udp_pack));
480 case Packet::PlayerCorrection::TYPE:
481 On(Packet::As<Packet::PlayerCorrection>(udp_pack));
483 case Packet::ChunkBegin::TYPE:
484 On(Packet::As<Packet::ChunkBegin>(udp_pack));
486 case Packet::ChunkData::TYPE:
487 On(Packet::As<Packet::ChunkData>(udp_pack));
490 // drop unknown or unhandled packets