fully implement KeepAlive behavior
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
#include <blitz/network/EnetClient.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <blitz/network/EnetConnexion.h>
|
||||
#include <blitz/network/EnetConnection.h>
|
||||
|
||||
#include <Nazara/Network/ENetPeer.hpp>
|
||||
#include <blitz/protocol/PacketSerializer.h>
|
||||
@@ -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()
|
||||
@@ -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<EnetConnexion>(peer);
|
||||
m_Connexion.insert({peer->GetPeerId(), std::move(con)});
|
||||
OnClientConnect(*GetConnexion(peer->GetPeerId()));
|
||||
auto con = std::make_unique<EnetConnection>(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() {
|
||||
|
||||
12
src/blitz/protocol/PacketHandler.cpp
Normal file
12
src/blitz/protocol/PacketHandler.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
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
|
||||
@@ -4,8 +4,6 @@
|
||||
#include <blitz/protocol/PacketFactory.h>
|
||||
#include <blitz/protocol/PacketVisitor.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user