mirror of
https://github.com/yuzu-emu/discord-rpc.git
synced 2024-11-24 04:45:46 +01:00
Wait for READY event for connection.
This commit is contained in:
parent
559f56b05c
commit
063a329a0b
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
#include "discord-rpc.h"
|
#include "discord-rpc.h"
|
||||||
|
|
||||||
static const char* APPLICATION_ID = "12345678910";
|
static const char* APPLICATION_ID = "338030514596216832";
|
||||||
static int FrustrationLevel = 0;
|
static int FrustrationLevel = 0;
|
||||||
|
|
||||||
static void updateDiscordPresence() {
|
static void updateDiscordPresence() {
|
||||||
|
@ -27,20 +27,40 @@ void RpcConnection::Open()
|
|||||||
|
|
||||||
if (state == State::Disconnected) {
|
if (state == State::Disconnected) {
|
||||||
if (connection->Open()) {
|
if (connection->Open()) {
|
||||||
state = State::Connecting;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return;
|
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.opcode = Opcode::Handshake;
|
||||||
sendFrame.length = JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId);
|
sendFrame.length = JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId);
|
||||||
|
|
||||||
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) {
|
||||||
state = State::Connected;
|
state = State::SentHandshake;
|
||||||
if (onConnect) {
|
}
|
||||||
onConnect();
|
else {
|
||||||
|
Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,7 +88,7 @@ bool RpcConnection::Write(const void* data, size_t length)
|
|||||||
|
|
||||||
bool RpcConnection::Read(rapidjson::Document& message)
|
bool RpcConnection::Read(rapidjson::Document& message)
|
||||||
{
|
{
|
||||||
if (state != State::Connected) {
|
if (state != State::Connected && state != State::SentHandshake) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
MessageFrame readFrame;
|
MessageFrame readFrame;
|
||||||
|
@ -26,7 +26,8 @@ struct RpcConnection {
|
|||||||
|
|
||||||
enum class State : uint32_t {
|
enum class State : uint32_t {
|
||||||
Disconnected,
|
Disconnected,
|
||||||
Connecting,
|
SentHandshake,
|
||||||
|
AwaitingResponse,
|
||||||
Connected,
|
Connected,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user