diff --git a/src/connection_win_sync.cpp b/src/connection_win_sync.cpp index 866a6a2..c712dc0 100644 --- a/src/connection_win_sync.cpp +++ b/src/connection_win_sync.cpp @@ -11,10 +11,12 @@ #include "yolojson.h" const int RpcVersion = 1; +const int NumFrames = 3; struct WinRpcConnection : public RpcConnection { HANDLE pipe{INVALID_HANDLE_VALUE}; - RpcMessageFrame frame; + RpcMessageFrame frames[NumFrames]; + int nextFrame{0}; }; static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc"; @@ -39,9 +41,6 @@ void RpcConnection::Open() for (;;) { self->pipe = ::CreateFileW(PipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); if (self->pipe != INVALID_HANDLE_VALUE) { - if (self->onConnect) { - self->onConnect(); - } break; } @@ -62,6 +61,10 @@ void RpcConnection::Open() JsonWriteHandshakeObj(msg, RpcVersion, appId); frame->length = msg - frame->message; WriteFrame(frame); + + if (self->onConnect) { + self->onConnect(); + } } void RpcConnection::Close() @@ -77,15 +80,18 @@ void RpcConnection::Close() void RpcConnection::Write(const void* data, size_t length) { auto self = reinterpret_cast(this); - - if (self->pipe == INVALID_HANDLE_VALUE) { - self->Open(); + const int retries = 3; + for (int i = 0; i < retries; ++i) { if (self->pipe == INVALID_HANDLE_VALUE) { - return; + self->Open(); + if (self->pipe == INVALID_HANDLE_VALUE) { + break; + } + } + BOOL success = ::WriteFile(self->pipe, data, length, nullptr, nullptr); + if (success) { + break; } - } - BOOL success = ::WriteFile(self->pipe, data, length, nullptr, nullptr); - if (!success) { self->Close(); } } @@ -93,7 +99,9 @@ void RpcConnection::Write(const void* data, size_t length) RpcMessageFrame* RpcConnection::GetNextFrame() { auto self = reinterpret_cast(this); - return &(self->frame); + auto result = &(self->frames[self->nextFrame]); + self->nextFrame = (self->nextFrame + 1) % NumFrames; + return result; } void RpcConnection::WriteFrame(RpcMessageFrame* frame) diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index 498d6d6..45e9b4f 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -34,6 +34,7 @@ extern "C" void Discord_Shutdown() extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) { auto frame = MyConnection->GetNextFrame(); + frame->opcode = OPCODE::FRAME; char* jsonWrite = frame->message; JsonWriteRichPresenceObj(jsonWrite, presence); frame->length = jsonWrite - frame->message; diff --git a/src/yolojson.h b/src/yolojson.h index e8298fd..0dc74e0 100644 --- a/src/yolojson.h +++ b/src/yolojson.h @@ -190,6 +190,7 @@ inline void JsonWriteRichPresenceObj(char*& dest, const DiscordRichPresence* pre dest -= 1; *(dest - 1) = '}'; + *dest = 0; } inline void JsonWriteHandshakeObj(char*& dest, int version, const char* applicationId)