mirror of
https://github.com/yuzu-emu/discord-rpc
synced 2024-11-26 09:57:46 +00:00
Wait for READY event for connection.
This commit is contained in:
parent
559f56b05c
commit
063a329a0b
3 changed files with 32 additions and 11 deletions
|
@ -11,7 +11,7 @@
|
|||
|
||||
#include "discord-rpc.h"
|
||||
|
||||
static const char* APPLICATION_ID = "12345678910";
|
||||
static const char* APPLICATION_ID = "338030514596216832";
|
||||
static int FrustrationLevel = 0;
|
||||
|
||||
static void updateDiscordPresence() {
|
||||
|
|
|
@ -27,20 +27,40 @@ void RpcConnection::Open()
|
|||
|
||||
if (state == State::Disconnected) {
|
||||
if (connection->Open()) {
|
||||
state = State::Connecting;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (state == State::SentHandshake) {
|
||||
rapidjson::Document message;
|
||||
if (Read(message)) {
|
||||
auto cmd = message.FindMember("cmd");
|
||||
if (cmd == message.MemberEnd() || !cmd->value.IsString()) {
|
||||
return;
|
||||
}
|
||||
auto evt = message.FindMember("evt");
|
||||
if (evt == message.MemberEnd() || !evt->value.IsString()) {
|
||||
return;
|
||||
}
|
||||
if (!strcmp(cmd->value.GetString(), "DISPATCH") && !strcmp(evt->value.GetString(), "READY")) {
|
||||
state = State::Connected;
|
||||
if (onConnect) {
|
||||
onConnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
sendFrame.opcode = Opcode::Handshake;
|
||||
sendFrame.length = JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId);
|
||||
|
||||
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
||||
state = State::Connected;
|
||||
if (onConnect) {
|
||||
onConnect();
|
||||
state = State::SentHandshake;
|
||||
}
|
||||
else {
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,7 +88,7 @@ bool RpcConnection::Write(const void* data, size_t length)
|
|||
|
||||
bool RpcConnection::Read(rapidjson::Document& message)
|
||||
{
|
||||
if (state != State::Connected) {
|
||||
if (state != State::Connected && state != State::SentHandshake) {
|
||||
return false;
|
||||
}
|
||||
MessageFrame readFrame;
|
||||
|
|
|
@ -26,7 +26,8 @@ struct RpcConnection {
|
|||
|
||||
enum class State : uint32_t {
|
||||
Disconnected,
|
||||
Connecting,
|
||||
SentHandshake,
|
||||
AwaitingResponse,
|
||||
Connected,
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue