]> git.localhorst.tv Git - blank.git/blobdiff - src/server/net.cpp
don't send chunks and entities simultaneously while skipping
[blank.git] / src / server / net.cpp
index cdd1a0a6092c47d9e9dab5b83f1474245c74ac3a..aa1bdeb89448c46c757f6a4f47eb283c730ce581 100644 (file)
@@ -180,6 +180,7 @@ 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)
@@ -211,7 +212,7 @@ void ClientConnection::Update(int dt) {
                                // they're the same
                                if (CanDespawn(*global_iter)) {
                                        SendDespawn(*local_iter);
-                               } else {
+                               } else if (SendingUpdates()) {
                                        // update
                                        QueueUpdate(*local_iter);
                                }
@@ -247,7 +248,6 @@ void ClientConnection::Update(int dt) {
                }
                SendUpdates();
 
-               input->Update(dt);
                CheckPlayerFix();
                CheckChunkQueue();
        }
@@ -309,6 +309,10 @@ void ClientConnection::SendDespawn(SpawnStatus &status) {
        ++confirm_wait;
 }
 
+bool ClientConnection::SendingUpdates() const noexcept {
+       return entity_updates_skipped >= NetStat().SuggestedPacketSkip();
+}
+
 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) {
@@ -317,6 +321,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);
@@ -336,6 +345,7 @@ void ClientConnection::SendUpdates() {
                Send(Packet::EntityUpdate::GetSize(entity_pos));
        }
        entity_updates.clear();
+       entity_updates_skipped = 0;
 }
 
 void ClientConnection::CheckPlayerFix() {
@@ -388,6 +398,11 @@ void ClientConnection::CheckChunkQueue() {
                old_base = PlayerChunks().Base();
                sort(chunk_queue.begin(), chunk_queue.end(), QueueCompare(old_base));
        }
+       // if we have packet skip enabled and just pushed an entity
+       // update, don't also send chunk data
+       if (NetStat().SuggestedPacketSkip() > 0 && entity_updates_skipped == 0) {
+               return;
+       }
        if (transmitter.Transmitting()) {
                transmitter.Transmit();
                return;
@@ -594,7 +609,7 @@ void ClientConnection::On(const Packet::Message &pack) {
        pack.ReadMessage(msg);
 
        if (type == 1 && HasPlayer()) {
-               server.DistributeMessage(1, PlayerEntity().ID(), msg);
+               server.DispatchMessage(input->GetPlayer(), msg);
        }
 }
 
@@ -610,7 +625,8 @@ Server::Server(
 , world(world)
 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
 , save(save)
-, player_model(nullptr) {
+, player_model(nullptr)
+, cli(world) {
        serv_sock = SDLNet_UDP_Open(conf.port);
        if (!serv_sock) {
                throw NetError("SDLNet_UDP_Open");
@@ -725,6 +741,17 @@ void Server::SetBlock(Chunk &chunk, int index, const Block &block) {
        }
 }
 
+void Server::DispatchMessage(Player &player, const string &msg) {
+       if (msg.empty()) {
+               return;
+       }
+       if (msg[0] == '/' && msg.size() > 1 && msg[1] != '/') {
+               cli.Execute(player, msg.substr(1));
+       } else {
+               DistributeMessage(1, player.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);