2017-07-17 17:28:54 +01:00
|
|
|
#include "rpc_connection.h"
|
2017-07-17 22:49:31 +01:00
|
|
|
#include "yolojson.h"
|
2017-07-17 17:28:54 +01:00
|
|
|
|
2017-07-17 22:49:31 +01:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
static const int RpcVersion = 1;
|
|
|
|
static RpcConnection Instance;
|
2017-07-17 17:28:54 +01:00
|
|
|
|
|
|
|
/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId)
|
|
|
|
{
|
|
|
|
Instance.connection = BaseConnection::Create();
|
|
|
|
StringCopy(Instance.appId, applicationId, sizeof(Instance.appId));
|
|
|
|
return &Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ void RpcConnection::Destroy(RpcConnection*& c)
|
|
|
|
{
|
2017-07-17 22:49:31 +01:00
|
|
|
c->Close();
|
2017-07-17 17:28:54 +01:00
|
|
|
BaseConnection::Destroy(c->connection);
|
|
|
|
}
|
|
|
|
|
2017-07-17 22:49:31 +01:00
|
|
|
void RpcConnection::Open()
|
|
|
|
{
|
|
|
|
if (state == State::Connected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == State::Disconnected) {
|
|
|
|
if (connection->Open()) {
|
|
|
|
state = State::Connecting;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 23:42:06 +01:00
|
|
|
sendFrame.opcode = Opcode::Handshake;
|
|
|
|
char* json = sendFrame.message;
|
2017-07-17 22:49:31 +01:00
|
|
|
JsonWriteHandshakeObj(json, RpcVersion, appId);
|
2017-07-17 23:42:06 +01:00
|
|
|
sendFrame.length = json - sendFrame.message;
|
2017-07-17 22:49:31 +01:00
|
|
|
|
2017-07-17 23:42:06 +01:00
|
|
|
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
2017-07-17 22:49:31 +01:00
|
|
|
state = State::Connected;
|
|
|
|
if (onConnect) {
|
|
|
|
onConnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RpcConnection::Close()
|
|
|
|
{
|
|
|
|
if (onDisconnect && state == State::Connected) {
|
|
|
|
onDisconnect(lastErrorCode, lastErrorMessage);
|
|
|
|
}
|
|
|
|
connection->Close();
|
|
|
|
state = State::Disconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RpcConnection::Write(const void* data, size_t length)
|
|
|
|
{
|
2017-07-17 23:42:06 +01:00
|
|
|
sendFrame.opcode = Opcode::Frame;
|
|
|
|
memcpy(sendFrame.message, data, length);
|
|
|
|
sendFrame.length = length;
|
|
|
|
if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) {
|
2017-07-17 22:49:31 +01:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RpcConnection::Read(rapidjson::Document& message)
|
|
|
|
{
|
|
|
|
if (state != State::Connected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MessageFrame readFrame;
|
|
|
|
for (;;) {
|
|
|
|
bool didRead = connection->Read(&readFrame, sizeof(MessageFrameHeader));
|
|
|
|
if (!didRead) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readFrame.length > 0) {
|
|
|
|
didRead = connection->Read(readFrame.message, readFrame.length);
|
|
|
|
if (!didRead) {
|
|
|
|
lastErrorCode = -2;
|
|
|
|
StringCopy(lastErrorMessage, "Partial data in frame");
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
readFrame.message[readFrame.length] = 0;
|
|
|
|
message.ParseInsitu(readFrame.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (readFrame.opcode) {
|
|
|
|
case Opcode::Close:
|
|
|
|
{
|
|
|
|
lastErrorCode = message["code"].GetInt();
|
|
|
|
const auto& m = message["message"];
|
|
|
|
StringCopy(lastErrorMessage, m.GetString(), sizeof(lastErrorMessage));
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
case Opcode::Frame:
|
|
|
|
return true;
|
|
|
|
case Opcode::Ping:
|
|
|
|
{
|
|
|
|
MessageFrameHeader frame{ Opcode::Pong, 0 };
|
|
|
|
if (!connection->Write(&frame, sizeof(MessageFrameHeader))) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Opcode::Pong:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// something bad happened
|
|
|
|
lastErrorCode = -1;
|
|
|
|
StringCopy(lastErrorMessage, "Bad ipc frame");
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|