true atomic EnttWorld

This commit is contained in:
2024-07-23 01:10:20 +02:00
parent 392eaeab83
commit 89d5ad5f54
16 changed files with 61 additions and 35 deletions

View File

@@ -1,9 +1,31 @@
#pragma once
#include <cstdint>
#include <Nazara/Core/EnttWorld.hpp>
namespace blitz {
struct AtomicEnttWorld {
Nz::EnttWorld& m_World;
std::lock_guard<std::mutex> m_LockGuard;
Nz::EnttWorld* operator->() {
return &m_World;
}
};
struct EnttWorld {
Nz::EnttWorld& m_World;
std::mutex m_Mutex;
/**
* \return an AtomicEnttWorld structure which will lock the associated mutex until destruction
* \warning Do not hold more than one instance or the current thread will self lock
*/
operator AtomicEnttWorld() {
return {m_World, std::lock_guard<std::mutex>(m_Mutex)};
}
};
using EntityID = std::uint32_t;
} // namespace blitz

View File

@@ -4,9 +4,6 @@
#include <blitz/network/EnetConnection.h>
namespace blitz {
using EnttWorld = std::atomic<std::shared_ptr<Nz::EnttWorld>>;
namespace protocol {
class PacketHandler : private NonCopyable {

View File

@@ -10,7 +10,7 @@ namespace client {
class Client : private NonCopyable {
public:
Client(std::shared_ptr<Nz::EnttWorld> a_World);
Client(Nz::EnttWorld& a_World);
~Client();
void Connect(const Nz::IpAddress& a_Ip);

View File

@@ -12,7 +12,7 @@ class Server {
* \brief Construct a server
* \pre Two instances of Server should not share the same world
*/
Server(std::uint16_t a_Port, std::shared_ptr<Nz::EnttWorld> a_World);
Server(std::uint16_t a_Port, Nz::EnttWorld& a_World);
~Server();
network::EnetConnection* GetConnection(std::uint16_t a_PeerId);

View File

@@ -2,6 +2,7 @@
#include <Nazara/Core/Time.hpp>
#include <entt/entity/registry.hpp>
#include <blitz/common/Types.h>
namespace blitz {
namespace server {
@@ -10,12 +11,12 @@ class Server;
class DisconectSystem {
public:
DisconectSystem(entt::registry& a_Registry, Server& a_Server);
DisconectSystem(entt::registry&, EnttWorld& a_World, Server& a_Server);
void Update(Nz::Time elapsedTime);
private:
entt::registry& m_Registry;
EnttWorld& m_World;
Server& m_Server;
};

View File

@@ -2,18 +2,19 @@
#include <Nazara/Core/Time.hpp>
#include <entt/entity/registry.hpp>
#include <blitz/common/Types.h>
namespace blitz {
namespace server {
class KeepAliveSystem {
public:
KeepAliveSystem(entt::registry& a_Registry);
KeepAliveSystem(entt::registry&, EnttWorld& a_World);
void Update(Nz::Time elapsedTime);
private:
entt::registry& m_Registry;
EnttWorld& m_World;
};
} // namespace server