Skip to content

Commit dd47c7c

Browse files
MinnDevelopmentmsciotti
authored andcommitted
Improve handling of disconnects and reconnects (discord#228)
* Check response 0 on disconnect From recv(): The return value will be 0 when the peer has performed an orderly shutdown * Add persistent presence and handlers * Use buffer instead of raw struct * Clear presence data on shutdown * Remove CurrentPresence and add boolean instead This removes the need for having 2 big buffers in favor of using a small boolean
1 parent 98855b4 commit dd47c7c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/connection_unix.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,8 @@ bool BaseConnection::Read(void* data, size_t length)
118118
}
119119
Close();
120120
}
121+
else if (res == 0) {
122+
Close();
123+
}
121124
return res == (int)length;
122125
}

src/discord_rpc.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static std::atomic_bool WasJustDisconnected{false};
5454
static std::atomic_bool GotErrorMessage{false};
5555
static std::atomic_bool WasJoinGame{false};
5656
static std::atomic_bool WasSpectateGame{false};
57+
static std::atomic_bool UpdatePresence{false};
5758
static char JoinGameSecret[256];
5859
static char SpectateGameSecret[256];
5960
static int LastErrorCode{0};
@@ -214,17 +215,17 @@ static void Discord_UpdateConnection(void)
214215
}
215216

216217
// writes
217-
if (QueuedPresence.length) {
218+
if (UpdatePresence.exchange(false) && QueuedPresence.length) {
218219
QueuedMessage local;
219220
{
220221
std::lock_guard<std::mutex> guard(PresenceMutex);
221222
local.Copy(QueuedPresence);
222-
QueuedPresence.length = 0;
223223
}
224224
if (!Connection->Write(local.buffer, local.length)) {
225225
// if we fail to send, requeue
226226
std::lock_guard<std::mutex> guard(PresenceMutex);
227227
QueuedPresence.Copy(local);
228+
UpdatePresence.exchange(true);
228229
}
229230
}
230231

@@ -310,6 +311,10 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
310311
Connection = RpcConnection::Create(applicationId);
311312
Connection->onConnect = [](JsonDocument& readyMessage) {
312313
Discord_UpdateHandlers(&QueuedHandlers);
314+
if (QueuedPresence.length > 0) {
315+
UpdatePresence.exchange(true);
316+
SignalIOActivity();
317+
}
313318
auto data = GetObjMember(&readyMessage, "data");
314319
auto user = GetObjMember(data, "user");
315320
auto userId = GetStrMember(user, "id");
@@ -335,10 +340,6 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
335340
Connection->onDisconnect = [](int err, const char* message) {
336341
LastDisconnectErrorCode = err;
337342
StringCopy(LastDisconnectErrorMessage, message);
338-
{
339-
std::lock_guard<std::mutex> guard(HandlerMutex);
340-
Handlers = {};
341-
}
342343
WasJustDisconnected.exchange(true);
343344
UpdateReconnectTime();
344345
};
@@ -354,6 +355,8 @@ extern "C" DISCORD_EXPORT void Discord_Shutdown(void)
354355
Connection->onConnect = nullptr;
355356
Connection->onDisconnect = nullptr;
356357
Handlers = {};
358+
QueuedPresence.length = 0;
359+
UpdatePresence.exchange(false);
357360
if (IoThread != nullptr) {
358361
IoThread->Stop();
359362
delete IoThread;
@@ -369,6 +372,7 @@ extern "C" DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence*
369372
std::lock_guard<std::mutex> guard(PresenceMutex);
370373
QueuedPresence.length = JsonWriteRichPresenceObj(
371374
QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
375+
UpdatePresence.exchange(true);
372376
}
373377
SignalIOActivity();
374378
}

0 commit comments

Comments
 (0)