]> git.localhorst.tv Git - blank.git/blobdiff - src/net/net.cpp
add packet for merging player state back to client
[blank.git] / src / net / net.cpp
index 9c29c1e09edeb96d9c7de52d433ad4e2f15ec341..3f4bf7453eab78a0ac79b7f045026ec10114a8f0 100644 (file)
@@ -1,20 +1,36 @@
 #include "Client.hpp"
+#include "ClientConnection.hpp"
 #include "Connection.hpp"
+#include "ConnectionHandler.hpp"
 #include "io.hpp"
 #include "Packet.hpp"
 #include "Server.hpp"
 
 #include "../app/init.hpp"
+#include "../model/CompositeModel.hpp"
+#include "../world/Entity.hpp"
+#include "../world/EntityState.hpp"
 #include "../world/World.hpp"
 
 #include <cstring>
 #include <iostream>
+#include <glm/gtx/io.hpp>
 
 using namespace std;
 
 
 namespace blank {
 
+constexpr size_t Packet::Ping::MAX_LEN;
+constexpr size_t Packet::Login::MAX_LEN;
+constexpr size_t Packet::Join::MAX_LEN;
+constexpr size_t Packet::Part::MAX_LEN;
+constexpr size_t Packet::PlayerUpdate::MAX_LEN;
+constexpr size_t Packet::SpawnEntity::MAX_LEN;
+constexpr size_t Packet::DespawnEntity::MAX_LEN;
+constexpr size_t Packet::EntityUpdate::MAX_LEN;
+constexpr size_t Packet::PlayerCorrection::MAX_LEN;
+
 namespace {
 
 UDPsocket client_bind(Uint16 port) {
@@ -35,9 +51,8 @@ IPaddress client_resolve(const char *host, Uint16 port) {
 
 }
 
-Client::Client(const Config &conf, World &world)
-: world(world)
-, conn(client_resolve(conf.host.c_str(), conf.port))
+Client::Client(const Config &conf)
+: conn(client_resolve(conf.host.c_str(), conf.port))
 , client_sock(client_bind(0))
 , client_pack{ -1, nullptr, 0 } {
        client_pack.data = new Uint8[sizeof(Packet)];
@@ -80,29 +95,271 @@ void Client::HandlePacket(const UDPpacket &udp_pack) {
 
 void Client::Update(int dt) {
        conn.Update(dt);
-       if (conn.TimedOut()) {
-               cout << "connection timed out :(" << endl;
-       } else if (conn.ShouldPing()) {
+       if (conn.ShouldPing()) {
                SendPing();
        }
 }
 
-void Client::SendPing() {
-       conn.SendPing(client_pack, client_sock);
+uint16_t Client::SendPing() {
+       return conn.SendPing(client_pack, client_sock);
+}
+
+uint16_t Client::SendLogin(const string &name) {
+       auto pack = Packet::Make<Packet::Login>(client_pack);
+       pack.WritePlayerName(name);
+       return conn.Send(client_pack, client_sock);
+}
+
+uint16_t Client::SendPlayerUpdate(const Entity &player) {
+       auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
+       pack.WritePlayer(player);
+       return conn.Send(client_pack, client_sock);
+}
+
+uint16_t Client::SendPart() {
+       Packet::Make<Packet::Part>(client_pack);
+       return conn.Send(client_pack, client_sock);
+}
+
+
+ClientConnection::ClientConnection(Server &server, const IPaddress &addr)
+: server(server)
+, conn(addr)
+, player(nullptr)
+, spawns()
+, confirm_wait(0)
+, player_update_pack(0)
+, player_update_timer(1500) {
+       conn.SetHandler(this);
+}
+
+ClientConnection::~ClientConnection() {
+       DetachPlayer();
+}
+
+void ClientConnection::Update(int dt) {
+       conn.Update(dt);
+       if (Disconnected()) {
+               return;
+       }
+       if (HasPlayer()) {
+               // sync entities
+               auto global_iter = server.GetWorld().Entities().begin();
+               auto global_end = server.GetWorld().Entities().end();
+               auto local_iter = spawns.begin();
+               auto local_end = spawns.end();
+
+               while (global_iter != global_end && local_iter != local_end) {
+                       if (global_iter->ID() == local_iter->entity->ID()) {
+                               // they're the same
+                               if (CanDespawn(*global_iter)) {
+                                       SendDespawn(*local_iter);
+                               } else {
+                                       // update
+                                       SendUpdate(*local_iter);
+                               }
+                               ++global_iter;
+                               ++local_iter;
+                       } else if (global_iter->ID() < local_iter->entity->ID()) {
+                               // global entity was inserted
+                               if (CanSpawn(*global_iter)) {
+                                       auto spawned = spawns.emplace(local_iter, *global_iter);
+                                       SendSpawn(*spawned);
+                               }
+                               ++global_iter;
+                       } else {
+                               // global entity was removed
+                               SendDespawn(*local_iter);
+                               ++local_iter;
+                       }
+               }
+
+               // leftover spawns
+               while (global_iter != global_end) {
+                       if (CanSpawn(*global_iter)) {
+                               spawns.emplace_back(*global_iter);
+                               SendSpawn(spawns.back());
+                       }
+                       ++global_iter;
+               }
+
+               // leftover despawns
+               while (local_iter != local_end) {
+                       SendDespawn(*local_iter);
+                       ++local_iter;
+               }
+
+               CheckPlayerFix();
+       }
+       if (conn.ShouldPing()) {
+               conn.SendPing(server.GetPacket(), server.GetSocket());
+       }
+}
+
+ClientConnection::SpawnStatus::SpawnStatus(Entity &e)
+: entity(&e)
+, spawn_pack(-1)
+, despawn_pack(-1) {
+       entity->Ref();
+}
+
+ClientConnection::SpawnStatus::~SpawnStatus() {
+       entity->UnRef();
+}
+
+bool ClientConnection::CanSpawn(const Entity &e) const noexcept {
+       return
+               &e != player &&
+               !e.Dead() &&
+               manhattan_radius(e.ChunkCoords() - Player().ChunkCoords()) < 7;
+}
+
+bool ClientConnection::CanDespawn(const Entity &e) const noexcept {
+       return
+               e.Dead() ||
+               manhattan_radius(e.ChunkCoords() - Player().ChunkCoords()) > 7;
+}
+
+void ClientConnection::SendSpawn(SpawnStatus &status) {
+       // don't double spawn
+       if (status.spawn_pack != -1) return;
+
+       auto pack = Packet::Make<Packet::SpawnEntity>(server.GetPacket());
+       pack.WriteEntity(*status.entity);
+       status.spawn_pack = conn.Send(server.GetPacket(), server.GetSocket());
+       ++confirm_wait;
+}
+
+void ClientConnection::SendDespawn(SpawnStatus &status) {
+       // don't double despawn
+       if (status.despawn_pack != -1) return;
+
+       auto pack = Packet::Make<Packet::DespawnEntity>(server.GetPacket());
+       pack.WriteEntityID(status.entity->ID());
+       status.despawn_pack = conn.Send(server.GetPacket(), server.GetSocket());
+       ++confirm_wait;
+}
+
+void ClientConnection::SendUpdate(SpawnStatus &status) {
+       // don't send updates while spawn not ack'd or despawn sent
+       if (status.spawn_pack != -1 || status.despawn_pack != -1) return;
+
+       // TODO: pack entity updates
+       auto pack = Packet::Make<Packet::EntityUpdate>(server.GetPacket());
+       pack.WriteEntityCount(1);
+       pack.WriteEntity(*status.entity, 0);
+       server.GetPacket().len = Packet::EntityUpdate::GetSize(1);
+       conn.Send(server.GetPacket(), server.GetSocket());
+}
+
+void ClientConnection::CheckPlayerFix() {
+       // check always succeeds for now ;)
+       auto pack = Packet::Make<Packet::PlayerCorrection>(server.GetPacket());
+       pack.WritePacketSeq(player_update_pack);
+       pack.WritePlayer(Player());
+       conn.Send(server.GetPacket(), server.GetSocket());
+}
+
+void ClientConnection::AttachPlayer(Entity &new_player) {
+       DetachPlayer();
+       player = &new_player;
+       player->Ref();
+       cout << "player \"" << player->Name() << "\" joined" << endl;
+}
+
+void ClientConnection::DetachPlayer() {
+       if (!player) return;
+       player->Kill();
+       player->UnRef();
+       cout << "player \"" << player->Name() << "\" left" << endl;
+       player = nullptr;
+}
+
+void ClientConnection::OnPacketReceived(uint16_t seq) {
+       if (!confirm_wait) return;
+       for (auto iter = spawns.begin(), end = spawns.end(); iter != end; ++iter) {
+               if (seq == iter->spawn_pack) {
+                       iter->spawn_pack = -1;
+                       --confirm_wait;
+                       return;
+               }
+               if (seq == iter->despawn_pack) {
+                       spawns.erase(iter);
+                       --confirm_wait;
+                       return;
+               }
+       }
+}
+
+void ClientConnection::OnPacketLost(uint16_t seq) {
+       if (!confirm_wait) return;
+       for (SpawnStatus &status : spawns) {
+               if (seq == status.spawn_pack) {
+                       status.spawn_pack = -1;
+                       --confirm_wait;
+                       SendSpawn(status);
+                       return;
+               }
+               if (seq == status.despawn_pack) {
+                       status.despawn_pack = -1;
+                       --confirm_wait;
+                       SendDespawn(status);
+                       return;
+               }
+       }
+}
+
+void ClientConnection::On(const Packet::Login &pack) {
+       string name;
+       pack.ReadPlayerName(name);
+
+       Entity *new_player = server.GetWorld().AddPlayer(name).entity;
+
+       if (new_player) {
+               // success!
+               AttachPlayer(*new_player);
+               cout << "accepted login from player \"" << name << '"' << endl;
+               auto response = Packet::Make<Packet::Join>(server.GetPacket());
+               response.WritePlayer(*new_player);
+               response.WriteWorldName(server.GetWorld().Name());
+               conn.Send(server.GetPacket(), server.GetSocket());
+               // set up update tracking
+               player_update_pack = pack.Seq();
+               player_update_timer.Reset();
+               player_update_timer.Start();
+       } else {
+               // aw no :(
+               cout << "rejected login from player \"" << name << '"' << endl;
+               Packet::Make<Packet::Part>(server.GetPacket());
+               conn.Send(server.GetPacket(), server.GetSocket());
+               conn.Close();
+       }
+}
+
+void ClientConnection::On(const Packet::Part &) {
+       conn.Close();
 }
 
-void Client::SendLogin(const string &name) {
-       Packet &pack = *reinterpret_cast<Packet *>(client_pack.data);
-       client_pack.len = pack.MakeLogin(name);
-       conn.Send(client_pack, client_sock);
+void ClientConnection::On(const Packet::PlayerUpdate &pack) {
+       if (!HasPlayer()) return;
+       int pack_diff = int16_t(pack.Seq()) - int16_t(player_update_pack);
+       bool overdue = player_update_timer.HitOnce();
+       player_update_timer.Reset();
+       if (pack_diff > 0 || overdue) {
+               player_update_pack = pack.Seq();
+               // TODO: do client input validation here
+               pack.ReadPlayerState(Player().GetState());
+       }
 }
 
 
 Connection::Connection(const IPaddress &addr)
-: addr(addr)
-, send_timer(3000)
+: handler(nullptr)
+, addr(addr)
+, send_timer(500)
 , recv_timer(10000)
-, ctrl{ 0, 0xFFFF, 0xFFFF }
+, ctrl_out{ 0, 0xFFFF, 0xFFFFFFFF }
+, ctrl_in{ 0, 0xFFFF, 0xFFFFFFFF }
 , closed(false) {
        send_timer.Start();
        recv_timer.Start();
@@ -121,7 +378,7 @@ void Connection::FlagRecv() noexcept {
 }
 
 bool Connection::ShouldPing() const noexcept {
-       return send_timer.HitOnce();
+       return !closed && send_timer.HitOnce();
 }
 
 bool Connection::TimedOut() const noexcept {
@@ -131,14 +388,19 @@ bool Connection::TimedOut() const noexcept {
 void Connection::Update(int dt) {
        send_timer.Update(dt);
        recv_timer.Update(dt);
+       if (TimedOut()) {
+               Close();
+               if (HasHandler()) {
+                       Handler().OnTimeout();
+               }
+       }
 }
 
 
-void Connection::Send(UDPpacket &udp_pack, UDPsocket sock) {
+uint16_t Connection::Send(UDPpacket &udp_pack, UDPsocket sock) {
        Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
-       pack.header.ctrl = ctrl;
-
-       cout << "sending " << pack.GetType() << " to " << Address() << endl;
+       pack.header.ctrl = ctrl_out;
+       uint16_t seq = ctrl_out.seq++;
 
        udp_pack.address = addr;
        if (SDLNet_UDP_Send(sock, -1, &udp_pack) == 0) {
@@ -146,49 +408,66 @@ void Connection::Send(UDPpacket &udp_pack, UDPsocket sock) {
        }
 
        FlagSend();
+       return seq;
 }
 
 void Connection::Received(const UDPpacket &udp_pack) {
        Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
 
-       cout << "received " << pack.GetType() << " from " << Address() << endl;
-
-       int diff = std::int16_t(pack.header.ctrl.seq) - std::int16_t(ctrl.ack);
-
+       // ack to the remote
+       int16_t diff = int16_t(pack.header.ctrl.seq) - int16_t(ctrl_out.ack);
        if (diff > 0) {
-               // incoming more recent than last acked
-
-               // TODO: packets considered lost are detected here
-               //       this should have ones for all of them:
-               //       ~hist & ((1 << dist) - 1) if dist is < 32
-
                if (diff >= 32) {
-                       // missed more than the last 32 oO
-                       ctrl.hist = 0;
+                       ctrl_out.hist = 0;
                } else {
-                       ctrl.hist >>= diff;
-                       ctrl.hist |= 1 << (32 - diff);
+                       ctrl_out.hist <<= diff;
+                       ctrl_out.hist |= 1 << (diff - 1);
                }
-       } else if (diff < 0) {
-               // incoming older than acked
-               if (diff > -32) {
-                       // too late :/
-               } else {
-                       ctrl.hist |= 1 << (32 + diff);
-               }
-       } else {
-               // incoming the same as last acked oO
+       } else if (diff < 0 && diff >= -32) {
+               ctrl_out.hist |= 1 << (-diff - 1);
+       }
+       ctrl_out.ack = pack.header.ctrl.seq;
+       FlagRecv();
+
+       if (!HasHandler()) {
+               return;
        }
 
-       ctrl.ack = pack.header.ctrl.seq;
+       Packet::TControl ctrl_new = pack.header.ctrl;
+       Handler().Handle(udp_pack);
 
-       FlagRecv();
+       if (diff > 0) {
+               // if the packet holds more recent information
+               // check if remote failed to ack one of our packets
+               diff = int16_t(ctrl_new.ack) - int16_t(ctrl_in.ack);
+               // should always be true, but you never know…
+               if (diff > 0) {
+                       for (int i = 0; i < diff; ++i) {
+                               if (i > 32 || (i < 32 && (ctrl_in.hist & (1 << (31 - i))) == 0)) {
+                                       Handler().OnPacketLost(ctrl_in.ack - 32 + i);
+                               }
+                       }
+               }
+               // check for newly ack'd packets
+               for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); ++s) {
+                       if (ctrl_new.Acks(s) && !ctrl_in.Acks(s)) {
+                               Handler().OnPacketReceived(s);
+                       }
+               }
+               ctrl_in = ctrl_new;
+       }
 }
 
-void Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) {
-       Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
-       udp_pack.len = pack.MakePing();
-       Send(udp_pack, sock);
+bool Packet::TControl::Acks(uint16_t s) const noexcept {
+       int16_t diff = int16_t(ack) - int16_t(s);
+       if (diff == 0) return true;
+       if (diff < 0 || diff > 32) return false;
+       return (hist & (1 << (diff - 1))) != 0;
+}
+
+uint16_t Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) {
+       Packet::Make<Packet::Ping>(udp_pack);
+       return Send(udp_pack, sock);
 }
 
 
@@ -205,85 +484,232 @@ ostream &operator <<(ostream &out, const IPaddress &addr) {
 }
 
 
-const char *Packet::Type2String(Type t) noexcept {
+const char *Packet::Type2String(uint8_t t) noexcept {
        switch (t) {
-               case PING:
-                       return "PING";
-               case LOGIN:
-                       return "LOGIN";
-               case JOIN:
-                       return "JOIN";
-               case PART:
-                       return "PART";
+               case Ping::TYPE:
+                       return "Ping";
+               case Login::TYPE:
+                       return "Login";
+               case Join::TYPE:
+                       return "Join";
+               case Part::TYPE:
+                       return "Part";
+               case PlayerUpdate::TYPE:
+                       return "PlayerUpdate";
+               case SpawnEntity::TYPE:
+                       return "SpawnEntity";
+               case DespawnEntity::TYPE:
+                       return "DespawnEntity";
+               case EntityUpdate::TYPE:
+                       return "EntityUpdate";
+               case PlayerCorrection::TYPE:
+                       return "PlayerCorrection";
                default:
-                       return "UNKNOWN";
+                       return "Unknown";
        }
 }
 
-void Packet::Tag() noexcept {
-       header.tag = TAG;
+template<class T>
+void Packet::Payload::Write(const T &src, size_t off) noexcept {
+       if ((length - off) < sizeof(T)) {
+               // dismiss out of bounds write
+               return;
+       }
+       *reinterpret_cast<T *>(&data[off]) = src;
 }
 
-size_t Packet::MakePing() noexcept {
-       Tag();
-       header.type = PING;
-       return sizeof(Header);
+template<class T>
+void Packet::Payload::Read(T &dst, size_t off) const noexcept {
+       if ((length - off) < sizeof(T)) {
+               // dismiss out of bounds read
+               return;
+       }
+       dst = *reinterpret_cast<T *>(&data[off]);
 }
 
-size_t Packet::MakeLogin(const string &name) noexcept {
-       constexpr size_t maxname = 32;
-
-       Tag();
-       header.type = LOGIN;
-       if (name.size() < maxname) {
-               memset(payload, '\0', maxname);
-               memcpy(payload, name.c_str(), name.size());
+void Packet::Payload::WriteString(const string &src, size_t off, size_t maxlen) noexcept {
+       uint8_t *dst = &data[off];
+       size_t len = min(maxlen, length - off);
+       if (src.size() < len) {
+               memset(dst, '\0', len);
+               memcpy(dst, src.c_str(), src.size());
        } else {
-               memcpy(payload, name.c_str(), maxname);
+               memcpy(dst, src.c_str(), len);
+       }
+}
+
+void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const noexcept {
+       size_t len = min(maxlen, length - off);
+       dst.clear();
+       dst.reserve(len);
+       for (size_t i = 0; i < len && data[off + i] != '\0'; ++i) {
+               dst.push_back(data[off + i]);
        }
-       return sizeof(Header) + maxname;
 }
 
-size_t Packet::MakeJoin(const Entity &player, const string &world_name) noexcept {
-       constexpr size_t maxname = 32;
 
-       Tag();
-       header.type = JOIN;
+void Packet::Login::WritePlayerName(const string &name) noexcept {
+       WriteString(name, 0, 32);
+}
+
+void Packet::Login::ReadPlayerName(string &name) const noexcept {
+       ReadString(name, 0, 32);
+}
+
+void Packet::Join::WritePlayer(const Entity &player) noexcept {
+       Write(player.ID(), 0);
+       Write(player.GetState(), 4);
+}
+
+void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept {
+       Read(id, 0);
+}
 
-       uint8_t *cursor = &payload[0];
+void Packet::Join::ReadPlayerState(EntityState &state) const noexcept {
+       Read(state, 4);
+}
 
-       // TODO: generate entity IDs
-       *reinterpret_cast<uint32_t *>(cursor) = 1;
-       cursor += 4;
+void Packet::Join::WriteWorldName(const string &name) noexcept {
+       WriteString(name, 68, 32);
+}
 
-       *reinterpret_cast<glm::ivec3 *>(cursor) = player.ChunkCoords();
-       cursor += 12;
+void Packet::Join::ReadWorldName(string &name) const noexcept {
+       ReadString(name, 68, 32);
+}
 
-       *reinterpret_cast<glm::vec3 *>(cursor) = player.Position();
-       cursor += 12;
-       *reinterpret_cast<glm::vec3 *>(cursor) = player.Velocity();
-       cursor += 12;
+void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept {
+       Write(player.GetState(), 0);
+}
 
-       *reinterpret_cast<glm::quat *>(cursor) = player.Orientation();
-       cursor += 16;
-       *reinterpret_cast<glm::vec3 *>(cursor) = player.AngularVelocity();
-       cursor += 12;
+void Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept {
+       Read(state, 0);
+}
 
-       if (world_name.size() < maxname) {
-               memset(cursor, '\0', maxname);
-               memcpy(cursor, world_name.c_str(), world_name.size());
+void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept {
+       Write(e.ID(), 0);
+       if (e.GetModel()) {
+               Write(e.GetModel().GetModel().ID(), 4);
        } else {
-               memcpy(cursor, world_name.c_str(), maxname);
+               Write(uint32_t(0), 4);
+       }
+       Write(e.GetState(), 8);
+       Write(e.Bounds(), 72);
+       uint32_t flags = 0;
+       if (e.WorldCollidable()) {
+               flags |= 1;
        }
-       cursor += maxname;
+       Write(flags, 96);
+       WriteString(e.Name(), 100, 32);
+}
 
-       return sizeof(Header) + (cursor - &payload[0]);
+void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept {
+       Read(id, 0);
 }
 
-size_t Packet::MakePart() noexcept {
-       Tag();
-       header.type = PART;
-       return sizeof(Header);
+void Packet::SpawnEntity::ReadSkeletonID(uint32_t &id) const noexcept {
+       Read(id, 4);
+}
+
+void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept {
+       EntityState state;
+       AABB bounds;
+       uint32_t flags = 0;
+       string name;
+
+       Read(state, 8);
+       Read(bounds, 72);
+       Read(flags, 96);
+       ReadString(name, 100, 32);
+
+       e.SetState(state);
+       e.Bounds(bounds);
+       e.WorldCollidable(flags & 1);
+       e.Name(name);
+}
+
+void Packet::DespawnEntity::WriteEntityID(uint32_t id) noexcept {
+       Write(id, 0);
+}
+
+void Packet::DespawnEntity::ReadEntityID(uint32_t &id) const noexcept {
+       Read(id, 0);
+}
+
+void Packet::EntityUpdate::WriteEntityCount(uint32_t count) noexcept {
+       Write(count, 0);
+}
+
+void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept {
+       Read(count, 0);
+}
+
+void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexcept {
+       uint32_t off = 4 + (num * 64);
+
+       Write(entity.ID(), off);
+       Write(entity.GetState(), off + 4);
+}
+
+void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept {
+       Read(id, 4 + (num * 64));
+}
+
+void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept {
+       uint32_t off = 4 + (num * 64);
+       Read(state, off + 4);
+}
+
+void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept {
+       Write(s, 0);
+}
+
+void Packet::PlayerCorrection::ReadPacketSeq(std::uint16_t &s) const noexcept {
+       Read(s, 0);
+}
+
+void Packet::PlayerCorrection::WritePlayer(const Entity &player) noexcept {
+       Write(player.GetState(), 2);
+}
+
+void Packet::PlayerCorrection::ReadPlayerState(EntityState &state) const noexcept {
+       Read(state, 2);
+}
+
+
+void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
+       const Packet &pack = *reinterpret_cast<const Packet *>(udp_pack.data);
+       switch (pack.Type()) {
+               case Packet::Ping::TYPE:
+                       On(Packet::As<Packet::Ping>(udp_pack));
+                       break;
+               case Packet::Login::TYPE:
+                       On(Packet::As<Packet::Login>(udp_pack));
+                       break;
+               case Packet::Join::TYPE:
+                       On(Packet::As<Packet::Join>(udp_pack));
+                       break;
+               case Packet::Part::TYPE:
+                       On(Packet::As<Packet::Part>(udp_pack));
+                       break;
+               case Packet::PlayerUpdate::TYPE:
+                       On(Packet::As<Packet::PlayerUpdate>(udp_pack));
+                       break;
+               case Packet::SpawnEntity::TYPE:
+                       On(Packet::As<Packet::SpawnEntity>(udp_pack));
+                       break;
+               case Packet::DespawnEntity::TYPE:
+                       On(Packet::As<Packet::DespawnEntity>(udp_pack));
+                       break;
+               case Packet::EntityUpdate::TYPE:
+                       On(Packet::As<Packet::EntityUpdate>(udp_pack));
+                       break;
+               case Packet::PlayerCorrection::TYPE:
+                       On(Packet::As<Packet::PlayerCorrection>(udp_pack));
+                       break;
+               default:
+                       // drop unknown or unhandled packets
+                       break;
+       }
 }
 
 
@@ -330,87 +756,29 @@ void Server::HandlePacket(const UDPpacket &udp_pack) {
                return;
        }
 
-       Connection &client = GetClient(udp_pack.address);
-       client.Received(udp_pack);
-
-       switch (pack.header.type) {
-               case Packet::LOGIN:
-                       HandleLogin(client, udp_pack);
-                       break;
-               case Packet::PART:
-                       HandlePart(client, udp_pack);
-                       break;
-               default:
-                       // just drop packets of unknown or unhandled type
-                       break;
-       }
+       ClientConnection &client = GetClient(udp_pack.address);
+       client.GetConnection().Received(udp_pack);
 }
 
-Connection &Server::GetClient(const IPaddress &addr) {
-       for (Connection &client : clients) {
+ClientConnection &Server::GetClient(const IPaddress &addr) {
+       for (ClientConnection &client : clients) {
                if (client.Matches(addr)) {
                        return client;
                }
        }
-       clients.emplace_back(addr);
-       OnConnect(clients.back());
+       clients.emplace_back(*this, addr);
        return clients.back();
 }
 
-void Server::OnConnect(Connection &client) {
-       cout << "new connection from " << client.Address() << endl;
-       // tell it we're alive
-       client.SendPing(serv_pack, serv_sock);
-}
-
 void Server::Update(int dt) {
-       for (list<Connection>::iterator client(clients.begin()), end(clients.end()); client != end;) {
+       for (list<ClientConnection>::iterator client(clients.begin()), end(clients.end()); client != end;) {
                client->Update(dt);
-               if (client->Closed()) {
-                       OnDisconnect(*client);
+               if (client->Disconnected()) {
                        client = clients.erase(client);
                } else {
-                       if (client->ShouldPing()) {
-                               client->SendPing(serv_pack, serv_sock);
-                       }
                        ++client;
                }
        }
 }
 
-void Server::OnDisconnect(Connection &client) {
-       cout << "connection timeout from " << client.Address() << endl;
-}
-
-
-void Server::HandleLogin(Connection &client, const UDPpacket &udp_pack) {
-       const Packet &pack = *reinterpret_cast<const Packet *>(udp_pack.data);
-       size_t maxlen = min(udp_pack.len - int(sizeof(Packet::Header)), 32);
-       string name;
-       name.reserve(maxlen);
-       for (size_t i = 0; i < maxlen && pack.payload[i] != '\0'; ++i) {
-               name.push_back(pack.payload[i]);
-       }
-
-       Entity *player = world.AddPlayer(name);
-       Packet &response = *reinterpret_cast<Packet *>(serv_pack.data);
-
-       if (player) {
-               // success!
-               cout << "accepted login from player \"" << name << '"' << endl;
-               response.MakeJoin(*player, world.Name());
-               client.Send(serv_pack, serv_sock);
-       } else {
-               // aw no :(
-               cout << "rejected login from player \"" << name << '"' << endl;
-               response.MakePart();
-               client.Send(serv_pack, serv_sock);
-               client.Close();
-       }
-}
-
-void Server::HandlePart(Connection &client, const UDPpacket &udp_pack) {
-       client.Close();
-}
-
 }