]> git.localhorst.tv Git - blank.git/blobdiff - src/server/net.cpp
add TCP based CLI
[blank.git] / src / server / net.cpp
index 38224ec9a57e879e2a84357a77456b30253f3f1c..f5e974d7e71bcfb9808e08a7d9fc909aa1dbe2d8 100644 (file)
@@ -3,8 +3,10 @@
 #include "Server.hpp"
 
 #include "../app/init.hpp"
+#include "../geometry/distance.hpp"
 #include "../io/WorldSave.hpp"
 #include "../model/Model.hpp"
+#include "../shared/CommandService.hpp"
 #include "../world/ChunkIndex.hpp"
 #include "../world/Entity.hpp"
 #include "../world/World.hpp"
@@ -161,7 +163,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 +182,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 +204,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 +314,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 +326,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<Packet::EntityUpdate>();
        pack.WriteChunkBase(base);
@@ -335,12 +350,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 +389,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 +402,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 +440,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 +472,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 +585,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 +617,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,12 +633,43 @@ void ClientConnection::On(const Packet::Message &pack) {
        pack.ReadReferral(ref);
        pack.ReadMessage(msg);
 
-       if (type == 1 && HasPlayer()) {
-               server.DistributeMessage(1, PlayerEntity().ID(), 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<Packet::Message>();
+       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);
+}
+
+
+// relying on {} zero intitialization for UDPpacket, because
+// the type and number of fields is not well defined
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
 Server::Server(
        const Config::Network &conf,
        World &world,
@@ -605,27 +677,66 @@ 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) {
+, player_model(nullptr)
+, cli(world)
+, cmd_srv() {
+#pragma GCC diagnostic pop
+       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);
+
+       if (conf.cmd_port) {
+               cmd_srv.reset(new CommandService(cli, conf.cmd_port));
+       }
 }
 
 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);
+       if (cmd_srv) {
+               cmd_srv->Wait(0);
+       }
+}
+
+bool Server::Ready() noexcept {
+       if (SDLNet_CheckSockets(serv_set, 0) > 0) {
+               return true;
+       }
+       return cmd_srv && cmd_srv->Ready();
+}
+
 void Server::Handle() {
        int result = SDLNet_UDP_Recv(serv_sock, &serv_pack);
        while (result > 0) {
@@ -636,6 +747,9 @@ void Server::Handle() {
                // a boo boo happened
                throw NetError("SDLNet_UDP_Recv");
        }
+       if (cmd_srv) {
+               cmd_srv->Handle();
+       }
 }
 
 void Server::HandlePacket(const UDPpacket &udp_pack) {
@@ -675,6 +789,9 @@ void Server::Update(int dt) {
                        ++client;
                }
        }
+       if (cmd_srv) {
+               cmd_srv->Send();
+       }
 }
 
 void Server::SetPlayerModel(const Model &m) noexcept {
@@ -724,6 +841,17 @@ void Server::SetBlock(Chunk &chunk, int index, const Block &block) {
        }
 }
 
+void Server::DispatchMessage(CLIContext &ctx, const string &msg) {
+       if (msg.empty()) {
+               return;
+       }
+       if (msg[0] == '/' && msg.size() > 1 && msg[1] != '/') {
+               cli.Execute(ctx, msg.substr(1));
+       } else {
+               DistributeMessage(1, ctx.GetPlayer().GetEntity().ID(), msg);
+       }
+}
+
 void Server::DistributeMessage(uint8_t type, uint32_t ref, const string &msg) {
        auto pack = Packet::Make<Packet::Message>(serv_pack);
        pack.WriteType(type);