138 lines
3.0 KiB
C++
138 lines
3.0 KiB
C++
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: AuRPCAPI.hpp
|
||
|
Date: 2022-6-29
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#pragma once
|
||
|
|
||
|
struct AuRPCRequest;
|
||
|
|
||
|
AUE_DEFINE(ERPCRequestState, (
|
||
|
ePending,
|
||
|
eSubmitting,
|
||
|
eSent,
|
||
|
|
||
|
eResponse,
|
||
|
eFailed
|
||
|
));
|
||
|
|
||
|
AUE_DEFINE(ERPCError, (
|
||
|
eNone,
|
||
|
eMissingService,
|
||
|
eMissingMethod,
|
||
|
eProtocolError,
|
||
|
eIOError,
|
||
|
eChannelTerminated,
|
||
|
eChannelFailure,
|
||
|
eAborted
|
||
|
));
|
||
|
|
||
|
struct AuRPCResponse
|
||
|
{
|
||
|
virtual ~AuRPCResponse()
|
||
|
{
|
||
|
}
|
||
|
AuRPCResponse() : error(ERPCError::eNone)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
AuRPCResponse(ERPCError error) : error(error)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ERPCError error;
|
||
|
AuUInt64 cookie {};
|
||
|
AuByteBuffer *message {};
|
||
|
|
||
|
void Deserialize()
|
||
|
{
|
||
|
this->error = AuStaticCast<ERPCError>(message->Read<AuUInt8>());
|
||
|
this->cookie = message->Read<AuUInt64>();
|
||
|
}
|
||
|
|
||
|
void PrepareMessage()
|
||
|
{
|
||
|
message->Write<AuUInt8>(0);
|
||
|
message->Write<AuUInt64>(this->cookie);
|
||
|
}
|
||
|
|
||
|
void WriteError()
|
||
|
{
|
||
|
message->Write<AuUInt8>(AuStaticCast<AuUInt8>(this->error));
|
||
|
message->Write<AuUInt64>(this->cookie);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
struct AuRPCResponseOwned : AuRPCResponse
|
||
|
{
|
||
|
AuSPtr<AuByteBuffer> buffer;
|
||
|
|
||
|
void PrepareResponse(AuUInt8 type)
|
||
|
{
|
||
|
buffer = AuMakeShared<AuByteBuffer>();
|
||
|
SysAssert(buffer);
|
||
|
this->message = buffer.get();
|
||
|
buffer->Write<AuUInt32>(0);
|
||
|
buffer->Write(type);
|
||
|
}
|
||
|
|
||
|
void FinalizeWrite()
|
||
|
{
|
||
|
auto length = buffer->writePtr - buffer->base;
|
||
|
buffer->writePtr = buffer->base;
|
||
|
buffer->Write<AuUInt32>(length);
|
||
|
buffer->writePtr += (length - 4);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
AUI_INTERFACE(AuRPCRequestCallback,
|
||
|
AUI_METHOD(void, OnResponse, (const AuRPCResponse &, response))
|
||
|
);
|
||
|
|
||
|
AUI_INTERFACE(AuIRPCChannelCallbacks,
|
||
|
AUI_METHOD(void, OnConnect, ()),
|
||
|
AUI_METHOD(void, OnDisconnect, ())
|
||
|
);
|
||
|
|
||
|
struct AuIRPCRequest
|
||
|
{
|
||
|
virtual bool SetData(const AuByteBuffer &toRead) = 0;
|
||
|
virtual bool SetData(const AuMemoryViewRead &view) = 0;
|
||
|
|
||
|
virtual void SetCallback(AuSPtr<AuRPCRequestCallback> callback) = 0;
|
||
|
};
|
||
|
|
||
|
struct AuIRPCClientChannel
|
||
|
{
|
||
|
virtual void Disconnect() = 0;
|
||
|
virtual void SendRequest(AuSPtr<AuIRPCRequest> response) = 0;
|
||
|
virtual bool IsConnected() = 0;
|
||
|
virtual void SetCallbacks(const AuSPtr<AuIRPCChannelCallbacks> &callbacks) = 0;
|
||
|
};
|
||
|
|
||
|
struct AuIRPCService
|
||
|
{
|
||
|
virtual AuUInt32 GetId() = 0;
|
||
|
virtual void Dispatch(AuRPCResponse &response, AuUInt32 id, AuByteBuffer& buffer) = 0;
|
||
|
};
|
||
|
|
||
|
|
||
|
struct AuIRPCServer
|
||
|
{
|
||
|
virtual bool RegisterService(const AuSPtr<AuIRPCService> service) = 0;
|
||
|
virtual AuString ExportString() = 0;
|
||
|
};
|
||
|
|
||
|
struct AuIRPC
|
||
|
{
|
||
|
virtual bool StartClient(AuAsync::WorkerPId_t worker) = 0;
|
||
|
virtual bool StartServer(AuAsync::WorkerPId_t worker) = 0;
|
||
|
|
||
|
virtual AuSPtr<AuIRPCServer> ToServer() = 0;
|
||
|
virtual AuSPtr<AuIRPCClientChannel> Connect(const AuString& str) = 0;
|
||
|
};
|
||
|
|
||
|
AuSPtr<AuIRPCRequest> AuRPCNewRequest(AuUInt32 serviceId, AuUInt32 methodId);
|
||
|
AuSPtr<AuIRPC> AuRPCNewInstance();
|