true atomic EnttWorld
This commit is contained in:
@@ -14,8 +14,7 @@ namespace server {
|
||||
|
||||
#define RegisterHandler(Handler) session.m_Handlers.push_back(std::make_unique<Handler>(*session.m_Connection, m_World))
|
||||
|
||||
Server::Server(std::uint16_t a_Port, std::shared_ptr<Nz::EnttWorld> a_World) : m_NetworkServer(a_Port) {
|
||||
m_World.store(a_World);
|
||||
Server::Server(std::uint16_t a_Port, Nz::EnttWorld& a_World) : m_World({a_World}), m_NetworkServer(a_Port) {
|
||||
RegisterSystems();
|
||||
m_NetworkServer.OnClientConnect.Connect(this, &Server::HandleConnect);
|
||||
m_NetworkServer.OnClientDisconnect.Connect(this, &Server::HandleDisconnect);
|
||||
@@ -40,7 +39,7 @@ void Server::HandleDisconnect(network::EnetConnection& a_Connection) {
|
||||
}
|
||||
|
||||
void Server::CreateEntity(network::EnetConnection& a_Connection) {
|
||||
auto world = m_World.load();
|
||||
AtomicEnttWorld world = m_World;
|
||||
|
||||
auto entity = world->CreateEntity();
|
||||
|
||||
@@ -48,9 +47,9 @@ void Server::CreateEntity(network::EnetConnection& a_Connection) {
|
||||
}
|
||||
|
||||
void Server::RegisterSystems() {
|
||||
auto world = m_World.load();
|
||||
world->AddSystem<KeepAliveSystem>();
|
||||
world->AddSystem<DisconectSystem>(*this);
|
||||
AtomicEnttWorld world = m_World;
|
||||
world->AddSystem<KeepAliveSystem>(m_World);
|
||||
world->AddSystem<DisconectSystem>(m_World, *this);
|
||||
|
||||
auto counter = world->CreateEntity();
|
||||
counter.emplace<ServerIdCounterComponent>(0);
|
||||
|
||||
@@ -16,7 +16,7 @@ KeepAliveHandler::KeepAliveHandler(network::EnetConnection& a_Connection, EnttWo
|
||||
KeepAliveHandler::~KeepAliveHandler() {}
|
||||
|
||||
void KeepAliveHandler::Handle(std::uint16_t a_PeerId, const protocol::data::KeepAlive& a_KeepAlive) {
|
||||
auto world = m_World.load();
|
||||
AtomicEnttWorld world = m_World;
|
||||
world->GetRegistry().view<KeepAliveSessionComponent>().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();
|
||||
|
||||
@@ -18,7 +18,7 @@ PlayerLoginHandler::PlayerLoginHandler(network::EnetConnection& a_Connection, En
|
||||
PlayerLoginHandler::~PlayerLoginHandler() {}
|
||||
|
||||
void PlayerLoginHandler::Handle(std::uint16_t a_PeerId, const protocol::data::PlayerLogin& a_PlayerLogin) {
|
||||
auto world = m_World.load();
|
||||
AtomicEnttWorld world = m_World;
|
||||
|
||||
std::vector<PlayerInfoComponent> players;
|
||||
|
||||
|
||||
@@ -8,29 +8,33 @@
|
||||
namespace blitz {
|
||||
namespace server {
|
||||
|
||||
DisconectSystem::DisconectSystem(entt::registry& a_Registry, Server& a_Server) : m_Registry(a_Registry), m_Server(a_Server) {}
|
||||
DisconectSystem::DisconectSystem(entt::registry&, EnttWorld& a_World, Server& a_Server) : m_World(a_World), m_Server(a_Server) {}
|
||||
|
||||
void DisconectSystem::Update(Nz::Time elapsedTime) {
|
||||
auto disconnects = m_Registry.view<DisconnectComponent>();
|
||||
AtomicEnttWorld world = m_World;
|
||||
|
||||
entt::registry& registry = world->GetRegistry();
|
||||
|
||||
auto disconnects = registry.view<DisconnectComponent>();
|
||||
|
||||
// broadcast player leave
|
||||
for (auto entity : disconnects) {
|
||||
auto* player = m_Registry.try_get<PlayerInfoComponent>(entity);
|
||||
auto* player = registry.try_get<PlayerInfoComponent>(entity);
|
||||
if (player) {
|
||||
for (auto [entity, connection] : m_Registry.view<EnetConnectionComponent>().each()) {
|
||||
for (auto [entity, connection] : registry.view<EnetConnectionComponent>().each()) {
|
||||
connection.m_Connection->SendPlayerLeave({player->m_PlayerId});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// close connections
|
||||
m_Registry.view<EnetConnectionComponent, DisconnectComponent>().each(
|
||||
registry.view<EnetConnectionComponent, DisconnectComponent>().each(
|
||||
[this](auto entity, EnetConnectionComponent& connection, DisconnectComponent disconnect) {
|
||||
m_Server.CloseConnection(connection.m_Connection->GetPeerId());
|
||||
});
|
||||
|
||||
// remove the entities
|
||||
m_Registry.destroy(disconnects.begin(), disconnects.end());
|
||||
registry.destroy(disconnects.begin(), disconnects.end());
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
|
||||
@@ -9,11 +9,15 @@
|
||||
namespace blitz {
|
||||
namespace server {
|
||||
|
||||
KeepAliveSystem::KeepAliveSystem(entt::registry& a_Registry) : m_Registry(a_Registry) {}
|
||||
KeepAliveSystem::KeepAliveSystem(entt::registry&, EnttWorld& a_World) : m_World(a_World) {}
|
||||
|
||||
void KeepAliveSystem::Update(Nz::Time elapsedTime) {
|
||||
m_Registry.view<KeepAliveSessionComponent, EnetConnectionComponent>().each(
|
||||
[this](auto entity, auto& keepAliveSession, auto& connection) {
|
||||
AtomicEnttWorld world = m_World;
|
||||
|
||||
entt::registry& registry = world->GetRegistry();
|
||||
|
||||
registry.view<KeepAliveSessionComponent, EnetConnectionComponent>().each(
|
||||
[®istry](auto entity, auto& keepAliveSession, auto& connection) {
|
||||
auto duration = Nz::GetElapsedMilliseconds() - keepAliveSession.m_LastTime;
|
||||
if (duration > Nz::Time::Seconds(10)) {
|
||||
if (keepAliveSession.m_RecievedResponse) {
|
||||
@@ -28,7 +32,7 @@ void KeepAliveSystem::Update(Nz::Time elapsedTime) {
|
||||
connection.m_Connection->SendKeepAlive({keepAliveId});
|
||||
} else {
|
||||
// We kick the player because he's not responding anymore
|
||||
m_Registry.emplace<DisconnectComponent>(entity);
|
||||
registry.emplace<DisconnectComponent>(entity);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user