AuroraRPC/README.md

1.6 KiB

Experimental RPC library

Example

#include <AuRPCAPI.hpp>

static AuSPtr<AuIRPC> gRpcServer = AuRPCNewInstance();
static AuSPtr<AuIRPC> gRpcClient = AuRPCNewInstance();
static AuSPtr<AuIRPCClientChannel> gRpcClientChannel;

void Run()
{
    SysAssert(gRpcServer->StartServer(AuAsync::GetCurrentWorkerPId()));
    SysAssert(gRpcClient->StartClient(AuAsync::GetCurrentWorkerPId()));

    auto server = gRpcServer->ToServer();

    auto handle = server->ExportString();

    struct DummyService : AuIRPCService
    {
        AuUInt32 GetId() override
        {
            return 2;
        }

        void Dispatch(AuRPCResponse &response, AuUInt32 id, AuByteBuffer &buffer) override
        {
            AuLogDbg("ID = {}, bytes = {}", id, buffer.RemainingBytes());
            
            response.message->Write<AuString>("Hello World");
        }
    };

    server->RegisterService(AuMakeShared<DummyService>());

    gRpcClientChannel = gRpcClient->Connect(handle);
    SysAssert(gRpcClientChannel);

    auto callback = AuMakeShared<AuIRPCChannelCallbacksFunctional>();
    SysAssert(callback);

    callback->OnConnectFunctional = []()
    {
        auto request = AuRPCNewRequest(2, 3);
        SysAssert(request);

        auto callback = AuMakeShared<AuRPCRequestCallbackFunctional>();
        SysAssert(callback);

        callback->OnResponseFunctional = [](const AuRPCResponse &res)
        {
            AuLogDbg("Complete! Message = {}", res.message->Read<AuString>());
        };

        request->SetCallback(callback);
        gRpcClientChannel->SendRequest(request);
    };

    gRpcClientChannel->SetCallbacks(callback);
}