39 lines
1.3 KiB
C++
39 lines
1.3 KiB
C++
#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
|