Loading net/include/net/replication_system.hpp +10 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,15 @@ namespace net { const std::vector<std::string>& owner_path, const std::string& getter_name, const std::string& setter_name); void unregister_property( com::ContextItem* owner, const std::string& getter_name, const std::string& setter_name); void unregister_property( const std::vector<std::string>& owner_path, const std::string& getter_name, const std::string& setter_name); void handle_packet(ConnectionId from, utils::Deserializer& deserializer); private: Loading @@ -33,7 +42,7 @@ namespace net { void register_functions() override; const size_t INITIAL_BUFFER_SIZE = 512; std::vector<ReplicatedProperty> replicated_properties; std::unordered_map<com::ContextItem*, std::vector<ReplicatedProperty>> replicated_properties; double replication_interval = 0.3; double time_since_last_replication = 0.0; Loading net/src/replication_system.cpp +67 −13 Original line number Diff line number Diff line #include <ranges> #include <net/index.hpp> #include <net/replication_system.hpp> #include <phx/timer.hpp> Loading @@ -9,7 +10,9 @@ namespace net { time_since_last_replication += osi::timer()->dt(); if (time_since_last_replication > replication_interval) { for (const auto& prop : replicated_properties) for (auto& properties : replicated_properties | std::views::values) { for (auto& prop : properties) { // TODO: Delta compression here com::Reflection::ValuePtr current_value = com::make_default_value(prop.last_value->typeID()); Loading @@ -22,6 +25,7 @@ namespace net { std::span<const uint8_t> buffer_span(buffer.data(), written_bytes); driver()->broadcast(buffer_span, ReliabilityMode::UNRELIABLE); } } time_since_last_replication -= replication_interval; } } Loading Loading @@ -66,7 +70,7 @@ namespace net { params.push_back(property.last_value); property.getter_func(params); replicated_properties.push_back(property); replicated_properties[owner].push_back(property); } void ReplicationSystem::register_property(const std::vector<std::string>& owner_path, Loading @@ -83,6 +87,44 @@ namespace net { register_property(owner, getter_name, setter_name); } void ReplicationSystem::unregister_property(com::ContextItem* owner, const std::string& getter_name, const std::string& setter_name) { if (!owner) { LOG(LSL_ERROR, "[ReplicationSystem] Invalid owner for replicated property"); return; } if (!replicated_properties.contains(owner)) { LOG(LSL_ERROR, "[ReplicationSystem] No replicated properties found for owner " << owner->name()); return; } auto& properties = replicated_properties[owner]; std::erase_if(properties, [&](const ReplicatedProperty& prop) { return prop.getter_name == getter_name && prop.setter_name == setter_name; }); if (properties.empty()) { replicated_properties.erase(owner); } } void ReplicationSystem::unregister_property(const std::vector<std::string>& owner_path, const std::string& getter_name, const std::string& setter_name) { auto owner = com::Folder::root()->locate<com::ContextItem>(owner_path); if (!owner) { LOG(LSL_ERROR, "[ReplicationSystem] Owner not found at path " << owner_path); return; } unregister_property(owner, getter_name, setter_name); } void ReplicationSystem::handle_packet(ConnectionId from, utils::Deserializer& deserializer) { Loading Loading @@ -172,12 +214,24 @@ namespace net { false, [this](std::vector<ValuePtr>& params) -> void { auto owner_path = com::as<ValuePATH>(params[0])->value; const auto owner_path = com::as<ValuePATH>(params[0])->value; const std::string& getter_name = com::as<ValueSTRING>(params[1])->value; const std::string& setter_name = com::as<ValueSTRING>(params[2])->value; register_property(owner_path, getter_name, setter_name); } }); com::Reflection::register_function(this, { "unregister_property", {TypeID::PATH, TypeID::STRING, TypeID::STRING}, false, [this](std::vector<ValuePtr>& params) -> void { const auto owner_path = com::as<ValuePATH>(params[0])->value; const std::string& getter_name = com::as<ValueSTRING>(params[1])->value; const std::string& setter_name = com::as<ValueSTRING>(params[2])->value; unregister_property(owner_path, getter_name, setter_name); } }); } Loading Loading
net/include/net/replication_system.hpp +10 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,15 @@ namespace net { const std::vector<std::string>& owner_path, const std::string& getter_name, const std::string& setter_name); void unregister_property( com::ContextItem* owner, const std::string& getter_name, const std::string& setter_name); void unregister_property( const std::vector<std::string>& owner_path, const std::string& getter_name, const std::string& setter_name); void handle_packet(ConnectionId from, utils::Deserializer& deserializer); private: Loading @@ -33,7 +42,7 @@ namespace net { void register_functions() override; const size_t INITIAL_BUFFER_SIZE = 512; std::vector<ReplicatedProperty> replicated_properties; std::unordered_map<com::ContextItem*, std::vector<ReplicatedProperty>> replicated_properties; double replication_interval = 0.3; double time_since_last_replication = 0.0; Loading
net/src/replication_system.cpp +67 −13 Original line number Diff line number Diff line #include <ranges> #include <net/index.hpp> #include <net/replication_system.hpp> #include <phx/timer.hpp> Loading @@ -9,7 +10,9 @@ namespace net { time_since_last_replication += osi::timer()->dt(); if (time_since_last_replication > replication_interval) { for (const auto& prop : replicated_properties) for (auto& properties : replicated_properties | std::views::values) { for (auto& prop : properties) { // TODO: Delta compression here com::Reflection::ValuePtr current_value = com::make_default_value(prop.last_value->typeID()); Loading @@ -22,6 +25,7 @@ namespace net { std::span<const uint8_t> buffer_span(buffer.data(), written_bytes); driver()->broadcast(buffer_span, ReliabilityMode::UNRELIABLE); } } time_since_last_replication -= replication_interval; } } Loading Loading @@ -66,7 +70,7 @@ namespace net { params.push_back(property.last_value); property.getter_func(params); replicated_properties.push_back(property); replicated_properties[owner].push_back(property); } void ReplicationSystem::register_property(const std::vector<std::string>& owner_path, Loading @@ -83,6 +87,44 @@ namespace net { register_property(owner, getter_name, setter_name); } void ReplicationSystem::unregister_property(com::ContextItem* owner, const std::string& getter_name, const std::string& setter_name) { if (!owner) { LOG(LSL_ERROR, "[ReplicationSystem] Invalid owner for replicated property"); return; } if (!replicated_properties.contains(owner)) { LOG(LSL_ERROR, "[ReplicationSystem] No replicated properties found for owner " << owner->name()); return; } auto& properties = replicated_properties[owner]; std::erase_if(properties, [&](const ReplicatedProperty& prop) { return prop.getter_name == getter_name && prop.setter_name == setter_name; }); if (properties.empty()) { replicated_properties.erase(owner); } } void ReplicationSystem::unregister_property(const std::vector<std::string>& owner_path, const std::string& getter_name, const std::string& setter_name) { auto owner = com::Folder::root()->locate<com::ContextItem>(owner_path); if (!owner) { LOG(LSL_ERROR, "[ReplicationSystem] Owner not found at path " << owner_path); return; } unregister_property(owner, getter_name, setter_name); } void ReplicationSystem::handle_packet(ConnectionId from, utils::Deserializer& deserializer) { Loading Loading @@ -172,12 +214,24 @@ namespace net { false, [this](std::vector<ValuePtr>& params) -> void { auto owner_path = com::as<ValuePATH>(params[0])->value; const auto owner_path = com::as<ValuePATH>(params[0])->value; const std::string& getter_name = com::as<ValueSTRING>(params[1])->value; const std::string& setter_name = com::as<ValueSTRING>(params[2])->value; register_property(owner_path, getter_name, setter_name); } }); com::Reflection::register_function(this, { "unregister_property", {TypeID::PATH, TypeID::STRING, TypeID::STRING}, false, [this](std::vector<ValuePtr>& params) -> void { const auto owner_path = com::as<ValuePATH>(params[0])->value; const std::string& getter_name = com::as<ValueSTRING>(params[1])->value; const std::string& setter_name = com::as<ValueSTRING>(params[2])->value; unregister_property(owner_path, getter_name, setter_name); } }); } Loading