fully implement KeepAlive behavior
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Network/ENetHost.hpp>
|
||||
#include <blitz/network/EnetConnexion.h>
|
||||
#include <blitz/network/EnetConnection.h>
|
||||
#include <thread>
|
||||
|
||||
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;
|
||||
|
||||
@@ -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 <Nazara/Core/ByteArray.hpp>
|
||||
@@ -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:
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <Nazara/Core/ThreadExt.hpp>
|
||||
#include <Nazara/Network/ENetHost.hpp>
|
||||
#include <blitz/network/EnetConnexion.h>
|
||||
#include <blitz/network/EnetConnection.h>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <thread>
|
||||
@@ -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<std::uint16_t, std::unique_ptr<EnetConnexion>> m_Connexion;
|
||||
std::map<std::uint16_t, std::unique_ptr<EnetConnection>> m_Connections;
|
||||
};
|
||||
|
||||
} // namespace network
|
||||
|
||||
23
include/blitz/protocol/PacketHandler.h
Normal file
23
include/blitz/protocol/PacketHandler.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/EnttWorld.hpp>
|
||||
#include <blitz/network/EnetConnection.h>
|
||||
|
||||
namespace blitz {
|
||||
|
||||
using EnttWorld = std::atomic<std::shared_ptr<Nz::EnttWorld>>;
|
||||
|
||||
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
|
||||
31
include/client/Client.h
Normal file
31
include/client/Client.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/EnttWorld.hpp>
|
||||
#include <atomic>
|
||||
#include <blitz/network/EnetClient.h>
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
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<network::EnetClient> m_NetworkClient;
|
||||
std::vector<std::unique_ptr<protocol::PacketHandler>> m_Handlers;
|
||||
|
||||
void BindHandlers();
|
||||
void UnbindHandlers();
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace blitz
|
||||
15
include/client/handlers/KeepAliveHandler.h
Normal file
15
include/client/handlers/KeepAliveHandler.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
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
|
||||
37
include/server/Server.h
Normal file
37
include/server/Server.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <blitz/network/EnetServer.h>
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace server {
|
||||
|
||||
class Server {
|
||||
public:
|
||||
Server(std::uint16_t a_Port, std::shared_ptr<Nz::EnttWorld> 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<std::unique_ptr<protocol::PacketHandler>> m_Handlers;
|
||||
};
|
||||
|
||||
EnttWorld m_World;
|
||||
std::map<std::uint16_t, Session> m_Sessions;
|
||||
network::EnetServer m_NetworkServer;
|
||||
|
||||
void RegisterHandlers(Session& session);
|
||||
void RegisterSystems();
|
||||
void CreateEntity(network::EnetConnection&);
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace blitz
|
||||
13
include/server/components/Disconnect.h
Normal file
13
include/server/components/Disconnect.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
namespace blitz {
|
||||
namespace server {
|
||||
|
||||
enum Reason { Timeout };
|
||||
|
||||
struct DisconnectComponent {
|
||||
Reason m_Reason;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace blitz
|
||||
13
include/server/components/EnetConnection.h
Normal file
13
include/server/components/EnetConnection.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <blitz/network/EnetConnection.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace server {
|
||||
|
||||
struct EnetConnectionComponent {
|
||||
network::EnetConnection* m_Connection;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace blitz
|
||||
17
include/server/components/KeepAliveSession.h
Normal file
17
include/server/components/KeepAliveSession.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
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
|
||||
20
include/server/handlers/KeepAliveHandler.h
Normal file
20
include/server/handlers/KeepAliveHandler.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
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
|
||||
23
include/server/systems/DisconnectSystem.h
Normal file
23
include/server/systems/DisconnectSystem.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
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
|
||||
20
include/server/systems/KeepAliveSystem.h
Normal file
20
include/server/systems/KeepAliveSystem.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Time.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user