Loading net/include/net/file.hpp +5 −2 Original line number Diff line number Diff line Loading @@ -5,7 +5,8 @@ #define NETWORK_ITEM(T)\ public:\ static constexpr uint32_t type_id = net::fnv1a_32(#T) static constexpr uint32_t m_type_id = net::fnv1a_32(#T);\ uint32_t type_id() const override { return m_type_id; } namespace net { struct File : public com::File Loading @@ -13,8 +14,10 @@ namespace net { File() = delete; explicit File(const std::string& name); NETWORK_ITEM(ContextItem); virtual uint32_t type_id() const = 0; NetworkIdentity* identity() { return &m_identity; } void set_identity(const NetworkIdentity& identity) { m_identity = identity; } private: Loading net/include/net/object_creation_registry.hpp +2 −2 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ struct ObjectCreationRegistry final : public com::Library static_assert(std::is_base_of_v<net::File, T>, "Registered type must be derived from net::File"); auto [it, inserted] = factories.emplace (T::type_id, [](com::Folder* parent, const std::string& name) (T::m_type_id, [](com::Folder* parent, const std::string& name) { return parent->push_back<T>(name); }); Loading net/include/net/object_system.hpp +16 −4 Original line number Diff line number Diff line Loading @@ -21,32 +21,44 @@ namespace net { * Only networked objects can have replicated properties. */ template<typename T> T* create_networked(const std::string& name, ConnectionId owner); T* create_networked(const std::string& name, ConnectionId owner = 0); /** * @brief SERVER ONLY: Destroys a networked object by its NetworkId. */ void destroy_networked(NetworkId owner); void destroy_networked(net::File* object); /** * @brief SERVER ONLY: Synchronizes the entire networked object hierarchy to a newly connected client. */ void synchronize_object_hierarchy(ConnectionId target); void handle_create(utils::Deserializer& deserializer); void handle_destroy(utils::Deserializer& deserializer); // In object_system.hpp private: void broadcast_create(const std::string& name, uint32_t type_id, const NetworkIdentity* identity); void build_create_packet(utils::Serializer& s, const std::string& name, uint32_t type_id, const NetworkIdentity* identity); com::Folder* m_objects; }; template<typename T> T* ObjectSystem::create_networked(const std::string& name, ConnectionId owner) { if (driver()->mode() != SocketMode::SERVER) { if (driver()->mode() != SocketMode::SERVER) { LOG(LSL_ERROR, "[ObjectSystem] create_networked can only be called on the server."); throw std::runtime_error("create_networked can only be called on the server."); } static_assert(std::is_base_of_v<net::File, T>, "Type must be derived from net::File"); T* obj = static_cast<T*>(object_creation_registry()->create(T::type_id, objects(), name)); T* obj = static_cast<T*>(object_creation_registry()->create(T::m_type_id, objects(), name)); obj->identity()->owner = owner; broadcast_create(name, T::m_type_id, obj->identity()); return obj; } } net/include/net/types.hpp +3 −1 Original line number Diff line number Diff line Loading @@ -34,7 +34,9 @@ namespace net enum class PacketType : uint8_t { RPC RPC, OBJECT_CREATE, OBJECT_DESTROY }; enum class SocketMode : uint8_t Loading net/src/driver.cpp +6 −0 Original line number Diff line number Diff line #include <net/driver.hpp> #include <net/socket/gns_socket.hpp> #include <ranges> #include <net/object_system.hpp> #include <net/rpc_system.hpp> #include <osi/index.hpp> #include <utils/serialization.hpp> Loading Loading @@ -36,6 +37,7 @@ bool Driver::start_server(int port, int max_connections) { LOG(LSL_DEBUG, "[NetDriver] New connection added with ID " << id); add_connection(id); object_system()->synchronize_object_hierarchy(id); }); socket->register_on_peer_disconnected([this](ConnectionId id) { Loading Loading @@ -146,6 +148,10 @@ void Driver::handle_packet(ConnectionId from, NetworkMessage* message) rpc_system()->process_packet(from, deserializer); break; case PacketType::OBJECT_CREATE: object_system()->handle_create(deserializer); break; default: LOG(LSL_WARNING, "[NetDriver] Received packet with unknown type from connection " << from); break; Loading Loading
net/include/net/file.hpp +5 −2 Original line number Diff line number Diff line Loading @@ -5,7 +5,8 @@ #define NETWORK_ITEM(T)\ public:\ static constexpr uint32_t type_id = net::fnv1a_32(#T) static constexpr uint32_t m_type_id = net::fnv1a_32(#T);\ uint32_t type_id() const override { return m_type_id; } namespace net { struct File : public com::File Loading @@ -13,8 +14,10 @@ namespace net { File() = delete; explicit File(const std::string& name); NETWORK_ITEM(ContextItem); virtual uint32_t type_id() const = 0; NetworkIdentity* identity() { return &m_identity; } void set_identity(const NetworkIdentity& identity) { m_identity = identity; } private: Loading
net/include/net/object_creation_registry.hpp +2 −2 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ struct ObjectCreationRegistry final : public com::Library static_assert(std::is_base_of_v<net::File, T>, "Registered type must be derived from net::File"); auto [it, inserted] = factories.emplace (T::type_id, [](com::Folder* parent, const std::string& name) (T::m_type_id, [](com::Folder* parent, const std::string& name) { return parent->push_back<T>(name); }); Loading
net/include/net/object_system.hpp +16 −4 Original line number Diff line number Diff line Loading @@ -21,32 +21,44 @@ namespace net { * Only networked objects can have replicated properties. */ template<typename T> T* create_networked(const std::string& name, ConnectionId owner); T* create_networked(const std::string& name, ConnectionId owner = 0); /** * @brief SERVER ONLY: Destroys a networked object by its NetworkId. */ void destroy_networked(NetworkId owner); void destroy_networked(net::File* object); /** * @brief SERVER ONLY: Synchronizes the entire networked object hierarchy to a newly connected client. */ void synchronize_object_hierarchy(ConnectionId target); void handle_create(utils::Deserializer& deserializer); void handle_destroy(utils::Deserializer& deserializer); // In object_system.hpp private: void broadcast_create(const std::string& name, uint32_t type_id, const NetworkIdentity* identity); void build_create_packet(utils::Serializer& s, const std::string& name, uint32_t type_id, const NetworkIdentity* identity); com::Folder* m_objects; }; template<typename T> T* ObjectSystem::create_networked(const std::string& name, ConnectionId owner) { if (driver()->mode() != SocketMode::SERVER) { if (driver()->mode() != SocketMode::SERVER) { LOG(LSL_ERROR, "[ObjectSystem] create_networked can only be called on the server."); throw std::runtime_error("create_networked can only be called on the server."); } static_assert(std::is_base_of_v<net::File, T>, "Type must be derived from net::File"); T* obj = static_cast<T*>(object_creation_registry()->create(T::type_id, objects(), name)); T* obj = static_cast<T*>(object_creation_registry()->create(T::m_type_id, objects(), name)); obj->identity()->owner = owner; broadcast_create(name, T::m_type_id, obj->identity()); return obj; } }
net/include/net/types.hpp +3 −1 Original line number Diff line number Diff line Loading @@ -34,7 +34,9 @@ namespace net enum class PacketType : uint8_t { RPC RPC, OBJECT_CREATE, OBJECT_DESTROY }; enum class SocketMode : uint8_t Loading
net/src/driver.cpp +6 −0 Original line number Diff line number Diff line #include <net/driver.hpp> #include <net/socket/gns_socket.hpp> #include <ranges> #include <net/object_system.hpp> #include <net/rpc_system.hpp> #include <osi/index.hpp> #include <utils/serialization.hpp> Loading Loading @@ -36,6 +37,7 @@ bool Driver::start_server(int port, int max_connections) { LOG(LSL_DEBUG, "[NetDriver] New connection added with ID " << id); add_connection(id); object_system()->synchronize_object_hierarchy(id); }); socket->register_on_peer_disconnected([this](ConnectionId id) { Loading Loading @@ -146,6 +148,10 @@ void Driver::handle_packet(ConnectionId from, NetworkMessage* message) rpc_system()->process_packet(from, deserializer); break; case PacketType::OBJECT_CREATE: object_system()->handle_create(deserializer); break; default: LOG(LSL_WARNING, "[NetDriver] Received packet with unknown type from connection " << from); break; Loading