X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fserver%2Fnet.cpp;h=e210fef4ff1e7db6cf4a0a2e51f8b0a29f514bb6;hb=dcd54cacda98c2c0f7cf0c7a9131fb858d8ee10a;hp=f7c874f665fb1fbc4f38e190e045b5ae4f96a255;hpb=10a310869c61cc52046e165f36ac9639fe9d0c69;p=blank.git diff --git a/src/server/net.cpp b/src/server/net.cpp index f7c874f..e210fef 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -3,6 +3,7 @@ #include "Server.hpp" #include "../app/init.hpp" +#include "../geometry/distance.hpp" #include "../io/WorldSave.hpp" #include "../model/Model.hpp" #include "../world/ChunkIndex.hpp" @@ -161,7 +162,7 @@ void ChunkTransmitter::SendData(size_t i) { if (data_packets[i] == -1) { ++confirm_wait; } - data_packets[i] = conn.Send(); + data_packets[i] = conn.Send(Packet::ChunkData::GetSize(len)); } void ChunkTransmitter::Release() { @@ -180,13 +181,15 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr) , spawns() , confirm_wait(0) , entity_updates() +, entity_updates_skipped(0) , player_update_state() , player_update_pack(0) , player_update_timer(1500) , old_actions(0) , transmitter(*this) , chunk_queue() -, old_base() { +, old_base() +, chunk_blocks_skipped(0) { conn.SetHandler(this); } @@ -200,58 +203,60 @@ void ClientConnection::Update(int dt) { 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 - QueueUpdate(*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 + CheckPlayerFix(); + CheckChunkQueue(); + CheckEntities(); + SendUpdates(); + } + if (conn.ShouldPing()) { + conn.SendPing(server.GetPacket(), server.GetSocket()); + } +} + +void ClientConnection::CheckEntities() { + 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); - ++local_iter; + } else if (SendingUpdates()) { + // update + QueueUpdate(*local_iter); } - } - - // leftover spawns - while (global_iter != global_end) { + ++global_iter; + ++local_iter; + } else if (global_iter->ID() < local_iter->entity->ID()) { + // global entity was inserted if (CanSpawn(*global_iter)) { - spawns.emplace_back(*global_iter); - SendSpawn(spawns.back()); + auto spawned = spawns.emplace(local_iter, *global_iter); + SendSpawn(*spawned); } ++global_iter; - } - - // leftover despawns - while (local_iter != local_end) { + } else { + // global entity was removed SendDespawn(*local_iter); ++local_iter; } - SendUpdates(); + } - CheckPlayerFix(); - CheckChunkQueue(); + // leftover spawns + while (global_iter != global_end) { + if (CanSpawn(*global_iter)) { + spawns.emplace_back(*global_iter); + SendSpawn(spawns.back()); + } + ++global_iter; } - if (conn.ShouldPing()) { - conn.SendPing(server.GetPacket(), server.GetSocket()); + + // leftover despawns + while (local_iter != local_end) { + SendDespawn(*local_iter); + ++local_iter; } } @@ -308,6 +313,10 @@ void ClientConnection::SendDespawn(SpawnStatus &status) { ++confirm_wait; } +bool ClientConnection::SendingUpdates() const noexcept { + return entity_updates_skipped >= NetStat().SuggestedPacketHold(); +} + void ClientConnection::QueueUpdate(SpawnStatus &status) { // don't send updates while spawn not ack'd or despawn sent if (status.spawn_pack == -1 && status.despawn_pack == -1) { @@ -316,6 +325,11 @@ void ClientConnection::QueueUpdate(SpawnStatus &status) { } void ClientConnection::SendUpdates() { + if (!SendingUpdates()) { + entity_updates.clear(); + ++entity_updates_skipped; + return; + } auto base = PlayerChunks().Base(); auto pack = Prepare(); pack.WriteChunkBase(base); @@ -335,12 +349,13 @@ void ClientConnection::SendUpdates() { Send(Packet::EntityUpdate::GetSize(entity_pos)); } entity_updates.clear(); + entity_updates_skipped = 0; } void ClientConnection::CheckPlayerFix() { // player_update_state's position holds the client's most recent prediction glm::vec3 diff = player_update_state.Diff(PlayerEntity().GetState()); - float dist_squared = dot(diff, diff); + float dist_squared = glm::length2(diff); // if client's prediction is off by more than 1cm, send // our (authoritative) state back so it can fix it @@ -373,9 +388,9 @@ struct QueueCompare { void ClientConnection::CheckChunkQueue() { if (PlayerChunks().Base() != old_base) { - Chunk::Pos begin = PlayerChunks().CoordsBegin(); - Chunk::Pos end = PlayerChunks().CoordsEnd(); - for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) { + ExactLocation::Coarse begin = PlayerChunks().CoordsBegin(); + ExactLocation::Coarse end = PlayerChunks().CoordsEnd(); + for (ExactLocation::Coarse pos = begin; pos.z < end.z; ++pos.z) { for (pos.y = begin.y; pos.y < end.y; ++pos.y) { for (pos.x = begin.x; pos.x < end.x; ++pos.x) { if (manhattan_radius(pos - old_base) > PlayerChunks().Extent()) { @@ -386,21 +401,29 @@ void ClientConnection::CheckChunkQueue() { } old_base = PlayerChunks().Base(); sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base)); + chunk_queue.erase(unique(chunk_queue.begin(), chunk_queue.end()), chunk_queue.end()); + } + // don't push entity updates and chunk data in the same tick + if (chunk_blocks_skipped >= NetStat().SuggestedPacketHold() && !SendingUpdates()) { + ++chunk_blocks_skipped; + return; } if (transmitter.Transmitting()) { transmitter.Transmit(); + chunk_blocks_skipped = 0; return; } if (transmitter.Idle()) { int count = 0; constexpr int max = 64; while (count < max && !chunk_queue.empty()) { - Chunk::Pos pos = chunk_queue.front(); + ExactLocation::Coarse pos = chunk_queue.front(); chunk_queue.pop_front(); if (PlayerChunks().InRange(pos)) { Chunk *chunk = PlayerChunks().Get(pos); if (chunk) { transmitter.Send(*chunk); + chunk_blocks_skipped = 0; return; } else { chunk_queue.push_back(pos); @@ -416,10 +439,12 @@ void ClientConnection::AttachPlayer(Player &player) { input.reset(new DirectInput(server.GetWorld(), player, server)); PlayerEntity().Ref(); + cli_ctx.reset(new NetworkCLIFeedback(player, *this)); + old_base = PlayerChunks().Base(); - Chunk::Pos begin = PlayerChunks().CoordsBegin(); - Chunk::Pos end = PlayerChunks().CoordsEnd(); - for (Chunk::Pos pos = begin; pos.z < end.z; ++pos.z) { + ExactLocation::Coarse begin = PlayerChunks().CoordsBegin(); + ExactLocation::Coarse end = PlayerChunks().CoordsEnd(); + for (ExactLocation::Coarse pos = begin; pos.z < end.z; ++pos.z) { for (pos.y = begin.y; pos.y < end.y; ++pos.y) { for (pos.x = begin.x; pos.x < end.x; ++pos.x) { chunk_queue.push_back(pos); @@ -446,6 +471,7 @@ void ClientConnection::DetachPlayer() { server.GetWorldSave().Write(input->GetPlayer()); PlayerEntity().Kill(); PlayerEntity().UnRef(); + cli_ctx.reset(); input.reset(); transmitter.Abort(); chunk_queue.clear(); @@ -558,10 +584,16 @@ void ClientConnection::On(const Packet::PlayerUpdate &pack) { pack.ReadActions(new_actions); pack.ReadSlot(slot); + // accept client's orientation as is + input->GetPlayer().GetEntity().Orientation(player_update_state.orient); + // simulate movement input->SetMovement(movement); + // rotate head to match client's "prediction" input->TurnHead(player_update_state.pitch - input->GetPitch(), player_update_state.yaw - input->GetYaw()); + // select the given inventory slot input->SelectInventory(slot); + // check if any actions have been started or stopped if ((new_actions & 0x01) && !(old_actions & 0x01)) { input->StartPrimaryAction(); } else if (!(new_actions & 0x01) && (old_actions & 0x01)) { @@ -584,6 +616,14 @@ bool ClientConnection::ChunkInRange(const glm::ivec3 &pos) const noexcept { return HasPlayer() && PlayerChunks().InRange(pos); } +void ClientConnection::On(const Packet::ChunkBegin &pack) { + glm::ivec3 pos; + pack.ReadChunkCoords(pos); + if (ChunkInRange(pos)) { + chunk_queue.push_front(pos); + } +} + void ClientConnection::On(const Packet::Message &pack) { uint8_t type; uint32_t ref; @@ -592,11 +632,38 @@ void ClientConnection::On(const Packet::Message &pack) { pack.ReadReferral(ref); pack.ReadMessage(msg); - if (type == 1 && HasPlayer()) { - server.DispatchMessage(input->GetPlayer(), msg); + if (type == 1 && cli_ctx) { + server.DispatchMessage(*cli_ctx, msg); } } +uint16_t ClientConnection::SendMessage(uint8_t type, uint32_t from, const string &msg) { + auto pack = Prepare(); + pack.WriteType(type); + pack.WriteReferral(from); + pack.WriteMessage(msg); + return Send(Packet::Message::GetSize(msg)); +} + + +NetworkCLIFeedback::NetworkCLIFeedback(Player &p, ClientConnection &c) +: CLIContext(p) +, conn(c) { + +} + +void NetworkCLIFeedback::Error(const string &msg) { + conn.SendMessage(0, 0, msg); +} + +void NetworkCLIFeedback::Message(const string &msg) { + conn.SendMessage(0, 0, msg); +} + +void NetworkCLIFeedback::Broadcast(const string &msg) { + conn.GetServer().DistributeMessage(0, GetPlayer().GetEntity().ID(), msg); +} + Server::Server( const Config::Network &conf, @@ -605,28 +672,54 @@ Server::Server( const WorldSave &save) : serv_sock(nullptr) , serv_pack{ -1, nullptr, 0 } +, serv_set(SDLNet_AllocSocketSet(1)) , clients() , world(world) , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3)) , save(save) , player_model(nullptr) , cli(world) { + if (!serv_set) { + throw NetError("SDLNet_AllocSocketSet"); + } + serv_sock = SDLNet_UDP_Open(conf.port); if (!serv_sock) { + SDLNet_FreeSocketSet(serv_set); throw NetError("SDLNet_UDP_Open"); } + if (SDLNet_UDP_AddSocket(serv_set, serv_sock) == -1) { + SDLNet_UDP_Close(serv_sock); + SDLNet_FreeSocketSet(serv_set); + throw NetError("SDLNet_UDP_AddSocket"); + } + serv_pack.data = new Uint8[sizeof(Packet)]; serv_pack.maxlen = sizeof(Packet); } Server::~Server() { + for (ClientConnection &client : clients) { + client.Disconnected(); + } + clients.clear(); world.Chunks().UnregisterIndex(spawn_index); delete[] serv_pack.data; + SDLNet_UDP_DelSocket(serv_set, serv_sock); SDLNet_UDP_Close(serv_sock); + SDLNet_FreeSocketSet(serv_set); } +void Server::Wait(int dt) noexcept { + SDLNet_CheckSockets(serv_set, dt); +} + +bool Server::Ready() noexcept { + return SDLNet_CheckSockets(serv_set, 0) > 0; +} + void Server::Handle() { int result = SDLNet_UDP_Recv(serv_sock, &serv_pack); while (result > 0) { @@ -725,14 +818,14 @@ void Server::SetBlock(Chunk &chunk, int index, const Block &block) { } } -void Server::DispatchMessage(Player &player, const string &msg) { +void Server::DispatchMessage(CLIContext &ctx, const string &msg) { if (msg.empty()) { return; } if (msg[0] == '/' && msg.size() > 1 && msg[1] != '/') { - cli.Execute(player, msg.substr(1)); + cli.Execute(ctx, msg.substr(1)); } else { - DistributeMessage(1, player.GetEntity().ID(), msg); + DistributeMessage(1, ctx.GetPlayer().GetEntity().ID(), msg); } }