fully implement KeepAlive behavior

This commit is contained in:
2024-07-21 20:59:13 +02:00
parent 36a2e67ac4
commit 92a2e53036
25 changed files with 513 additions and 61 deletions

View File

@@ -0,0 +1,38 @@
#include <server/systems/KeepAliveSystem.h>
#include <server/components/Disconnect.h>
#include <server/components/EnetConnection.h>
#include <server/components/KeepAliveSession.h>
#include <random>
namespace blitz {
namespace server {
KeepAliveSystem::KeepAliveSystem(entt::registry& a_Registry) : m_Registry(a_Registry) {}
void KeepAliveSystem::Update(Nz::Time elapsedTime) {
m_Registry.view<KeepAliveSessionComponent, EnetConnectionComponent>().each(
[this](auto entity, auto& keepAliveSession, auto& connection) {
auto duration = Nz::GetElapsedMilliseconds() - keepAliveSession.m_LastTime;
if (duration > Nz::Time::Seconds(10)) {
if (keepAliveSession.m_RecievedResponse) {
keepAliveSession.m_RecievedResponse = false;
keepAliveSession.m_LastTime = Nz::GetElapsedMilliseconds();
std::random_device rd;
std::uniform_int_distribution<std::uint64_t> dis(0, std::numeric_limits<std::uint64_t>::max());
std::uint64_t keepAliveId = dis(rd);
keepAliveSession.m_LastKeepAliveId = keepAliveId;
connection.m_Connection->SendKeepAlive({keepAliveId});
} else {
// We kick the player because he's not responding anymore
m_Registry.emplace<DisconnectComponent>(entity);
}
}
});
}
} // namespace server
} // namespace blitz