From 63058ddf230f7bc788ef6aceb037952a542f3772 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Mon, 24 Jul 2017 14:58:53 -0700 Subject: [PATCH] Error message cb --- examples/send-presence/send-presence.c | 5 +++++ include/discord-rpc.h | 1 + src/discord-rpc.cpp | 13 ++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/send-presence/send-presence.c b/examples/send-presence/send-presence.c index b4d36c0..0d1cad7 100644 --- a/examples/send-presence/send-presence.c +++ b/examples/send-presence/send-presence.c @@ -32,6 +32,10 @@ static void handleDiscordDisconnected(int errcode, const char* message) { printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); } +static void handleDiscordError(int errcode, const char* message) { + printf("\nDiscord: error (%d: %s)\n", errcode, message); +} + static void handleDiscordPresenceRequested() { printf("\nDiscord: requests presence\n"); updateDiscordPresence(); @@ -83,6 +87,7 @@ int main() { memset(&handlers, 0, sizeof(handlers)); handlers.ready = handleDiscordReady; handlers.disconnected = handleDiscordDisconnected; + handlers.errored = handleDiscordError; handlers.presenceRequested = handleDiscordPresenceRequested; Discord_Initialize(APPLICATION_ID, &handlers); diff --git a/include/discord-rpc.h b/include/discord-rpc.h index 6166811..e61d1f6 100644 --- a/include/discord-rpc.h +++ b/include/discord-rpc.h @@ -28,6 +28,7 @@ typedef struct DiscordRichPresence { typedef struct DiscordEventHandlers { void (*ready)(); void (*disconnected)(int errorCode, const char* message); + void (*errored)(int errorCode, const char* message); void (*presenceRequested)(); void (*joinGame)(const char* joinSecret); void (*spectateGame)(const char* spectateSecret); diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index faa189b..be6f3f3 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -25,6 +25,7 @@ static char ApplicationId[64]{}; static DiscordEventHandlers Handlers{}; static std::atomic_bool WasJustConnected{false}; static std::atomic_bool WasJustDisconnected{false}; +static std::atomic_bool GotErrorMessage{false}; static std::atomic_bool WasPresenceRequested{false}; static std::atomic_bool WasJoinGame{false}; static std::atomic_bool WasSpectateGame{false}; @@ -32,6 +33,8 @@ static char JoinGameSecret[256]; static char SpectateGameSecret[256]; static int LastErrorCode{0}; static char LastErrorMessage[256]; +static int LastDisconnectErrorCode{0}; +static char LastDisconnectErrorMessage[256]; static QueuedMessage SendQueue[MessageQueueSize]{}; static std::atomic_uint SendQueueNextAdd{0}; static std::atomic_uint SendQueueNextSend{0}; @@ -183,8 +186,8 @@ extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandle } }; Connection->onDisconnect = [](int err, const char* message) { - LastErrorCode = err; - StringCopy(LastErrorMessage, message); + LastDisconnectErrorCode = err; + StringCopy(LastDisconnectErrorMessage, message); WasJustDisconnected.exchange(true); UpdateReconnectTime(); }; @@ -221,8 +224,12 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) extern "C" void Discord_RunCallbacks() { + if (GotErrorMessage.exchange(false) && Handlers.errored) { + Handlers.errored(LastErrorCode, LastErrorMessage); + } + if (WasJustDisconnected.exchange(false) && Handlers.disconnected) { - Handlers.disconnected(LastErrorCode, LastErrorMessage); + Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage); } if (WasJustConnected.exchange(false) && Handlers.ready) {