X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnet%2Fnet.cpp;h=4f7fc66647a2817eb8f87133f4f937e382096097;hb=dcd54cacda98c2c0f7cf0c7a9131fb858d8ee10a;hp=98954bf0f168635ac5810b61b70889b2f6cb471e;hpb=896c4c0ba2efd6774894fd8308cc097b7f4123e3;p=blank.git diff --git a/src/net/net.cpp b/src/net/net.cpp index 98954bf..4f7fc66 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -1,20 +1,16 @@ -#include "Client.hpp" -#include "ClientConnection.hpp" +#include "CongestionControl.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 "../geometry/const.hpp" +#include "../model/Model.hpp" #include "../world/Entity.hpp" #include "../world/EntityState.hpp" -#include "../world/World.hpp" #include -#include -#include using namespace std; @@ -30,328 +26,171 @@ 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) { - 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"); +constexpr size_t Packet::ChunkBegin::MAX_LEN; +constexpr size_t Packet::ChunkData::MAX_LEN; +constexpr size_t Packet::BlockUpdate::MAX_LEN; +constexpr size_t Packet::Message::MAX_LEN; +constexpr size_t Packet::Message::MAX_MESSAGE_LEN; + + +CongestionControl::CongestionControl() +// I know, I know, it's an estimate (about 20 for IPv4, 48 for IPv6) +: packet_overhead(20) +// only sample every eighth packet for measuring RTT +, sample_skip(8) +, packets_lost(0) +, packets_received(0) +, packet_loss(0.0f) +, stamp_last(0) +, rtt(64.0f) +, next_sample(1000) +, tx_bytes(0) +, rx_bytes(0) +, tx_kbps(0.0f) +, rx_kbps(0.0f) +, mode(GOOD) +// rtt > 75ms or packet loss > 5% is BAD +, bad_rtt(75.0f) +, bad_loss(0.05f) +// rtt > 150ms or packet loss > 15% is UGLY +, ugly_rtt(150.0f) +, ugly_loss(0.15f) +, mode_keep_time(1000) { + Uint32 now = SDL_GetTicks(); + for (Uint32 &s : stamps) { + s = now; + } + next_sample += now; + mode_entered = now; + mode_reset = now; + mode_step = now; +} + +void CongestionControl::PacketSent(uint16_t seq) noexcept { + if (!SamplePacket(seq)) { + return; } - return addr; -} - + stamps[SampleIndex(seq)] = SDL_GetTicks(); + stamp_last = seq; } -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(); +void CongestionControl::PacketLost(uint16_t seq) noexcept { + ++packets_lost; + UpdatePacketLoss(); + UpdateRTT(seq); } -Client::~Client() { - delete[] client_pack.data; - SDLNet_UDP_Close(client_sock); +void CongestionControl::PacketReceived(uint16_t seq) noexcept { + ++packets_received; + UpdatePacketLoss(); + UpdateRTT(seq); } - -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 CongestionControl::UpdatePacketLoss() noexcept { + unsigned int packets_total = packets_lost + packets_received; + if (packets_total >= 256) { + packet_loss = float(packets_lost) / float(packets_total); + packets_lost = 0; + packets_received = 0; } } -void Client::HandlePacket(const UDPpacket &udp_pack) { - if (!conn.Matches(udp_pack.address)) { - // packet came from somewhere else, drop +void CongestionControl::UpdateRTT(uint16_t seq) noexcept { + if (!SamplePacket(seq)) return; + int16_t diff = int16_t(stamp_last) - int16_t(seq); + if (diff < 0 || diff > int(15 * sample_skip)) { + // packet outside observed frame 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); + int cur_rtt = SDL_GetTicks() - stamps[SampleIndex(seq)]; + rtt += (cur_rtt - rtt) * 0.1f; } -uint16_t Client::SendLogin(const string &name) { - auto pack = Packet::Make(client_pack); - pack.WritePlayerName(name); - return conn.Send(client_pack, client_sock); +bool CongestionControl::SamplePacket(uint16_t seq) const noexcept { + return seq % sample_skip == 0; } -uint16_t Client::SendPlayerUpdate(const Entity &player) { - auto pack = Packet::Make(client_pack); - pack.WritePlayer(player); - return conn.Send(client_pack, client_sock); +size_t CongestionControl::SampleIndex(uint16_t seq) const noexcept { + return (seq / sample_skip) % 16; } -uint16_t Client::SendPart() { - Packet::Make(client_pack); - return conn.Send(client_pack, client_sock); +void CongestionControl::PacketIn(const UDPpacket &pack) noexcept { + rx_bytes += pack.len + packet_overhead; + UpdateStats(); } - -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 CongestionControl::PacketOut(const UDPpacket &pack) noexcept { + tx_bytes += pack.len + packet_overhead; + UpdateStats(); } -void ClientConnection::Update(int dt) { - conn.Update(dt); - if (Disconnected()) { +void CongestionControl::UpdateStats() noexcept { + Uint32 now = SDL_GetTicks(); + if (now < next_sample) { + // not yet 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(server.GetPacket()); - pack.WriteEntity(*status.entity); - status.spawn_pack = conn.Send(server.GetPacket(), server.GetSocket()); - ++confirm_wait; + tx_kbps = float(tx_bytes) * (1.0f / 1024.0f); + rx_kbps = float(rx_bytes) * (1.0f / 1024.0f); + tx_bytes = 0; + rx_bytes = 0; + next_sample += 1000; + UpdateMode(); } -void ClientConnection::SendDespawn(SpawnStatus &status) { - // don't double despawn - if (status.despawn_pack != -1) return; - - auto pack = Packet::Make(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(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(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 CongestionControl::UpdateMode() noexcept { + Mode now_mode = Conditions(); + if (now_mode > mode) { + ChangeMode(now_mode); + } else if (now_mode < mode) { + CheckUpgrade(now_mode); + } else { + KeepMode(); + } } -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 CongestionControl::CheckUpgrade(Mode m) noexcept { + Uint32 now = SDL_GetTicks(); + Uint32 time_in_mode = now - mode_entered; + if (time_in_mode < mode_keep_time) { + return; } + ChangeMode(m); } -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 CongestionControl::ChangeMode(Mode m) noexcept { + Uint32 now = SDL_GetTicks(); + if (m > mode) { + // changed to worse mode + // if we spent less than 10 seconds in better mode + // double keep time till up to 64 seconds + if (now - mode_entered < 10000) { + if (mode_keep_time < 64000) { + mode_keep_time *= 2; + } } } + mode = m; + mode_entered = now; + mode_reset = mode_entered; } -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(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(server.GetPacket()); - conn.Send(server.GetPacket(), server.GetSocket()); - conn.Close(); +void CongestionControl::KeepMode() noexcept { + mode_reset = SDL_GetTicks(); + // if in good mode for 10 seconds, halve keep time till down to one second + if (mode == GOOD && mode_keep_time > 1000 && mode_step - mode_reset > 10000) { + mode_keep_time /= 2; + mode_step = mode_reset; } } -void ClientConnection::On(const Packet::Part &) { - conn.Close(); -} - -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 - EntityState new_state; - pack.ReadPlayerState(new_state); - Player().Velocity(new_state.velocity); - Player().Orientation(new_state.orient); +CongestionControl::Mode CongestionControl::Conditions() const noexcept { + if (rtt > ugly_rtt || packet_loss > ugly_loss) { + return UGLY; + } else if (rtt > bad_rtt || packet_loss > bad_loss) { + return BAD; + } else { + return GOOD; } } @@ -359,7 +198,9 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) { Connection::Connection(const IPaddress &addr) : handler(nullptr) , addr(addr) -, send_timer(500) +// make sure a packet is sent at least every 50ms since packets contains +// acks that the remote end will use to measure RTT +, send_timer(50) , recv_timer(10000) , ctrl_out{ 0, 0xFFFF, 0xFFFFFFFF } , ctrl_in{ 0, 0xFFFF, 0xFFFFFFFF } @@ -410,6 +251,11 @@ uint16_t Connection::Send(UDPpacket &udp_pack, UDPsocket sock) { throw NetError("SDLNet_UDP_Send"); } + if (HasHandler()) { + Handler().PacketOut(udp_pack); + Handler().PacketSent(seq); + } + FlagSend(); return seq; } @@ -437,6 +283,7 @@ void Connection::Received(const UDPpacket &udp_pack) { } Packet::TControl ctrl_new = pack.header.ctrl; + Handler().PacketIn(udp_pack); Handler().Handle(udp_pack); if (diff > 0) { @@ -447,14 +294,14 @@ void Connection::Received(const UDPpacket &udp_pack) { 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); + Handler().PacketLost(ctrl_in.ack - 32 + i); } } } // check for newly ack'd packets - for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); ++s) { + for (uint16_t s = ctrl_new.AckBegin(); s != ctrl_new.AckEnd(); --s) { if (ctrl_new.Acks(s) && !ctrl_in.Acks(s)) { - Handler().OnPacketReceived(s); + Handler().PacketReceived(s); } } ctrl_in = ctrl_new; @@ -474,6 +321,34 @@ uint16_t Connection::SendPing(UDPpacket &udp_pack, UDPsocket sock) { } +ConnectionHandler::ConnectionHandler() +: cc() { + +} + +void ConnectionHandler::PacketSent(uint16_t seq) noexcept { + cc.PacketSent(seq); +} + +void ConnectionHandler::PacketLost(uint16_t seq) { + OnPacketLost(seq); + cc.PacketLost(seq); +} + +void ConnectionHandler::PacketReceived(uint16_t seq) { + OnPacketReceived(seq); + cc.PacketReceived(seq); +} + +void ConnectionHandler::PacketIn(const UDPpacket &pack) noexcept { + cc.PacketIn(pack); +} + +void ConnectionHandler::PacketOut(const UDPpacket &pack) noexcept { + cc.PacketOut(pack); +} + + ostream &operator <<(ostream &out, const IPaddress &addr) { const unsigned char *host = reinterpret_cast(&addr.host); out << int(host[0]) @@ -507,6 +382,14 @@ const char *Packet::Type2String(uint8_t t) noexcept { return "EntityUpdate"; case PlayerCorrection::TYPE: return "PlayerCorrection"; + case ChunkBegin::TYPE: + return "ChunkBegin"; + case ChunkData::TYPE: + return "ChunkData"; + case BlockUpdate::TYPE: + return "BlockUpdate"; + case Message::TYPE: + return "Message"; default: return "Unknown"; } @@ -550,6 +433,175 @@ void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const n } } +void Packet::Payload::Write(const glm::quat &val, size_t off) noexcept { + // find the largest component + float largest = 0.0f; + int largest_index = -1; + for (int i = 0; i < 4; ++i) { + float iabs = abs(val[i]); + if (iabs > largest) { + largest = iabs; + largest_index = i; + } + } + // make sure it's positive + const glm::quat q(val[largest_index] < 0.0f ? -val : val); + // move index to the two most significant bits + uint64_t packed = uint64_t(largest_index) << 62; + // we have to map from [-0.7072,0.7072] to [-524287,524287] and move into positive range + constexpr float conv = 524287.0f / 0.7072f; + // if largest is 1, the other three are 0 + // precision of comparison is the interval of our mapping + if (abs(1.0 - largest) < (1.0f / conv)) { + packed |= 0x7FFFF7FFFF7FFFF; + } else { + // pack the three smaller components into 20bit ints each + int shift = 40; + for (int i = 0; i < 4; ++i) { + if (i != largest_index) { + packed |= uint64_t(int(q[i] * conv) + 524287) << shift; + shift -= 20; + } + } + } + // and write it out + Write(packed, off); +} + +void Packet::Payload::Read(glm::quat &val, size_t off) const noexcept { + // extract the 8 byte packed value + uint64_t packed = 0; + Read(packed, off); + // two most significant bits are the index of the largest (omitted) component + int largest_index = packed >> 62; + // if all other three are 0, largest is 1 and we can omit the conversion + if ((packed & 0xFFFFFFFFFFFFFFF) == 0x7FFFF7FFFF7FFFF) { + val = { 0.0f, 0.0f, 0.0f, 0.0f }; + val[largest_index] = 1.0f; + return; + } + // we have to map from [-524287,524287] to [-0.7072,0.7072] + constexpr float conv = 0.7072f / 524287.0f; + int shift = 40; + for (int i = 0; i < 4; ++i) { + if (i != largest_index) { + val[i] = float(int((packed >> shift) & 0xFFFFF) - 524287) * conv; + shift -= 20; + } else { + // set to zero so the length of the other three can be determined + val[i] = 0.0f; + } + } + // omitted component squared is 1 - length squared of others + val[largest_index] = sqrt(1.0f - glm::length2(val)); + // and already normalized +} + +void Packet::Payload::Write(const EntityState &state, size_t off) noexcept { + Write(state.pos.chunk, off); + WritePackU(state.pos.block * (1.0f / ExactLocation::fscale), off + 12); + Write(state.velocity, off + 18); + Write(state.orient, off + 30); + WritePackN(state.pitch * PI_0p5_inv, off + 38); + WritePackN(state.yaw * PI_inv, off + 40); +} + +void Packet::Payload::Read(EntityState &state, size_t off) const noexcept { + Read(state.pos.chunk, off); + ReadPackU(state.pos.block, off + 12); + Read(state.velocity, off + 18); + Read(state.orient, off + 30); + ReadPackN(state.pitch, off + 38); + ReadPackN(state.yaw, off + 40); + state.pos.block *= ExactLocation::fscale; + state.pitch *= PI_0p5; + state.yaw *= PI; +} + +void Packet::Payload::Write(const EntityState &state, const glm::ivec3 &base, size_t off) noexcept { + WritePackB(state.pos.chunk - base, off); + WritePackU(state.pos.block * (1.0f / ExactLocation::fscale), off + 3); + Write(state.velocity, off + 9); + Write(state.orient, off + 21); + WritePackN(state.pitch * PI_0p5_inv, off + 29); + WritePackN(state.yaw * PI_inv, off + 31); +} + +void Packet::Payload::Read(EntityState &state, const glm::ivec3 &base, size_t off) const noexcept { + ReadPackB(state.pos.chunk, off); + ReadPackU(state.pos.block, off + 3); + Read(state.velocity, off + 9); + Read(state.orient, off + 21); + ReadPackN(state.pitch, off + 29); + ReadPackN(state.yaw, off + 31); + state.pos.chunk += base; + state.pos.block *= ExactLocation::fscale; + state.pitch *= PI_0p5; + state.yaw *= PI; +} + +void Packet::Payload::WritePackB(const glm::ivec3 &val, size_t off) noexcept { + Write(int8_t(val.x), off); + Write(int8_t(val.y), off + 1); + Write(int8_t(val.z), off + 2); +} + +void Packet::Payload::ReadPackB(glm::ivec3 &val, size_t off) const noexcept { + int8_t conv = 0; + Read(conv, off); + val.x = conv; + Read(conv, off + 1); + val.y = conv; + Read(conv, off + 2); + val.z = conv; +} + +void Packet::Payload::WritePackN(float val, size_t off) noexcept { + int16_t raw = glm::clamp(glm::round(val * 32767.0f), -32767.0f, 32767.0f); + Write(raw, off); +} + +void Packet::Payload::ReadPackN(float &val, size_t off) const noexcept { + int16_t raw = 0; + Read(raw, off); + val = raw * (1.0f/32767.0f); +} + +void Packet::Payload::WritePackN(const glm::vec3 &val, size_t off) noexcept { + WritePackN(val.x, off); + WritePackN(val.y, off + 2); + WritePackN(val.z, off + 4); +} + +void Packet::Payload::ReadPackN(glm::vec3 &val, size_t off) const noexcept { + ReadPackN(val.x, off); + ReadPackN(val.y, off + 2); + ReadPackN(val.z, off + 4); +} + +void Packet::Payload::WritePackU(float val, size_t off) noexcept { + uint16_t raw = glm::clamp(glm::round(val * 65535.0f), 0.0f, 65535.0f); + Write(raw, off); +} + +void Packet::Payload::ReadPackU(float &val, size_t off) const noexcept { + uint16_t raw = 0; + Read(raw, off); + val = raw * (1.0f/65535.0f); +} + +void Packet::Payload::WritePackU(const glm::vec3 &val, size_t off) noexcept { + WritePackU(val.x, off); + WritePackU(val.y, off + 2); + WritePackU(val.z, off + 4); +} + +void Packet::Payload::ReadPackU(glm::vec3 &val, size_t off) const noexcept { + ReadPackU(val.x, off); + ReadPackU(val.y, off + 2); + ReadPackU(val.z, off + 4); +} + void Packet::Login::WritePlayerName(const string &name) noexcept { WriteString(name, 0, 32); @@ -573,21 +625,45 @@ void Packet::Join::ReadPlayerState(EntityState &state) const noexcept { } void Packet::Join::WriteWorldName(const string &name) noexcept { - WriteString(name, 68, 32); + WriteString(name, 46, 32); } void Packet::Join::ReadWorldName(string &name) const noexcept { - ReadString(name, 68, 32); + ReadString(name, 46, 32); } -void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept { - Write(player.GetState(), 0); +void Packet::PlayerUpdate::WritePredictedState(const EntityState &state) noexcept { + Write(state, 0); } -void Packet::PlayerUpdate::ReadPlayerState(EntityState &state) const noexcept { +void Packet::PlayerUpdate::ReadPredictedState(EntityState &state) const noexcept { Read(state, 0); } +void Packet::PlayerUpdate::WriteMovement(const glm::vec3 &mov) noexcept { + WritePackN(mov, 42); +} + +void Packet::PlayerUpdate::ReadMovement(glm::vec3 &mov) const noexcept { + ReadPackN(mov, 42); +} + +void Packet::PlayerUpdate::WriteActions(uint8_t actions) noexcept { + Write(actions, 48); +} + +void Packet::PlayerUpdate::ReadActions(uint8_t &actions) const noexcept { + Read(actions, 48); +} + +void Packet::PlayerUpdate::WriteSlot(uint8_t slot) noexcept { + Write(slot, 49); +} + +void Packet::PlayerUpdate::ReadSlot(uint8_t &slot) const noexcept { + Read(slot, 49); +} + void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { Write(e.ID(), 0); if (e.GetModel()) { @@ -596,20 +672,20 @@ void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept { Write(uint32_t(0), 4); } Write(e.GetState(), 8); - Write(e.Bounds(), 72); + Write(e.Bounds(), 50); uint32_t flags = 0; if (e.WorldCollidable()) { flags |= 1; } - Write(flags, 96); - WriteString(e.Name(), 100, 32); + Write(flags, 74); + WriteString(e.Name(), 78, 32); } void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept { Read(id, 0); } -void Packet::SpawnEntity::ReadSkeletonID(uint32_t &id) const noexcept { +void Packet::SpawnEntity::ReadModelID(uint32_t &id) const noexcept { Read(id, 4); } @@ -620,9 +696,9 @@ void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept { string name; Read(state, 8); - Read(bounds, 72); - Read(flags, 96); - ReadString(name, 100, 32); + Read(bounds, 50); + Read(flags, 74); + ReadString(name, 78, 32); e.SetState(state); e.Bounds(bounds); @@ -646,20 +722,29 @@ 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); +void Packet::EntityUpdate::WriteChunkBase(const glm::ivec3 &base) noexcept { + Write(base, 4); +} + +void Packet::EntityUpdate::ReadChunkBase(glm::ivec3 &base) const noexcept { + Read(base, 4); +} + +void Packet::EntityUpdate::WriteEntity(const Entity &entity, const glm::ivec3 &base, uint32_t num) noexcept { + uint32_t off = GetSize(num); Write(entity.ID(), off); - Write(entity.GetState(), off + 4); + Write(entity.GetState(), base, off + 4); } void Packet::EntityUpdate::ReadEntityID(uint32_t &id, uint32_t num) const noexcept { - Read(id, 4 + (num * 64)); + uint32_t off = GetSize(num); + Read(id, off); } -void Packet::EntityUpdate::ReadEntityState(EntityState &state, uint32_t num) const noexcept { - uint32_t off = 4 + (num * 64); - Read(state, off + 4); +void Packet::EntityUpdate::ReadEntityState(EntityState &state, const glm::ivec3 &base, uint32_t num) const noexcept { + uint32_t off = GetSize(num); + Read(state, base, off + 4); } void Packet::PlayerCorrection::WritePacketSeq(std::uint16_t s) noexcept { @@ -678,6 +763,132 @@ void Packet::PlayerCorrection::ReadPlayerState(EntityState &state) const noexcep 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); +} + +void Packet::BlockUpdate::WriteChunkCoords(const glm::ivec3 &coords) noexcept { + Write(coords, 0); +} + +void Packet::BlockUpdate::ReadChunkCoords(glm::ivec3 &coords) const noexcept { + Read(coords, 0); +} + +void Packet::BlockUpdate::WriteBlockCount(uint32_t count) noexcept { + Write(count, 12); +} + +void Packet::BlockUpdate::ReadBlockCount(uint32_t &count) const noexcept { + Read(count, 12); +} + +void Packet::BlockUpdate::WriteIndex(uint16_t index, uint32_t num) noexcept { + uint32_t off = GetSize(num); + Write(index, off); +} + +void Packet::BlockUpdate::ReadIndex(uint16_t &index, uint32_t num) const noexcept { + uint32_t off = GetSize(num); + Read(index, off); +} + +void Packet::BlockUpdate::WriteBlock(const Block &block, uint32_t num) noexcept { + uint32_t off = GetSize(num) + 2; + Write(block, off); +} + +void Packet::BlockUpdate::ReadBlock(Block &block, uint32_t num) const noexcept { + uint32_t off = GetSize(num) + 2; + Read(block, off); +} + +void Packet::Message::WriteType(uint8_t type) noexcept { + Write(type, 0); +} + +void Packet::Message::ReadType(uint8_t &type) const noexcept { + Read(type, 0); +} + +void Packet::Message::WriteReferral(uint32_t ref) noexcept { + Write(ref, 1); +} + +void Packet::Message::ReadReferral(uint32_t &ref) const noexcept { + Read(ref, 1); +} + +void Packet::Message::WriteMessage(const string &msg) noexcept { + WriteString(msg, 5, MAX_MESSAGE_LEN); +} + +void Packet::Message::ReadMessage(string &msg) const noexcept { + ReadString(msg, 5, MAX_MESSAGE_LEN); +} + void ConnectionHandler::Handle(const UDPpacket &udp_pack) { const Packet &pack = *reinterpret_cast(udp_pack.data); @@ -709,79 +920,22 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { 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; + case Packet::BlockUpdate::TYPE: + On(Packet::As(udp_pack)); + break; + case Packet::Message::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; - } - } -} - }