mirror of
https://git.suyu.dev/suyu/discord-rpc.git
synced 2026-01-11 00:48:35 +01:00
Let's use rapidjson instead of roll-your-own json. Added helpers to keep allocations minimized.
This commit is contained in:
parent
12054246a2
commit
79d70b8bae
6 changed files with 234 additions and 224 deletions
|
|
@ -1,5 +1,5 @@
|
|||
#include "rpc_connection.h"
|
||||
#include "yolojson.h"
|
||||
#include "serialization.h"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
|
|
@ -9,7 +9,7 @@ static RpcConnection Instance;
|
|||
/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId)
|
||||
{
|
||||
Instance.connection = BaseConnection::Create();
|
||||
StringCopy(Instance.appId, applicationId, sizeof(Instance.appId));
|
||||
StringCopy(Instance.appId, applicationId);
|
||||
return &Instance;
|
||||
}
|
||||
|
||||
|
|
@ -35,9 +35,7 @@ void RpcConnection::Open()
|
|||
}
|
||||
|
||||
sendFrame.opcode = Opcode::Handshake;
|
||||
char* json = sendFrame.message;
|
||||
JsonWriteHandshakeObj(json, RpcVersion, appId);
|
||||
sendFrame.length = json - sendFrame.message;
|
||||
sendFrame.length = JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId);
|
||||
|
||||
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
||||
state = State::Connected;
|
||||
|
|
@ -97,7 +95,7 @@ bool RpcConnection::Read(rapidjson::Document& message)
|
|||
message.ParseInsitu(readFrame.message);
|
||||
lastErrorCode = message["code"].GetInt();
|
||||
const auto& m = message["message"];
|
||||
StringCopy(lastErrorMessage, m.GetString(), sizeof(lastErrorMessage));
|
||||
StringCopy(lastErrorMessage, m.GetString());
|
||||
Close();
|
||||
return false;
|
||||
}
|
||||
|
|
@ -105,13 +103,11 @@ bool RpcConnection::Read(rapidjson::Document& message)
|
|||
message.ParseInsitu(readFrame.message);
|
||||
return true;
|
||||
case Opcode::Ping:
|
||||
{
|
||||
readFrame.opcode = Opcode::Pong;
|
||||
if (!connection->Write(&readFrame, sizeof(MessageFrameHeader) + readFrame.length)) {
|
||||
Close();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Opcode::Pong:
|
||||
break;
|
||||
default:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue