From: Daniel Karbach Date: Fri, 18 Sep 2015 14:41:53 +0000 (+0200) Subject: group entity updates in as few packets as possible X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=38a4cffc0b6aa58e49d24c06aad7bee14cb6515d;p=blank.git group entity updates in as few packets as possible --- diff --git a/src/client/client.cpp b/src/client/client.cpp index 52bc0a9..7dac3ad 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -396,7 +396,8 @@ void MasterState::On(const Packet::SpawnEntity &pack) { if (skel) { skel->Instantiate(entity.GetModel()); } - cout << "spawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl; + cout << "spawned entity #" << entity_id << " (" << entity.Name() + << ") at " << entity.AbsolutePosition() << endl; } void MasterState::On(const Packet::DespawnEntity &pack) { @@ -411,7 +412,7 @@ void MasterState::On(const Packet::DespawnEntity &pack) { for (Entity &entity : state->GetWorld().Entities()) { if (entity.ID() == entity_id) { entity.Kill(); - cout << "despawned entity " << entity.Name() << " at " << entity.AbsolutePosition() << endl; + cout << "despawned entity #" << entity_id << " (" << entity.Name() << ") at " << entity.AbsolutePosition() << endl; return; } } diff --git a/src/server/ClientConnection.hpp b/src/server/ClientConnection.hpp index c5efb51..2f03e67 100644 --- a/src/server/ClientConnection.hpp +++ b/src/server/ClientConnection.hpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace blank { @@ -77,7 +78,8 @@ private: void SendSpawn(SpawnStatus &); void SendDespawn(SpawnStatus &); - void SendUpdate(SpawnStatus &); + void QueueUpdate(SpawnStatus &); + void SendUpdates(); void CheckPlayerFix(); @@ -90,6 +92,8 @@ private: std::list spawns; unsigned int confirm_wait; + std::vector entity_updates; + EntityState player_update_state; std::uint16_t player_update_pack; IntervalTimer player_update_timer; diff --git a/src/server/net.cpp b/src/server/net.cpp index 0cf0cdd..abe00a9 100644 --- a/src/server/net.cpp +++ b/src/server/net.cpp @@ -174,6 +174,7 @@ ClientConnection::ClientConnection(Server &server, const IPaddress &addr) , player(nullptr, nullptr) , spawns() , confirm_wait(0) +, entity_updates() , player_update_state() , player_update_pack(0) , player_update_timer(1500) @@ -206,7 +207,7 @@ void ClientConnection::Update(int dt) { SendDespawn(*local_iter); } else { // update - SendUpdate(*local_iter); + QueueUpdate(*local_iter); } ++global_iter; ++local_iter; @@ -238,6 +239,7 @@ void ClientConnection::Update(int dt) { SendDespawn(*local_iter); ++local_iter; } + SendUpdates(); CheckPlayerFix(); CheckChunkQueue(); @@ -300,15 +302,31 @@ void ClientConnection::SendDespawn(SpawnStatus &status) { ++confirm_wait; } -void ClientConnection::SendUpdate(SpawnStatus &status) { +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) return; + if (status.spawn_pack == -1 && status.despawn_pack == -1) { + entity_updates.push_back(&status); + } +} - // TODO: pack entity updates +void ClientConnection::SendUpdates() { auto pack = Prepare(); - pack.WriteEntityCount(1); - pack.WriteEntity(*status.entity, 0); - Send(Packet::EntityUpdate::GetSize(1)); + int entity_pos = 0; + for (SpawnStatus *status : entity_updates) { + pack.WriteEntity(*status->entity, entity_pos); + ++entity_pos; + if (entity_pos == Packet::EntityUpdate::MAX_ENTITIES) { + pack.WriteEntityCount(entity_pos); + Send(Packet::EntityUpdate::GetSize(entity_pos)); + pack = Prepare(); + entity_pos = 0; + } + } + if (entity_pos > 0) { + pack.WriteEntityCount(entity_pos); + Send(Packet::EntityUpdate::GetSize(entity_pos)); + } + entity_updates.clear(); } void ClientConnection::CheckPlayerFix() {