Commit 4d209245 authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

NetDriver & RPC cleanup

parent 7754eef3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ namespace net
        void register_on_connected(const std::function<void()>& callback);
        void register_on_disconnected(const std::function<void()>& callback);
        void register_on_peer_connected(const std::function<void(ConnectionId)>& callback);
        void register_on_peer_disconnected(std::function<void(ConnectionId)> callback);
        void register_on_peer_disconnected(const std::function<void(ConnectionId)>& callback);

        SocketMode mode() const { return socket->get_mode(); }
        ConnectionStatus status() const { return socket->get_status(); }
+2 −1
Original line number Diff line number Diff line
@@ -40,7 +40,8 @@ namespace net
        NOT_CONNECTED,
        MESSAGE_TOO_LARGE,
        BUFFER_FULL,
        PEER_NOT_FOUND
        PEER_NOT_FOUND,
        BROADCAST_INCOMPLETE
    };

    enum class ReliabilityMode : uint8_t
+11 −2
Original line number Diff line number Diff line
@@ -17,8 +17,16 @@ namespace net
{
    struct RPCSystem
    {
        /**
         * @brief Processes an incoming RPC packet, deserializes it, and calls the appropriate local function.
         * Should not be called directly, only by the NetDriver when receiving an RPC packet.
         */
        static void process_packet(ConnectionId from, void* deserializer);

        /**
         * @brief Calls a remote function on a target connection.
         * Prefer using the RPC_RELIABLE and RPC_UNRELIABLE macros instead of calling this directly.
         */
        template<typename... Args>
        static void call_remote(
            com::ContextItem* context_item,
@@ -29,10 +37,11 @@ namespace net
        {

            std::vector<com::Reflection::ValuePtr> params;
            params.reserve(sizeof...(args));
            (params.push_back(make_reflection_value(std::forward<Args>(args))), ...); // Convert each argument to a ValuePtr and add to params vector

            int written_bytes = 0;
            auto buffer = create_packet(context_item, method_name, params, &written_bytes);
            auto buffer = create_packet(context_item, method_name, params,written_bytes);
            std::span<const uint8_t> buffer_span(buffer.data(), written_bytes);
            driver()->send(target, buffer_span, reliability);
        }
@@ -42,7 +51,7 @@ namespace net
            com::ContextItem* context_item,
            std::string const& function_name,
            const std::vector<std::shared_ptr<com::Reflection::Value>>& params,
            int* written_bytes = nullptr);
            int& written_bytes);

        static void call_function_local(
            std::vector<std::string> const& context_item_path,
+6 −3
Original line number Diff line number Diff line
@@ -76,16 +76,19 @@ SendResult NetDriver::broadcast(
    OrderingMode ordering,
    int channel)
{
    bool all_success = true;
    for (const auto& connection : connections | views::values)
    {
        if (!connection.active) continue;
        auto result = socket->send(connection.id, data, reliability, ordering, channel);
        if (result != SendResult::SUCCESS)
        {
            return result; // Return the first failure result encountered
            LOG(LSL_WARNING, "[NetDriver] Broadcast failed to send message to connection " << connection.id);
            all_success = false;
        }
    }
    return SendResult::SUCCESS;

    return all_success ? SendResult::SUCCESS : SendResult::BROADCAST_INCOMPLETE;
}

void NetDriver::register_on_connected(const std::function<void()>& callback)
@@ -100,7 +103,7 @@ void NetDriver::register_on_peer_connected(const std::function<void(ConnectionId
{
    socket->register_on_peer_connected(callback);
}
void NetDriver::register_on_peer_disconnected(std::function<void(ConnectionId)> callback)
void NetDriver::register_on_peer_disconnected(const std::function<void(ConnectionId)>& callback)
{
    socket->register_on_peer_disconnected(callback);
}
+13 −13
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@ namespace net
        auto des = static_cast<Deserializer*>(deserializer);

        std::vector<std::string> context_item_path;
        des->container(context_item_path, 32, [](auto& s, std::string& str) {
            s.text1b(str, 250);
        des->container(context_item_path, MAX_PATH_DEPTH, [](auto& s, std::string& str) {
            s.text1b(str, MAX_STRING_LENGTH);
        });
        if (context_item_path.empty())
        {
@@ -18,29 +18,32 @@ namespace net
        }

        std::string function_name;
        des->text1b(function_name, 250);
        des->text1b(function_name, MAX_STRING_LENGTH);
        if (function_name.empty())
        {
            LOG(LSL_ERROR, "[RPCSystem] Received RPC packet with empty function name");
            return;
        }

        std::vector<uint8_t> param_data;
        call_function_local(context_item_path, function_name, des, from);
    }

    std::vector<uint8_t> RPCSystem::create_packet(com::ContextItem* context_item, std::string const& function_name, const std::vector<std::shared_ptr<com::Reflection::Value>>& params, int* written_bytes)
    std::vector<uint8_t> RPCSystem::create_packet(
        com::ContextItem* context_item,
        std::string const& function_name,
        const std::vector<std::shared_ptr<com::Reflection::Value>>& params,
        int& written_bytes)
    {
        Buffer buffer(1024);
        Buffer buffer(RPC_PACKET_INITIAL_BUFFER_SIZE);
        auto serializer = create_serializer(buffer);
        serializer.value1b(static_cast<uint8_t>(PacketType::RPC));

        const std::vector<std::string> context_item_path = context_item->path(root());
        serializer.container(context_item_path, 32, [](auto& s, std::string& str) {
            s.text1b(str, 250);
        serializer.container(context_item_path, MAX_PATH_DEPTH, [](auto& s, std::string& str) {
            s.text1b(str, MAX_STRING_LENGTH);
        });

        serializer.text1b(function_name, 250);
        serializer.text1b(function_name, MAX_STRING_LENGTH);

        if (!context_item->reflection()->functions().contains(function_name))
        {
@@ -65,10 +68,7 @@ namespace net
            serialize_reflection_value(serializer, params[i]);
        }

        if (written_bytes)
        {
            *written_bytes = static_cast<int>(serializer.adapter().writtenBytesCount());
        }
        written_bytes = static_cast<int>(serializer.adapter().writtenBytesCount());

        return buffer;
    }
Loading