diff --git a/include/blitz/network/EnetClient.h b/include/blitz/network/EnetClient.h index 2ca3148..4f8ae16 100644 --- a/include/blitz/network/EnetClient.h +++ b/include/blitz/network/EnetClient.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include namespace blitz { @@ -18,12 +18,12 @@ class EnetClient : private NonCopyable { NazaraSignal(OnDisconnect); NazaraSignal(OnDisconnectTimeout); - const EnetConnexion& GetConnexion() const { - return m_Connexion; + EnetConnection& GetConnection() { + return m_Connection; } private: - EnetConnexion m_Connexion; + EnetConnection m_Connection; Nz::ENetHost m_Host; Nz::ENetPeer* m_Peer; std::thread m_Thread; diff --git a/include/blitz/network/EnetConnexion.h b/include/blitz/network/EnetConnection.h similarity index 84% rename from include/blitz/network/EnetConnexion.h rename to include/blitz/network/EnetConnection.h index 77575ba..00b0850 100644 --- a/include/blitz/network/EnetConnexion.h +++ b/include/blitz/network/EnetConnection.h @@ -1,8 +1,8 @@ #pragma once /** - * \file EnetConnexion.h - * \brief File containing the blitz::network::EnetConnexion class + * \file EnetConnection.h + * \brief File containing the blitz::network::EnetConnection class */ #include @@ -35,12 +35,14 @@ class EnetServer; -class EnetConnexion : private NonCopyable { +class EnetConnection : private NonCopyable { public: - EnetConnexion(Nz::ENetPeer* a_Peer = nullptr); + EnetConnection(Nz::ENetPeer* a_Peer = nullptr); bool IsConnected() const; + std::uint16_t GetPeerId() const; + DeclareAllPacket(); private: diff --git a/include/blitz/network/EnetServer.h b/include/blitz/network/EnetServer.h index 915a90b..d50df8d 100644 --- a/include/blitz/network/EnetServer.h +++ b/include/blitz/network/EnetServer.h @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include @@ -17,24 +17,24 @@ class EnetServer : private NonCopyable { void Destroy(); - // void BroadcastPacket(const protocol::Packet& a_Packet, Nz::ENetPacketFlags a_Flags); + void CloseConnection(std::uint16_t a_PeerId); - EnetConnexion* GetConnexion(std::uint16_t a_PeerId); + EnetConnection* GetConnection(std::uint16_t a_PeerId); - NazaraSignal(OnClientConnect, EnetConnexion& /*a_Peer*/); - NazaraSignal(OnClientDisconnect, EnetConnexion& /*a_Peer*/); - NazaraSignal(OnClientDisconnectTimeout, EnetConnexion& /*a_Peer*/); + NazaraSignal(OnClientConnect, EnetConnection& /*a_Peer*/); + NazaraSignal(OnClientDisconnect, EnetConnection& /*a_Peer*/); + NazaraSignal(OnClientDisconnectTimeout, EnetConnection& /*a_Peer*/); private: void Update(); void WorkerThread(); - void RemoveConnexion(std::uint16_t a_PeerId); + void RemoveConnection(std::uint16_t a_PeerId); Nz::ENetHost m_Host; bool m_Running; std::thread m_Thread; - std::map> m_Connexion; + std::map> m_Connections; }; } // namespace network diff --git a/include/blitz/protocol/PacketHandler.h b/include/blitz/protocol/PacketHandler.h new file mode 100644 index 0000000..4383b65 --- /dev/null +++ b/include/blitz/protocol/PacketHandler.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +namespace blitz { + +using EnttWorld = std::atomic>; + +namespace protocol { + +class PacketHandler : private NonCopyable { + public: + PacketHandler(network::EnetConnection& a_Connection, EnttWorld& a_World); + virtual ~PacketHandler(); + + protected: + network::EnetConnection& m_Connection; + EnttWorld& m_World; +}; + +} // namespace protocol +} // namespace blitz diff --git a/include/client/Client.h b/include/client/Client.h new file mode 100644 index 0000000..68c9f50 --- /dev/null +++ b/include/client/Client.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include +#include + +namespace blitz { +namespace client { + +class Client : private NonCopyable { + public: + Client(); + ~Client(); + + void Connect(const Nz::IpAddress& a_Ip); + void Disconnect(); + + bool IsConnected(); + + private: + EnttWorld m_World; + std::unique_ptr m_NetworkClient; + std::vector> m_Handlers; + + void BindHandlers(); + void UnbindHandlers(); +}; + +} // namespace client +} // namespace blitz diff --git a/include/client/handlers/KeepAliveHandler.h b/include/client/handlers/KeepAliveHandler.h new file mode 100644 index 0000000..e63eafe --- /dev/null +++ b/include/client/handlers/KeepAliveHandler.h @@ -0,0 +1,15 @@ +#include + +namespace blitz { +namespace client { + +class KeepAliveHandler : public protocol::PacketHandler { + public: + KeepAliveHandler(network::EnetConnection& a_Connection, EnttWorld& a_World); + ~KeepAliveHandler(); + + NazaraSlot(network::EnetConnection, OnKeepAlive, m_Slot); +}; + +} // namespace client +} // namespace blitz diff --git a/include/server/Server.h b/include/server/Server.h new file mode 100644 index 0000000..1e9525f --- /dev/null +++ b/include/server/Server.h @@ -0,0 +1,37 @@ +#pragma once + +#include +#include + +namespace blitz { +namespace server { + +class Server { + public: + Server(std::uint16_t a_Port, std::shared_ptr a_World); + ~Server(); + + network::EnetConnection* GetConnection(std::uint16_t a_PeerId); + + void CloseConnection(std::uint16_t a_PeerId); + + private: + void HandleConnect(network::EnetConnection&); + void HandleDisconnect(network::EnetConnection&); + + struct Session { + network::EnetConnection* m_Connection; + std::vector> m_Handlers; + }; + + EnttWorld m_World; + std::map m_Sessions; + network::EnetServer m_NetworkServer; + + void RegisterHandlers(Session& session); + void RegisterSystems(); + void CreateEntity(network::EnetConnection&); +}; + +} // namespace server +} // namespace blitz diff --git a/include/server/components/Disconnect.h b/include/server/components/Disconnect.h new file mode 100644 index 0000000..059d52c --- /dev/null +++ b/include/server/components/Disconnect.h @@ -0,0 +1,13 @@ +#pragma once + +namespace blitz { +namespace server { + +enum Reason { Timeout }; + +struct DisconnectComponent { + Reason m_Reason; +}; + +} // namespace server +} // namespace blitz diff --git a/include/server/components/EnetConnection.h b/include/server/components/EnetConnection.h new file mode 100644 index 0000000..61bccc6 --- /dev/null +++ b/include/server/components/EnetConnection.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace blitz { +namespace server { + +struct EnetConnectionComponent { + network::EnetConnection* m_Connection; +}; + +} // namespace server +} // namespace blitz diff --git a/include/server/components/KeepAliveSession.h b/include/server/components/KeepAliveSession.h new file mode 100644 index 0000000..c0cc33f --- /dev/null +++ b/include/server/components/KeepAliveSession.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace blitz { +namespace server { + +struct KeepAliveSessionComponent { + std::uint16_t m_PeerId; + std::uint64_t m_LastKeepAliveId; + Nz::Time m_LastTime; + bool m_RecievedResponse = false; +}; + +} // namespace server +} // namespace blitz diff --git a/include/server/handlers/KeepAliveHandler.h b/include/server/handlers/KeepAliveHandler.h new file mode 100644 index 0000000..31065e5 --- /dev/null +++ b/include/server/handlers/KeepAliveHandler.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +namespace blitz { +namespace server { + +class KeepAliveHandler : public protocol::PacketHandler { + public: + KeepAliveHandler(network::EnetConnection& a_Connection, EnttWorld& a_World); + ~KeepAliveHandler(); + + NazaraSlot(network::EnetConnection, OnKeepAlive, m_Slot); + + private: + void Handle(std::uint16_t, const protocol::data::KeepAlive&); +}; + +} // namespace server +} // namespace blitz diff --git a/include/server/systems/DisconnectSystem.h b/include/server/systems/DisconnectSystem.h new file mode 100644 index 0000000..049ba03 --- /dev/null +++ b/include/server/systems/DisconnectSystem.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +namespace blitz { +namespace server { + +class Server; + +class DisconectSystem { + public: + DisconectSystem(entt::registry& a_Registry, Server& a_Server); + + void Update(Nz::Time elapsedTime); + + private: + entt::registry& m_Registry; + Server& m_Server; +}; + +} // namespace server +} // namespace blitz diff --git a/include/server/systems/KeepAliveSystem.h b/include/server/systems/KeepAliveSystem.h new file mode 100644 index 0000000..b2426c0 --- /dev/null +++ b/include/server/systems/KeepAliveSystem.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace blitz { +namespace server { + +class KeepAliveSystem { + public: + KeepAliveSystem(entt::registry& a_Registry); + + void Update(Nz::Time elapsedTime); + + private: + entt::registry& m_Registry; +}; + +} // namespace server +} // namespace blitz diff --git a/src/blitz/network/EnetClient.cpp b/src/blitz/network/EnetClient.cpp index 9fd7444..d0448c3 100644 --- a/src/blitz/network/EnetClient.cpp +++ b/src/blitz/network/EnetClient.cpp @@ -1,18 +1,13 @@ #include -#include - namespace blitz { namespace network { - - - EnetClient::EnetClient(const Nz::IpAddress& address) : m_Running(true) { m_Host.Create(Nz::IpAddress::LoopbackIpV4, 1); m_Peer = m_Host.Connect(address); m_Thread = std::thread(&EnetClient::WorkerThread, this); - m_Connexion.SetPeer(m_Peer); + m_Connection.SetPeer(m_Peer); } EnetClient::~EnetClient() { @@ -25,7 +20,7 @@ EnetClient::~EnetClient() { void EnetClient::Disconnect() { m_Peer->DisconnectNow(0); - m_Connexion.SetPeer(nullptr); + m_Connection.SetPeer(nullptr); } void EnetClient::WorkerThread() { @@ -53,7 +48,7 @@ void EnetClient::Update() { break; case Nz::ENetEventType::Receive: - m_Connexion.Recieve(event.packet.m_packet->data); + m_Connection.Recieve(event.packet.m_packet->data); break; case Nz::ENetEventType::None: diff --git a/src/blitz/network/EnetConnexion.cpp b/src/blitz/network/EnetConnection.cpp similarity index 57% rename from src/blitz/network/EnetConnexion.cpp rename to src/blitz/network/EnetConnection.cpp index edcc326..5875280 100644 --- a/src/blitz/network/EnetConnexion.cpp +++ b/src/blitz/network/EnetConnection.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -11,20 +11,17 @@ namespace packets = blitz::protocol::packets; #define DeclarePacket(PacketName, ...) \ - void Visit(const protocol::packets::PacketName& a_Packet) { \ - m_Connexion.On##PacketName(a_Packet.m_Data); \ - } + void Visit(const protocol::packets::PacketName& a_Packet) { m_Connection.On##PacketName(a_Packet.m_Data); } class PacketDispatcher : public protocol::PacketVisitor { public: - PacketDispatcher(EnetConnexion& a_Connexion) : m_Connexion(a_Connexion) {} + PacketDispatcher(EnetConnection& a_Connection) : m_Connection(a_Connection) {} DeclareAllPacket() - private: - EnetConnexion& m_Connexion; + private : EnetConnection& m_Connection; }; #undef DeclarePacket @@ -33,19 +30,24 @@ class PacketDispatcher : public protocol::PacketVisitor { -EnetConnexion::EnetConnexion(Nz::ENetPeer* a_Peer) : m_Peer(a_Peer) {} +EnetConnection::EnetConnection(Nz::ENetPeer* a_Peer) : m_Peer(a_Peer) {} -void EnetConnexion::SetPeer(Nz::ENetPeer* a_Peer) { +void EnetConnection::SetPeer(Nz::ENetPeer* a_Peer) { m_Peer = a_Peer; } -bool EnetConnexion::IsConnected() const { +std::uint16_t EnetConnection::GetPeerId() const { + assert(m_Peer); + return m_Peer->GetPeerId(); +} + +bool EnetConnection::IsConnected() const { if (!m_Peer) return false; return m_Peer->IsConnected(); } -void EnetConnexion::Recieve(Nz::ByteArray& a_Data) { +void EnetConnection::Recieve(Nz::ByteArray& a_Data) { auto packet = protocol::PacketSerializer::Deserialize(a_Data); if (!packet) return; @@ -54,8 +56,8 @@ void EnetConnexion::Recieve(Nz::ByteArray& a_Data) { } #define DeclarePacket(Name, NFlag, ...) \ - void EnetConnexion::Send##Name(const blitz::protocol::data::Name& a_##Name) const { \ - m_Peer->Send(0, Nz::ENetPacketFlag::NFlag, protocol::PacketSerializer::Serialize(protocol::packets::Name(a_##Name))); \ + void EnetConnection::Send##Name(const blitz::protocol::data::Name& a_##Name) const { \ + m_Peer->Send(0, Nz::ENetPacketFlag::NFlag, protocol::PacketSerializer::Serialize(protocol::packets::Name(a_##Name))); \ } DeclareAllPacket() diff --git a/src/blitz/network/EnetServer.cpp b/src/blitz/network/EnetServer.cpp index 856f935..2b3ba63 100644 --- a/src/blitz/network/EnetServer.cpp +++ b/src/blitz/network/EnetServer.cpp @@ -29,34 +29,36 @@ void EnetServer::Update() { do { switch (event.type) { case Nz::ENetEventType::Disconnect: { - EnetConnexion* connexion = GetConnexion(event.peer->GetPeerId()); - if (!connexion) + EnetConnection* connection = GetConnection(event.peer->GetPeerId()); + if (!connection) break; - OnClientDisconnect(*connexion); + OnClientDisconnect(*connection); + RemoveConnection(connection->GetPeerId()); break; } case Nz::ENetEventType::DisconnectTimeout: { - EnetConnexion* connexion = GetConnexion(event.peer->GetPeerId()); - if (!connexion) + EnetConnection* connection = GetConnection(event.peer->GetPeerId()); + if (!connection) break; - OnClientDisconnectTimeout(*connexion); + OnClientDisconnectTimeout(*connection); + RemoveConnection(connection->GetPeerId()); break; } case Nz::ENetEventType::IncomingConnect: { Nz::ENetPeer* peer = event.peer; - auto con = std::make_unique(peer); - m_Connexion.insert({peer->GetPeerId(), std::move(con)}); - OnClientConnect(*GetConnexion(peer->GetPeerId())); + auto con = std::make_unique(peer); + m_Connections.insert({peer->GetPeerId(), std::move(con)}); + OnClientConnect(*GetConnection(peer->GetPeerId())); break; } case Nz::ENetEventType::Receive: { - EnetConnexion* connexion = GetConnexion(event.peer->GetPeerId()); - if (!connexion) + EnetConnection* connection = GetConnection(event.peer->GetPeerId()); + if (!connection) break; - connexion->Recieve(event.packet.m_packet->data); + connection->Recieve(event.packet.m_packet->data); break; } @@ -68,21 +70,28 @@ void EnetServer::Update() { } } -EnetConnexion* EnetServer::GetConnexion(std::uint16_t a_PeerId) { - auto it = m_Connexion.find(a_PeerId); +EnetConnection* EnetServer::GetConnection(std::uint16_t a_PeerId) { + auto it = m_Connections.find(a_PeerId); - if (it == m_Connexion.end()) + if (it == m_Connections.end()) return nullptr; return it->second.get(); } -/*void EnetServer::BroadcastPacket(const protocol::Packet& a_Packet, Nz::ENetPacketFlags a_Flags) { - m_Host.Broadcast(0, a_Flags, std::move(a_Data)); -}*/ +void EnetServer::CloseConnection(std::uint16_t a_PeerId) { + auto connection = GetConnection(a_PeerId); -void EnetServer::RemoveConnexion(std::uint16_t a_PeerId) { - m_Connexion.erase(a_PeerId); + if (!connection) + return; + + connection->m_Peer->DisconnectNow(0); + OnClientDisconnect(*connection); + RemoveConnection(a_PeerId); +} + +void EnetServer::RemoveConnection(std::uint16_t a_PeerId) { + m_Connections.erase(a_PeerId); } void EnetServer::Destroy() { diff --git a/src/blitz/protocol/PacketHandler.cpp b/src/blitz/protocol/PacketHandler.cpp new file mode 100644 index 0000000..6924bf1 --- /dev/null +++ b/src/blitz/protocol/PacketHandler.cpp @@ -0,0 +1,12 @@ +#include + +namespace blitz { +namespace protocol { + +PacketHandler::PacketHandler(network::EnetConnection& a_Connection, EnttWorld& a_World) : + m_Connection(a_Connection), m_World(a_World) {} + +PacketHandler::~PacketHandler() {} + +} // namespace protocol +} // namespace blitz diff --git a/src/blitz/protocol/PacketSerializer.cpp b/src/blitz/protocol/PacketSerializer.cpp index d26e96b..c798a44 100644 --- a/src/blitz/protocol/PacketSerializer.cpp +++ b/src/blitz/protocol/PacketSerializer.cpp @@ -4,8 +4,6 @@ #include #include -#include - namespace blitz { namespace protocol { diff --git a/src/client/Client.cpp b/src/client/Client.cpp new file mode 100644 index 0000000..24f89bf --- /dev/null +++ b/src/client/Client.cpp @@ -0,0 +1,45 @@ +#include + +#include + +namespace blitz { +namespace client { + +Client::Client() : m_World() { + m_World.store(std::make_shared()); +} + +Client::~Client() { + Disconnect(); +} + +void Client::BindHandlers() { + m_Handlers.push_back(std::make_unique(m_NetworkClient->GetConnection(), m_World)); +} + +void Client::UnbindHandlers() { + m_Handlers.clear(); +} + +void Client::Connect(const Nz::IpAddress& a_Ip) { + m_NetworkClient = std::make_unique(a_Ip); + BindHandlers(); +} + +void Client::Disconnect() { + if (!m_NetworkClient) + return; + m_NetworkClient->Disconnect(); + UnbindHandlers(); + m_NetworkClient.reset(nullptr); +} + +bool Client::IsConnected() { + if (!m_NetworkClient) + return false; + + return m_NetworkClient->GetConnection().IsConnected(); +} + +} // namespace client +} // namespace blitz diff --git a/src/client/handlers/KeepAliveHandler.cpp b/src/client/handlers/KeepAliveHandler.cpp new file mode 100644 index 0000000..4f81fbc --- /dev/null +++ b/src/client/handlers/KeepAliveHandler.cpp @@ -0,0 +1,15 @@ +#include + +namespace blitz { +namespace client { + +KeepAliveHandler::KeepAliveHandler(network::EnetConnection& a_Connection, EnttWorld& a_World) : + protocol::PacketHandler(a_Connection, a_World) { + m_Slot.Connect(m_Connection.OnKeepAlive, + [this](const blitz::protocol::data::KeepAlive& a_KeepAlive) { m_Connection.SendKeepAlive(a_KeepAlive); }); +} + +KeepAliveHandler::~KeepAliveHandler() {} + +} // namespace client +} // namespace blitz diff --git a/src/server/Server.cpp b/src/server/Server.cpp new file mode 100644 index 0000000..d1abd97 --- /dev/null +++ b/src/server/Server.cpp @@ -0,0 +1,70 @@ +#include + +#include +#include +#include +#include +#include + +namespace blitz { +namespace server { + +Server::Server(std::uint16_t a_Port, std::shared_ptr a_World) : m_NetworkServer(a_Port) { + m_World.store(a_World); + RegisterSystems(); + m_NetworkServer.OnClientConnect.Connect(this, &Server::HandleConnect); + m_NetworkServer.OnClientDisconnect.Connect(this, &Server::HandleDisconnect); + m_NetworkServer.OnClientDisconnectTimeout.Connect(this, &Server::HandleDisconnect); +} + +Server::~Server() {} + +void Server::HandleConnect(network::EnetConnection& a_Connection) { + Session newSession; + newSession.m_Connection = &a_Connection; + + RegisterHandlers(newSession); + + m_Sessions.insert({a_Connection.GetPeerId(), std::move(newSession)}); + + CreateEntity(a_Connection); +} + +void Server::HandleDisconnect(network::EnetConnection& a_Connection) { + m_Sessions.erase(a_Connection.GetPeerId()); +} + +void Server::CreateEntity(network::EnetConnection& a_Connection) { + auto world = m_World.load(); + + auto entity = world->CreateEntity(); + + entity.emplace(a_Connection.GetPeerId(), 69, Nz::GetElapsedMilliseconds(), true); + entity.emplace(&a_Connection); +} + +void Server::RegisterSystems() { + auto world = m_World.load(); + world->AddSystem(); + world->AddSystem(*this); +} + +void Server::RegisterHandlers(Session& session) { + session.m_Handlers.push_back(std::make_unique(*session.m_Connection, m_World)); +} + +network::EnetConnection* Server::GetConnection(std::uint16_t a_PeerId) { + auto it = m_Sessions.find(a_PeerId); + + if (it == m_Sessions.end()) + return nullptr; + + return it->second.m_Connection; +} + +void Server::CloseConnection(std::uint16_t a_PeerId) { + m_NetworkServer.CloseConnection(a_PeerId); +} + +} // namespace server +} // namespace blitz diff --git a/src/server/handlers/KeepAliveHandler.cpp b/src/server/handlers/KeepAliveHandler.cpp new file mode 100644 index 0000000..a979bd8 --- /dev/null +++ b/src/server/handlers/KeepAliveHandler.cpp @@ -0,0 +1,29 @@ +#include + +#include + +#include + +namespace blitz { +namespace server { + +KeepAliveHandler::KeepAliveHandler(network::EnetConnection& a_Connection, EnttWorld& a_World) : + protocol::PacketHandler(a_Connection, a_World) { + m_Slot.Connect(a_Connection.OnKeepAlive, + [this](const protocol::data::KeepAlive& a_KeepAlive) { Handle(m_Connection.GetPeerId(), a_KeepAlive); }); +} + +KeepAliveHandler::~KeepAliveHandler() {} + +void KeepAliveHandler::Handle(std::uint16_t a_PeerId, const protocol::data::KeepAlive& a_KeepAlive) { + auto world = m_World.load(); + world->GetRegistry().view().each([a_PeerId, &a_KeepAlive](auto& keepAliveSession) { + if (keepAliveSession.m_PeerId == a_PeerId && keepAliveSession.m_LastKeepAliveId == a_KeepAlive.m_KeepAliveId) { + keepAliveSession.m_LastTime = Nz::GetElapsedMilliseconds(); + keepAliveSession.m_RecievedResponse = true; + } + }); +} + +} // namespace server +} // namespace blitz diff --git a/src/server/systems/DisconnectSystem.cpp b/src/server/systems/DisconnectSystem.cpp new file mode 100644 index 0000000..60e2c3a --- /dev/null +++ b/src/server/systems/DisconnectSystem.cpp @@ -0,0 +1,25 @@ +#include + +#include +#include + +#include + +namespace blitz { +namespace server { + +DisconectSystem::DisconectSystem(entt::registry& a_Registry, Server& a_Server) : m_Registry(a_Registry), m_Server(a_Server) {} + +void DisconectSystem::Update(Nz::Time elapsedTime) { + m_Registry.view().each( + [this](auto entity, EnetConnectionComponent& connection, DisconnectComponent disconnect) { + m_Server.CloseConnection(connection.m_Connection->GetPeerId()); + }); + + // remove the entities + auto it = m_Registry.view(); + m_Registry.destroy(it.begin(), it.end()); +} + +} // namespace server +} // namespace blitz diff --git a/src/server/systems/KeepAliveSystem.cpp b/src/server/systems/KeepAliveSystem.cpp new file mode 100644 index 0000000..c267947 --- /dev/null +++ b/src/server/systems/KeepAliveSystem.cpp @@ -0,0 +1,38 @@ +#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 diff --git a/xmake.lua b/xmake.lua index 7958a59..8a4f46b 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,7 +1,7 @@ add_rules("mode.debug", "mode.release") add_repositories("nazara-repo https://github.com/NazaraEngine/xmake-repo.git") -add_requires("nazaraengine", { debug = is_mode("debug") }) +add_requires("nazaraengine", { debug = false }) set_languages("c++20") set_warnings("all") @@ -12,7 +12,7 @@ end target("BlitzLib") set_kind("static") - add_files("src/blitz/**.cpp") + add_files("src/blitz/**.cpp", "src/client/**.cpp", "src/server/**.cpp") add_includedirs("include") add_packages("nazaraengine")