fully implement KeepAlive behavior

This commit is contained in:
2024-07-21 20:59:13 +02:00
parent 36a2e67ac4
commit 92a2e53036
25 changed files with 513 additions and 61 deletions

45
src/client/Client.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include <client/Client.h>
#include <client/handlers/KeepAliveHandler.h>
namespace blitz {
namespace client {
Client::Client() : m_World() {
m_World.store(std::make_shared<Nz::EnttWorld>());
}
Client::~Client() {
Disconnect();
}
void Client::BindHandlers() {
m_Handlers.push_back(std::make_unique<KeepAliveHandler>(m_NetworkClient->GetConnection(), m_World));
}
void Client::UnbindHandlers() {
m_Handlers.clear();
}
void Client::Connect(const Nz::IpAddress& a_Ip) {
m_NetworkClient = std::make_unique<network::EnetClient>(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

View File

@@ -0,0 +1,15 @@
#include <client/handlers/KeepAliveHandler.h>
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