Error message cb

This commit is contained in:
Chris Marsh 2017-07-24 14:58:53 -07:00
parent 88ab85c81e
commit 63058ddf23
3 changed files with 16 additions and 3 deletions

View file

@ -32,6 +32,10 @@ static void handleDiscordDisconnected(int errcode, const char* message) {
printf("\nDiscord: disconnected (%d: %s)\n", errcode, 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() { static void handleDiscordPresenceRequested() {
printf("\nDiscord: requests presence\n"); printf("\nDiscord: requests presence\n");
updateDiscordPresence(); updateDiscordPresence();
@ -83,6 +87,7 @@ int main() {
memset(&handlers, 0, sizeof(handlers)); memset(&handlers, 0, sizeof(handlers));
handlers.ready = handleDiscordReady; handlers.ready = handleDiscordReady;
handlers.disconnected = handleDiscordDisconnected; handlers.disconnected = handleDiscordDisconnected;
handlers.errored = handleDiscordError;
handlers.presenceRequested = handleDiscordPresenceRequested; handlers.presenceRequested = handleDiscordPresenceRequested;
Discord_Initialize(APPLICATION_ID, &handlers); Discord_Initialize(APPLICATION_ID, &handlers);

View file

@ -28,6 +28,7 @@ typedef struct DiscordRichPresence {
typedef struct DiscordEventHandlers { typedef struct DiscordEventHandlers {
void (*ready)(); void (*ready)();
void (*disconnected)(int errorCode, const char* message); void (*disconnected)(int errorCode, const char* message);
void (*errored)(int errorCode, const char* message);
void (*presenceRequested)(); void (*presenceRequested)();
void (*joinGame)(const char* joinSecret); void (*joinGame)(const char* joinSecret);
void (*spectateGame)(const char* spectateSecret); void (*spectateGame)(const char* spectateSecret);

View file

@ -25,6 +25,7 @@ static char ApplicationId[64]{};
static DiscordEventHandlers Handlers{}; static DiscordEventHandlers Handlers{};
static std::atomic_bool WasJustConnected{false}; static std::atomic_bool WasJustConnected{false};
static std::atomic_bool WasJustDisconnected{false}; static std::atomic_bool WasJustDisconnected{false};
static std::atomic_bool GotErrorMessage{false};
static std::atomic_bool WasPresenceRequested{false}; static std::atomic_bool WasPresenceRequested{false};
static std::atomic_bool WasJoinGame{false}; static std::atomic_bool WasJoinGame{false};
static std::atomic_bool WasSpectateGame{false}; static std::atomic_bool WasSpectateGame{false};
@ -32,6 +33,8 @@ static char JoinGameSecret[256];
static char SpectateGameSecret[256]; static char SpectateGameSecret[256];
static int LastErrorCode{0}; static int LastErrorCode{0};
static char LastErrorMessage[256]; static char LastErrorMessage[256];
static int LastDisconnectErrorCode{0};
static char LastDisconnectErrorMessage[256];
static QueuedMessage SendQueue[MessageQueueSize]{}; static QueuedMessage SendQueue[MessageQueueSize]{};
static std::atomic_uint SendQueueNextAdd{0}; static std::atomic_uint SendQueueNextAdd{0};
static std::atomic_uint SendQueueNextSend{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) { Connection->onDisconnect = [](int err, const char* message) {
LastErrorCode = err; LastDisconnectErrorCode = err;
StringCopy(LastErrorMessage, message); StringCopy(LastDisconnectErrorMessage, message);
WasJustDisconnected.exchange(true); WasJustDisconnected.exchange(true);
UpdateReconnectTime(); UpdateReconnectTime();
}; };
@ -221,8 +224,12 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
extern "C" void Discord_RunCallbacks() extern "C" void Discord_RunCallbacks()
{ {
if (GotErrorMessage.exchange(false) && Handlers.errored) {
Handlers.errored(LastErrorCode, LastErrorMessage);
}
if (WasJustDisconnected.exchange(false) && Handlers.disconnected) { if (WasJustDisconnected.exchange(false) && Handlers.disconnected) {
Handlers.disconnected(LastErrorCode, LastErrorMessage); Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage);
} }
if (WasJustConnected.exchange(false) && Handlers.ready) { if (WasJustConnected.exchange(false) && Handlers.ready) {