2022-07-02 22:08:52 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuRPCServerChannel.cpp
|
|
|
|
Date: 2022-6-29
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <AuroraRuntime.hpp>
|
|
|
|
#include "AuRPC.hpp"
|
|
|
|
#include "AuRPCServerChannel.hpp"
|
|
|
|
#include "AuRPCRequest.hpp"
|
|
|
|
#include "AuRPCPipePacket.hpp"
|
|
|
|
|
|
|
|
AuRPCServerChannel::AuRPCServerChannel(AuSPtr<AuRPC> parent, AuSPtr<AuRPCServer> server) :
|
|
|
|
parent(parent),
|
|
|
|
pipe(this),
|
|
|
|
server_(server)
|
|
|
|
{
|
2023-12-16 18:16:32 +00:00
|
|
|
this->uConnectTime_ = AuTime::SteadyClockNS();
|
|
|
|
}
|
2022-07-02 22:08:52 +00:00
|
|
|
|
2023-12-16 18:16:32 +00:00
|
|
|
AuRPCServerChannel::~AuRPCServerChannel()
|
|
|
|
{
|
|
|
|
this->RemoveFromParent();
|
2022-07-02 22:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<AuRPC> AuRPCServerChannel::ToContext()
|
|
|
|
{
|
|
|
|
return this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuRPCServerChannel::SendResponse(AuSPtr<AuRPCResponse> response)
|
|
|
|
{
|
|
|
|
if (response->message->flagWriteError)
|
|
|
|
{
|
|
|
|
this->FatalError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuRPCPipePacket packet;
|
|
|
|
packet.serverResponse = response;
|
|
|
|
this->pipe.SendPacket(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuString AuRPCServerChannel::ExportString()
|
|
|
|
{
|
|
|
|
return this->pipe.pipe->ExportToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuRPCServerChannel::OnConnect()
|
|
|
|
{
|
|
|
|
RpcLogDebug("Server channel: on connect");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuRPCServerChannel::OnDisconnect(bool error)
|
|
|
|
{
|
|
|
|
RpcLogDebug("Server channel disconnected: {}", error);
|
2023-12-16 18:16:32 +00:00
|
|
|
this->RemoveFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuRPCServerChannel::RemoveFromParent()
|
|
|
|
{
|
2023-12-16 19:56:10 +00:00
|
|
|
if (auto pCallbacks = this->server_->pCallbacks)
|
|
|
|
{
|
|
|
|
if (!this->isTempChannel_)
|
|
|
|
{
|
|
|
|
pCallbacks->OnDisconnect(this->SharedFromThis());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 18:16:32 +00:00
|
|
|
if (auto pParent = this->server_)
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(pParent->lock->AsWritable());
|
|
|
|
|
|
|
|
if (auto pChannel = pParent->channel)
|
|
|
|
{
|
|
|
|
AuRemoveIf(pChannel->subchannels_,
|
|
|
|
[=](AuSPtr<AuRPCServerChannel> pChannel) -> bool
|
|
|
|
{
|
|
|
|
return pChannel.get() == this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 22:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AuRPCServerChannel::OnDataAvailable(AuByteBuffer& view)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
auto bytesAvailable = view.RemainingBytes();
|
|
|
|
|
|
|
|
if (bytesAvailable < 4)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto oldRead = view.readPtr;
|
2022-07-07 04:16:27 +00:00
|
|
|
|
|
|
|
auto frameLength = view.Read<AuUInt32>();
|
|
|
|
if (!frameLength)
|
|
|
|
{
|
|
|
|
FatalError();
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-02 22:08:52 +00:00
|
|
|
|
2022-07-07 04:16:27 +00:00
|
|
|
if (frameLength > bytesAvailable)
|
2022-07-02 22:08:52 +00:00
|
|
|
{
|
|
|
|
view.readPtr = oldRead;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto packetType = view.Read<AuUInt8>();
|
|
|
|
|
|
|
|
if (packetType == kRequestConnect)
|
|
|
|
{
|
|
|
|
if (this->isTempChannel_)
|
|
|
|
{
|
|
|
|
this->SendToNewChannel();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->SendConnectOK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (packetType == kRequestRPC)
|
|
|
|
{
|
|
|
|
auto request = AuMakeShared<AuRPCRequest>();
|
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
view.readPtr = oldRead;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->serviceId = view.Read<AuUInt32>();
|
|
|
|
request->methodId = view.Read<AuUInt32>();
|
|
|
|
request->cookie = view.Read<AuUInt64>();
|
|
|
|
|
2022-07-07 04:16:27 +00:00
|
|
|
auto oldLength = view.length;
|
2023-12-16 18:16:32 +00:00
|
|
|
auto oldWriteHead = view.writePtr;
|
2022-07-07 04:16:27 +00:00
|
|
|
view.length = (oldRead - view.base) + frameLength;
|
2023-12-16 18:16:32 +00:00
|
|
|
view.writePtr = view.base + view.length;
|
2022-07-07 04:16:27 +00:00
|
|
|
|
2022-07-02 22:08:52 +00:00
|
|
|
this->server_->Dispatch(this, request, &view);
|
2022-07-07 04:16:27 +00:00
|
|
|
|
2023-12-16 18:16:32 +00:00
|
|
|
view.readPtr = oldRead + frameLength;
|
|
|
|
view.writePtr = oldWriteHead;
|
|
|
|
view.length = oldLength;
|
|
|
|
}
|
|
|
|
else if (packetType == kGeneralServerMessage)
|
|
|
|
{
|
|
|
|
auto oldLength = view.length;
|
|
|
|
auto oldWriteHead = view.writePtr;
|
|
|
|
view.length = (oldRead - view.base) + frameLength;
|
|
|
|
view.writePtr = view.base + view.length;
|
|
|
|
|
|
|
|
if (auto pCallbacks = this->server_->pCallbacks)
|
|
|
|
{
|
|
|
|
pCallbacks->OnMessage(this->SharedFromThis(),
|
|
|
|
view);
|
|
|
|
}
|
|
|
|
|
|
|
|
view.readPtr = oldRead + frameLength;
|
|
|
|
view.writePtr = oldWriteHead;
|
2022-07-07 04:16:27 +00:00
|
|
|
view.length = oldLength;
|
2022-07-02 22:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
while (true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 18:16:32 +00:00
|
|
|
AuUInt64 AuRPCServerChannel::GetConnectTimeNS()
|
|
|
|
{
|
|
|
|
return this->uConnectTime_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuRPCServerChannel::SendMessage(const AuMemoryViewRead &view)
|
|
|
|
{
|
|
|
|
auto pMessage = AuMakeSharedPanic<AuRPCResponseOwned>();
|
|
|
|
pMessage->PrepareResponse(kGeneralClientMessage);
|
|
|
|
pMessage->buffer->Write(view.ptr, view.length);
|
|
|
|
pMessage->FinalizeWrite();
|
|
|
|
this->SendResponse(pMessage);
|
|
|
|
}
|
|
|
|
|
2022-07-02 22:08:52 +00:00
|
|
|
void AuRPCServerChannel::SendConnectOK()
|
|
|
|
{
|
|
|
|
RpcLogDebug("AuRPCServerChannel::SendConnectOK");
|
|
|
|
|
|
|
|
auto res = AuMakeShared<AuRPCResponseOwned>();
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
FatalError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->PrepareResponse(kResponseConnectOK);
|
|
|
|
res->FinalizeWrite();
|
|
|
|
|
|
|
|
this->SendResponse(res);
|
2023-12-16 19:56:10 +00:00
|
|
|
|
|
|
|
if (auto pCallbacks = this->server_->pCallbacks)
|
|
|
|
{
|
|
|
|
pCallbacks->OnConnect(this->SharedFromThis());
|
|
|
|
}
|
2022-07-02 22:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AuRPCServerChannel::SendToNewChannel()
|
|
|
|
{
|
|
|
|
RpcLogDebug("AuRPCServerChannel::SendToNewChannel");
|
|
|
|
|
|
|
|
auto res = AuMakeShared<AuRPCResponseOwned>();
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
FatalError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->PrepareResponse(kResponseMulticonnect);
|
2023-12-16 18:16:32 +00:00
|
|
|
auto channel = this->server_->NewChannel(false);
|
2022-07-02 22:08:52 +00:00
|
|
|
if (!channel)
|
|
|
|
{
|
|
|
|
this->FatalError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AuTryInsert(this->subchannels_, channel))
|
|
|
|
{
|
|
|
|
this->FatalError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->buffer->Write(channel->ExportString());
|
|
|
|
|
|
|
|
res->FinalizeWrite();
|
|
|
|
|
|
|
|
this->SendResponse(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuRPCServerChannel::FatalError()
|
|
|
|
{
|
|
|
|
RpcLogDebug("AuRPCServerChannel::FatalError");
|
|
|
|
this->pipe.Deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AuRPCServerChannel::Init()
|
|
|
|
{
|
2023-12-16 18:16:32 +00:00
|
|
|
return this->pipe.Init(this->parent->optPipeLength);
|
2022-07-02 22:08:52 +00:00
|
|
|
}
|