Fix this up a little so we retry a little right after failure.

This commit is contained in:
Chris Marsh 2017-07-10 16:00:05 -07:00
parent f41137e5cf
commit e3d663bc95
3 changed files with 22 additions and 12 deletions

View file

@ -11,10 +11,12 @@
#include "yolojson.h" #include "yolojson.h"
const int RpcVersion = 1; const int RpcVersion = 1;
const int NumFrames = 3;
struct WinRpcConnection : public RpcConnection { struct WinRpcConnection : public RpcConnection {
HANDLE pipe{INVALID_HANDLE_VALUE}; HANDLE pipe{INVALID_HANDLE_VALUE};
RpcMessageFrame frame; RpcMessageFrame frames[NumFrames];
int nextFrame{0};
}; };
static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc"; static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc";
@ -39,9 +41,6 @@ void RpcConnection::Open()
for (;;) { for (;;) {
self->pipe = ::CreateFileW(PipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); self->pipe = ::CreateFileW(PipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
if (self->pipe != INVALID_HANDLE_VALUE) { if (self->pipe != INVALID_HANDLE_VALUE) {
if (self->onConnect) {
self->onConnect();
}
break; break;
} }
@ -62,6 +61,10 @@ void RpcConnection::Open()
JsonWriteHandshakeObj(msg, RpcVersion, appId); JsonWriteHandshakeObj(msg, RpcVersion, appId);
frame->length = msg - frame->message; frame->length = msg - frame->message;
WriteFrame(frame); WriteFrame(frame);
if (self->onConnect) {
self->onConnect();
}
} }
void RpcConnection::Close() void RpcConnection::Close()
@ -77,15 +80,18 @@ void RpcConnection::Close()
void RpcConnection::Write(const void* data, size_t length) void RpcConnection::Write(const void* data, size_t length)
{ {
auto self = reinterpret_cast<WinRpcConnection*>(this); auto self = reinterpret_cast<WinRpcConnection*>(this);
const int retries = 3;
if (self->pipe == INVALID_HANDLE_VALUE) { for (int i = 0; i < retries; ++i) {
self->Open();
if (self->pipe == INVALID_HANDLE_VALUE) { 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(); self->Close();
} }
} }
@ -93,7 +99,9 @@ void RpcConnection::Write(const void* data, size_t length)
RpcMessageFrame* RpcConnection::GetNextFrame() RpcMessageFrame* RpcConnection::GetNextFrame()
{ {
auto self = reinterpret_cast<WinRpcConnection*>(this); auto self = reinterpret_cast<WinRpcConnection*>(this);
return &(self->frame); auto result = &(self->frames[self->nextFrame]);
self->nextFrame = (self->nextFrame + 1) % NumFrames;
return result;
} }
void RpcConnection::WriteFrame(RpcMessageFrame* frame) void RpcConnection::WriteFrame(RpcMessageFrame* frame)

View file

@ -34,6 +34,7 @@ extern "C" void Discord_Shutdown()
extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
{ {
auto frame = MyConnection->GetNextFrame(); auto frame = MyConnection->GetNextFrame();
frame->opcode = OPCODE::FRAME;
char* jsonWrite = frame->message; char* jsonWrite = frame->message;
JsonWriteRichPresenceObj(jsonWrite, presence); JsonWriteRichPresenceObj(jsonWrite, presence);
frame->length = jsonWrite - frame->message; frame->length = jsonWrite - frame->message;

View file

@ -190,6 +190,7 @@ inline void JsonWriteRichPresenceObj(char*& dest, const DiscordRichPresence* pre
dest -= 1; dest -= 1;
*(dest - 1) = '}'; *(dest - 1) = '}';
*dest = 0;
} }
inline void JsonWriteHandshakeObj(char*& dest, int version, const char* applicationId) inline void JsonWriteHandshakeObj(char*& dest, int version, const char* applicationId)