X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=81de3e476e8286245e574905081b047d28b17308;hb=38a4cffc0b6aa58e49d24c06aad7bee14cb6515d;hp=837a7fb18125dccf52b7ea4e8eca491e5d27b566;hpb=c6ca9d21e45af5ea7caeec722a9b59fdf3aa3b24;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index 837a7fb..81de3e4 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -1,17 +1,14 @@ -#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 "../world/World.hpp" +#include "../model/CompositeModel.hpp" +#include "../world/Entity.hpp" +#include "../world/EntityState.hpp" #include -#include -#include using namespace std; @@ -23,158 +20,12 @@ 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; - -namespace { - -UDPsocket client_bind(Uint16 port) { - UDPsocket sock = SDLNet_UDP_Open(port); - if (!sock) { - throw NetError("SDLNet_UDP_Open"); - } - return sock; -} - -IPaddress client_resolve(const char *host, Uint16 port) { - IPaddress addr; - if (SDLNet_ResolveHost(&addr, host, port) != 0) { - throw NetError("SDLNet_ResolveHost"); - } - return addr; -} - -} - -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)]; - client_pack.maxlen = sizeof(Packet); - // establish connection - SendPing(); -} - -Client::~Client() { - delete[] client_pack.data; - SDLNet_UDP_Close(client_sock); -} - - -void Client::Handle() { - int result = SDLNet_UDP_Recv(client_sock, &client_pack); - while (result > 0) { - HandlePacket(client_pack); - result = SDLNet_UDP_Recv(client_sock, &client_pack); - } - if (result == -1) { - // a boo boo happened - throw NetError("SDLNet_UDP_Recv"); - } -} - -void Client::HandlePacket(const UDPpacket &udp_pack) { - if (!conn.Matches(udp_pack.address)) { - // packet came from somewhere else, drop - return; - } - const Packet &pack = *reinterpret_cast(udp_pack.data); - if (pack.header.tag != Packet::TAG) { - // mistagged packet, drop - return; - } - - conn.Received(udp_pack); -} - -void Client::Update(int dt) { - conn.Update(dt); - if (conn.ShouldPing()) { - SendPing(); - } -} - -uint16_t Client::SendPing() { - return conn.SendPing(client_pack, client_sock); -} - -uint16_t Client::SendLogin(const string &name) { - auto pack = Packet::Make(client_pack); - pack.WritePlayerName(name); - return conn.Send(client_pack, client_sock); -} - -uint16_t Client::SendPlayerUpdate(const Entity &player) { - auto pack = Packet::Make(client_pack); - pack.WritePlayer(player); - return conn.Send(client_pack, client_sock); -} - - -ClientConnection::ClientConnection(Server &server, const IPaddress &addr) -: server(server) -, conn(addr) -, player(nullptr) { - conn.SetHandler(this); -} - -ClientConnection::~ClientConnection() { - DetachPlayer(); -} - -void ClientConnection::Update(int dt) { - conn.Update(dt); - if (Disconnected()) { - cout << "disconnect from " << conn.Address() << endl; - } else if (conn.ShouldPing()) { - conn.SendPing(server.GetPacket(), server.GetSocket()); - } -} - -void ClientConnection::AttachPlayer(Entity &new_player) { - DetachPlayer(); - player = &new_player; - player->Ref(); -} - -void ClientConnection::DetachPlayer() { - if (!player) return; - player->Kill(); - player->UnRef(); - player = nullptr; -} - -void ClientConnection::On(const Packet::Login &pack) { - string name; - pack.ReadPlayerName(name); - - Entity *new_player = server.GetWorld().AddPlayer(name); - - if (new_player) { - // success! - AttachPlayer(*new_player); - cout << "accepted login from player \"" << name << '"' << endl; - auto response = Packet::Make(server.GetPacket()); - response.WritePlayer(*new_player); - response.WriteWorldName(server.GetWorld().Name()); - conn.Send(server.GetPacket(), server.GetSocket()); - } else { - // aw no :( - cout << "rejected login from player \"" << name << '"' << endl; - Packet::Make(server.GetPacket()); - conn.Send(server.GetPacket(), server.GetSocket()); - conn.Close(); - } -} - -void ClientConnection::On(const Packet::Part &) { - conn.Close(); -} - -void ClientConnection::On(const Packet::PlayerUpdate &pack) { - if (!HasPlayer()) return; - pack.ReadPlayer(Player()); -} - +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; +constexpr size_t Packet::ChunkBegin::MAX_LEN; +constexpr size_t Packet::ChunkData::MAX_LEN; Connection::Connection(const IPaddress &addr) : handler(nullptr) @@ -271,10 +122,23 @@ void Connection::Received(const UDPpacket &udp_pack) { } } } + // 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; } } +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(udp_pack); return Send(udp_pack, sock); @@ -306,6 +170,18 @@ const char *Packet::Type2String(uint8_t t) noexcept { 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"; + case ChunkBegin::TYPE: + return "ChunkBegin"; + case ChunkData::TYPE: + return "ChunkData"; default: return "Unknown"; } @@ -360,34 +236,15 @@ void Packet::Login::ReadPlayerName(string &name) const noexcept { void Packet::Join::WritePlayer(const Entity &player) noexcept { Write(player.ID(), 0); - Write(player.ChunkCoords(), 4); - Write(player.Position(), 16); - Write(player.Velocity(), 28); - Write(player.Orientation(), 40); - Write(player.AngularVelocity(), 56); + Write(player.GetState(), 4); } void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept { Read(id, 0); } -void Packet::Join::ReadPlayer(Entity &player) const noexcept { - glm::ivec3 chunk_coords(0); - glm::vec3 pos; - glm::vec3 vel; - glm::quat rot; - glm::vec3 ang; - - Read(chunk_coords, 4); - Read(pos, 16); - Read(vel, 28); - Read(rot, 40); - Read(ang, 56); - - player.Position(chunk_coords, pos); - player.Velocity(vel); - player.Orientation(rot); - player.AngularVelocity(ang); +void Packet::Join::ReadPlayerState(EntityState &state) const noexcept { + Read(state, 4); } void Packet::Join::WriteWorldName(const string &name) noexcept { @@ -399,30 +256,167 @@ void Packet::Join::ReadWorldName(string &name) const noexcept { } void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept { - Write(player.ChunkCoords(), 0); - Write(player.Position(), 12); - Write(player.Velocity(), 24); - Write(player.Orientation(), 36); - Write(player.AngularVelocity(), 52); + Write(player.GetState(), 0); +} + +void Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept { + Read(state, 0); } -void Packet::PlayerUpdate::ReadPlayer(Entity &player) const noexcept { - glm::ivec3 chunk_coords(0); - glm::vec3 pos; - glm::vec3 vel; - glm::quat rot; - glm::vec3 ang; +void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { + Write(e.ID(), 0); + if (e.GetModel()) { + Write(e.GetModel().GetModel().ID(), 4); + } else { + Write(uint32_t(0), 4); + } + Write(e.GetState(), 8); + Write(e.Bounds(), 72); + uint32_t flags = 0; + if (e.WorldCollidable()) { + flags |= 1; + } + Write(flags, 96); + WriteString(e.Name(), 100, 32); +} - Read(chunk_coords, 0); - Read(pos, 12); - Read(vel, 24); - Read(rot, 36); - Read(ang, 52); +void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept { + Read(id, 0); +} + +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); +} - player.Position(chunk_coords, pos); - player.Velocity(vel); - player.Orientation(rot); - player.AngularVelocity(ang); +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 Packet::ChunkBegin::WriteTransmissionId(uint32_t id) noexcept { + Write(id, 0); +} + +void Packet::ChunkBegin::ReadTransmissionId(uint32_t &id) const noexcept { + Read(id, 0); +} + +void Packet::ChunkBegin::WriteFlags(uint32_t f) noexcept { + Write(f, 4); +} + +void Packet::ChunkBegin::ReadFlags(uint32_t &f) const noexcept { + Read(f, 4); +} + +void Packet::ChunkBegin::WriteChunkCoords(const glm::ivec3 &pos) noexcept { + Write(pos, 8); +} + +void Packet::ChunkBegin::ReadChunkCoords(glm::ivec3 &pos) const noexcept { + Read(pos, 8); +} + +void Packet::ChunkBegin::WriteDataSize(uint32_t s) noexcept { + Write(s, 20); +} + +void Packet::ChunkBegin::ReadDataSize(uint32_t &s) const noexcept { + Read(s, 20); +} + +void Packet::ChunkData::WriteTransmissionId(uint32_t id) noexcept { + Write(id, 0); +} + +void Packet::ChunkData::ReadTransmissionId(uint32_t &id) const noexcept { + Read(id, 0); +} + +void Packet::ChunkData::WriteDataOffset(uint32_t o) noexcept { + Write(o, 4); +} + +void Packet::ChunkData::ReadDataOffset(uint32_t &o) const noexcept { + Read(o, 4); +} + +void Packet::ChunkData::WriteDataSize(uint32_t s) noexcept { + Write(s, 8); +} + +void Packet::ChunkData::ReadDataSize(uint32_t &s) const noexcept { + Read(s, 8); +} + +void Packet::ChunkData::WriteData(const uint8_t *d, size_t l) noexcept { + size_t len = min(length - 12, l); + memcpy(&data[12], d, len); +} + +void Packet::ChunkData::ReadData(uint8_t *d, size_t l) const noexcept { + size_t len = min(length - 12, l); + memcpy(d, &data[12], len); } @@ -444,79 +438,28 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { case Packet::PlayerUpdate::TYPE: On(Packet::As(udp_pack)); break; + case Packet::SpawnEntity::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::DespawnEntity::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::EntityUpdate::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::PlayerCorrection::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::ChunkBegin::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::ChunkData::TYPE: + On(Packet::As(udp_pack)); + break; default: // drop unknown or unhandled packets break; } } - -Server::Server(const Config &conf, World &world) -: serv_sock(nullptr) -, serv_pack{ -1, nullptr, 0 } -, clients() -, world(world) { - serv_sock = SDLNet_UDP_Open(conf.port); - if (!serv_sock) { - throw NetError("SDLNet_UDP_Open"); - } - - serv_pack.data = new Uint8[sizeof(Packet)]; - serv_pack.maxlen = sizeof(Packet); -} - -Server::~Server() { - delete[] serv_pack.data; - SDLNet_UDP_Close(serv_sock); -} - - -void Server::Handle() { - int result = SDLNet_UDP_Recv(serv_sock, &serv_pack); - while (result > 0) { - HandlePacket(serv_pack); - result = SDLNet_UDP_Recv(serv_sock, &serv_pack); - } - if (result == -1) { - // a boo boo happened - throw NetError("SDLNet_UDP_Recv"); - } -} - -void Server::HandlePacket(const UDPpacket &udp_pack) { - if (udp_pack.len < int(sizeof(Packet::Header))) { - // packet too small, drop - return; - } - const Packet &pack = *reinterpret_cast(udp_pack.data); - if (pack.header.tag != Packet::TAG) { - // mistagged packet, drop - return; - } - - ClientConnection &client = GetClient(udp_pack.address); - client.GetConnection().Received(udp_pack); -} - -ClientConnection &Server::GetClient(const IPaddress &addr) { - for (ClientConnection &client : clients) { - if (client.Matches(addr)) { - return client; - } - } - clients.emplace_back(*this, addr); - return clients.back(); -} - -void Server::Update(int dt) { - for (list::iterator client(clients.begin()), end(clients.end()); client != end;) { - client->Update(dt); - if (client->Disconnected()) { - client = clients.erase(client); - } else { - ++client; - } - } -} - }