From 60ad70adc2897e4e7e894285d4d65c80621e9a93 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Wed, 2 Aug 2017 10:44:55 -0700 Subject: [PATCH] Fix ordering of ready/disconnect callbacks. --- src/discord-rpc.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index f633165..ed1c975 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -274,18 +274,28 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) extern "C" void Discord_RunCallbacks() { - if (GotErrorMessage.exchange(false) && Handlers.errored) { - Handlers.errored(LastErrorCode, LastErrorMessage); - } + // Note on some weirdness: internally we might connect, get other signals, disconnect any number + // of times inbetween calls here. Externally, we want the sequence to seem sane, so any other + // signals are book-ended by calls to ready and disconnect. - if (WasJustDisconnected.exchange(false) && Handlers.disconnected) { - Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage); + bool wasDisconnected = WasJustDisconnected.exchange(false); + bool isConnected = Connection->IsOpen(); + + if (isConnected) { + // if we are connected, disconnect cb first + if (wasDisconnected && Handlers.disconnected) { + Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage); + } } if (WasJustConnected.exchange(false) && Handlers.ready) { Handlers.ready(); } + if (GotErrorMessage.exchange(false) && Handlers.errored) { + Handlers.errored(LastErrorCode, LastErrorMessage); + } + if (WasJoinGame.exchange(false) && Handlers.joinGame) { Handlers.joinGame(JoinGameSecret); } @@ -293,4 +303,11 @@ extern "C" void Discord_RunCallbacks() if (WasSpectateGame.exchange(false) && Handlers.spectateGame) { Handlers.spectateGame(SpectateGameSecret); } + + if (!isConnected) { + // if we are not connected, disconnect message last + if (wasDisconnected && Handlers.disconnected) { + Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage); + } + } }