AuroraRPC/README.md

64 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2022-07-02 22:08:52 +00:00
# Experimental RPC library
## Example
```cpp
#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);
}
2022-12-28 12:09:07 +00:00
```