/*** 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(message->Read()); this->cookie = message->Read(); } void PrepareMessage() { message->Write(0); message->Write(this->cookie); } void WriteError() { message->Write(AuStaticCast(this->error)); message->Write(this->cookie); } }; struct AuRPCResponseOwned : AuRPCResponse { AuSPtr buffer; void PrepareResponse(AuUInt8 type) { buffer = AuMakeShared(); SysAssert(buffer); this->message = buffer.get(); buffer->Write(0); buffer->Write(type); } void FinalizeWrite() { auto length = buffer->writePtr - buffer->base; buffer->writePtr = buffer->base; buffer->Write(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 bool EmptyRequest() = 0; virtual void SetCallback(AuSPtr callback) = 0; }; struct AuIRPCClientChannel { virtual void Disconnect() = 0; virtual void SendRequest(AuSPtr response) = 0; virtual bool IsConnected() = 0; virtual void SetCallbacks(const AuSPtr &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 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 ToServer() = 0; virtual AuSPtr Connect(const AuString& str) = 0; }; AuSPtr AuRPCNewRequest(AuUInt32 serviceId, AuUInt32 methodId); AuSPtr AuRPCNewInstance();