#include #include #include #include #include namespace blitz { namespace server { KeepAliveSystem::KeepAliveSystem(entt::registry& a_Registry) : m_Registry(a_Registry) {} void KeepAliveSystem::Update(Nz::Time elapsedTime) { m_Registry.view().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 dis(0, std::numeric_limits::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(entity); } } }); } } // namespace server } // namespace blitz