Commit 49d5a2ce authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

move serialization to namespace, use in rpc API

parent 1d16d8f0
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
#include <com/library.hpp>
#include <net/index.hpp>
#include <net/types.hpp>
#include <utils/serialization.hpp>

namespace net
{
@@ -20,7 +21,7 @@ namespace net
         * @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.
         */
        void process_packet(ConnectionId from, void* deserializer);
        void process_packet(ConnectionId from, utils::Deserializer& deserializer);

        void call_remote(
            com::ContextItem* context_item,
@@ -59,7 +60,7 @@ namespace net
        void call_function_local(
            std::vector<std::string> const& context_item_path,
            std::string const& function_name,
            void* deserializer,
            utils::Deserializer& deserializer,
            ConnectionId from_id);

        template<typename T>
+2 −2
Original line number Diff line number Diff line
@@ -135,7 +135,7 @@ void Driver::next_round()

void Driver::handle_packet(ConnectionId from, NetworkMessage* message)
{
    auto deserializer = create_deserializer(message->data);
    auto deserializer = utils::create_deserializer(message->data);

    PacketType packet_type;
    deserializer.value1b(packet_type);
@@ -143,7 +143,7 @@ void Driver::handle_packet(ConnectionId from, NetworkMessage* message)
    switch (packet_type)
    {
        case PacketType::RPC:
            rpc_system()->process_packet(from, &deserializer);
            rpc_system()->process_packet(from, deserializer);
            break;

        default:
+13 −13
Original line number Diff line number Diff line
@@ -49,12 +49,10 @@ void RPCSystem::call_remote(
    driver()->send(target, buffer_span, reliability);
}

void RPCSystem::process_packet(ConnectionId from, void* deserializer)
void RPCSystem::process_packet(ConnectionId from, utils::Deserializer& deserializer)
{
    auto des = static_cast<Deserializer*>(deserializer);

    std::vector<std::string> context_item_path;
    des->container(context_item_path, MAX_PATH_DEPTH, [](auto& s, std::string& str)
    deserializer.container(context_item_path, MAX_PATH_DEPTH, [](auto& s, std::string& str)
    {
        s.text1b(str, MAX_STRING_LENGTH);
    });
@@ -65,14 +63,14 @@ void RPCSystem::process_packet(ConnectionId from, void* deserializer)
    }

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

    call_function_local(context_item_path, function_name, des, from);
    call_function_local(context_item_path, function_name, deserializer, from);
}

std::vector<uint8_t> RPCSystem::create_packet(
@@ -81,8 +79,8 @@ std::vector<uint8_t> RPCSystem::create_packet(
    const std::vector<std::shared_ptr<com::Reflection::Value>>& params,
    int& written_bytes)
{
    Buffer buffer(RPC_PACKET_INITIAL_BUFFER_SIZE);
    auto serializer = create_serializer(buffer);
    utils::Buffer buffer(RPC_PACKET_INITIAL_BUFFER_SIZE);
    auto serializer = utils::create_serializer(buffer);
    serializer.value1b(static_cast<uint8_t>(PacketType::RPC));

    const std::vector<std::string> context_item_path = context_item->path(com::Folder::root());
@@ -112,7 +110,7 @@ std::vector<uint8_t> RPCSystem::create_packet(
            LOG(LSL_ERROR, "[RPCSystem] Parameter type mismatch for parameter " << i << " of function " << function_name << " in context item " << context_item->name());
            return {};
        }
        serialize_reflection_value(serializer, params[i]);
        utils::serialize_reflection_value(serializer, params[i]);
    }

    written_bytes = static_cast<int>(serializer.adapter().writtenBytesCount());
@@ -120,10 +118,12 @@ std::vector<uint8_t> RPCSystem::create_packet(
    return buffer;
}

void RPCSystem::call_function_local(std::vector<std::string> const& context_item_path, std::string const& function_name, void* deserializer, ConnectionId from_id)
void RPCSystem::call_function_local(
    std::vector<std::string> const& context_item_path,
    std::string const& function_name,
    utils::Deserializer& deserializer,
    ConnectionId from_id)
{
    auto des = static_cast<Deserializer*>(deserializer);

    const auto context_item = com::Folder::root()->locate<com::ContextItem>(context_item_path);
    if (!context_item)
    {
@@ -144,7 +144,7 @@ void RPCSystem::call_function_local(std::vector<std::string> const& context_item

    for (int i = 0; i < function.param_types.size(); i++)
    {
        params.push_back(deserialize_reflection_value(*des));
        params.push_back(utils::deserialize_reflection_value(deserializer));
    }

    if (function.param_types.size() != params.size() - 1)
+65 −63
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ constexpr size_t MAX_STRING_LENGTH = 250;
constexpr size_t MAX_PATH_DEPTH = 16;
constexpr size_t RPC_PACKET_INITIAL_BUFFER_SIZE = 1024;

namespace utils {
// Buffer type for serialization
using Buffer = std::vector<uint8_t>;

@@ -181,6 +182,7 @@ inline com::Reflection::ValuePtr deserialize_reflection_value(Deserializer& s)
        throw std::runtime_error("Unsupported type for deserialization");
    }
}
}

// Register serialization for custom types
namespace bitsery