Commit 2a707c81 authored by Filip Hauzvic's avatar Filip Hauzvic
Browse files

Rewrite RPC system into com::Library to register to reflection

parent 89f6b70e
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -4,12 +4,14 @@

namespace net
{
    struct RPCSystem;
    struct Index final
    {
        static Index const &instance();

        com::Folder* root() const { return m_root; }
        Driver* driver() const { return m_net_driver; }
        Driver* driver() const { return m_driver; }
        RPCSystem* rpc_system() const { return m_rpc_system; }

      private:
        Index();
@@ -19,12 +21,14 @@ namespace net
        Index &operator=(Index &&) const = delete;

        com::Folder* m_root;
        Driver* m_net_driver;
        Driver* m_driver;
        RPCSystem* m_rpc_system;
    };

    inline Index const &index() { return Index::instance(); }

    inline com::Folder* root() { return index().root(); }
    inline Driver* driver() { return index().driver(); }
    inline RPCSystem* rpc_system() { return index().rpc_system(); }

}
+25 −10
Original line number Diff line number Diff line
@@ -2,24 +2,27 @@
#include <string>
#include <vector>
#include <com/context.hpp>
#include <com/library.hpp>
#include <net/index.hpp>
#include <net/types.hpp>

namespace net
{
    struct RPCSystem
    struct RPCSystem : com::Library
    {
        static inline std::string self_name() { return "rpc_system.lib"; }

        RPCSystem();
        ~RPCSystem() override;

        /**
         * @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);
        void process_packet(ConnectionId from, void* deserializer);

        /**
         * @brief Calls a remote function on a target connection.
         */
        template<typename... Args>
        static void call_remote(
        void call_remote(
            com::ContextItem* context_item,
            const std::string& method_name,
            ConnectionId target,
@@ -31,7 +34,17 @@ namespace net
            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

            // TODO: Extract into method, register into reflection
            call_remote(context_item, method_name, target, reliability,
                static_cast<const std::vector<com::Reflection::ValuePtr>&>(params));
        }

        void call_remote(
            com::ContextItem* context_item,
            const std::string& method_name,
            ConnectionId target,
            ReliabilityMode reliability,
            const std::vector<com::Reflection::ValuePtr>& params)
        {
            int written_bytes = 0;
            auto buffer = create_packet(context_item, method_name, params, written_bytes);
            std::span<const uint8_t> buffer_span(buffer.data(), written_bytes);
@@ -39,13 +52,15 @@ namespace net
        }

    private:
        static std::vector<uint8_t> create_packet(
        void register_functions() override;

        std::vector<uint8_t> create_packet(
            com::ContextItem* context_item,
            std::string const& function_name,
            const std::vector<std::shared_ptr<com::Reflection::Value>>& params,
            int& written_bytes);

        static void call_function_local(
        void call_function_local(
            std::vector<std::string> const& context_item_path,
            std::string const& function_name,
            void* deserializer,
+1 −1
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ void Driver::handle_packet(ConnectionId from, NetworkMessage* message)
    switch (packet_type)
    {
        case PacketType::RPC:
            RPCSystem::process_packet(from, &deserializer);
            rpc_system()->process_packet(from, &deserializer);
            break;

        default:
+5 −2
Original line number Diff line number Diff line
#include <net/index.hpp>
#include <net/rpc_system.hpp>

namespace net {

@@ -9,10 +10,12 @@ namespace net {

    Index::Index()
        : m_root { com::Folder::root()->find<com::Folder>("net") }
        , m_net_driver { m_root->find<Driver>(Driver::self_name()) }
        , m_driver { m_root->find<Driver>(Driver::self_name()) }
        , m_rpc_system { m_root->find<RPCSystem>(RPCSystem::self_name()) }
    {
        ASSUMPTION(
            m_net_driver != nullptr
            m_driver != nullptr &&
            m_rpc_system != nullptr
            );
    }
}
 No newline at end of file
+3 −1
Original line number Diff line number Diff line
#include <net/module.hpp>
#include <com/context.hpp>
#include <net/driver.hpp>
#include <net/rpc_system.hpp>

void net::boot(com::Folder *ctx_root)
{
	auto* const root_net = ctx_root->push_back<com::Folder>(folder_name());
	root_net->push_back<Driver>();

	root_net->push_back<RPCSystem>();
}

void net::shutdown(com::Folder *ctx_root)
{
	auto* const root_net = ctx_root->find<com::Folder>(folder_name());
	root_net->erase(RPCSystem::self_name());
	root_net->erase(Driver::self_name());

	ctx_root->erase(root_net);
Loading