diff --git a/examples/simple/simple.c b/examples/simple/simple.c index c4be6df..edd883d 100644 --- a/examples/simple/simple.c +++ b/examples/simple/simple.c @@ -70,7 +70,11 @@ static void gameLoop() { ++FrustrationLevel; updateDiscordPresence(); - Discord_Update(); + +#ifdef DISCORD_DISABLE_IO_THREAD + Discord_UpdateConnection(); +#endif + Discord_RunCallbacks(); } } diff --git a/include/discord-rpc.h b/include/discord-rpc.h index 4f6e900..0a19cef 100644 --- a/include/discord-rpc.h +++ b/include/discord-rpc.h @@ -1,6 +1,8 @@ #pragma once #include +//#define DISCORD_DISABLE_IO_THREAD + #ifdef __cplusplus extern "C" { #endif @@ -36,7 +38,12 @@ void Discord_Shutdown(); void Discord_UpdatePresence(const DiscordRichPresence* presence); /* checks for incoming messages, dispatches callbacks */ -void Discord_Update(); +void Discord_RunCallbacks(); + +/* If you disable the lib starting its own io thread, you'll need to call this from your own */ +#ifdef DISCORD_DISABLE_IO_THREAD +void Discord_UpdateConnection(); +#endif #ifdef __cplusplus } /* extern "C" */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 888000e..54aadba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ include_directories(${PROJECT_SOURCE_DIR}/include) -set(BASE_RPC_SRC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc.cpp rpc_connection.h rpc_connection.cpp yolojson.h connection.h) +set(BASE_RPC_SRC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc.cpp rpc_connection.h rpc_connection.cpp yolojson.h connection.h backoff.h) if(WIN32) add_library(discord-rpc-simple STATIC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc-simple.cpp) diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index b8b290e..a766c2f 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -25,15 +25,18 @@ static std::atomic_bool WasJustConnected{false}; static std::atomic_bool WasJustDisconnected{false}; static int LastErrorCode{0}; static char LastErrorMessage[256]; -static std::atomic_bool KeepRunning{true}; -static std::mutex WaitForIOMutex; -static std::condition_variable WaitForIOActivity; -static std::thread IoThread; static QueuedMessage SendQueue[MessageQueueSize]{}; static std::atomic_uint SendQueueNextAdd{0}; static std::atomic_uint SendQueueNextSend{0}; static std::atomic_uint SendQueuePendingSends{0}; +#ifndef DISCORD_DISABLE_IO_THREAD +static std::atomic_bool KeepRunning{ true }; +static std::mutex WaitForIOMutex; +static std::condition_variable WaitForIOActivity; +static std::thread IoThread; +#endif // DISCORD_DISABLE_IO_THREAD + static QueuedMessage* SendQueueGetNextAddMessage() { // if we are falling behind, bail if (SendQueuePendingSends.load() >= MessageQueueSize) { @@ -50,7 +53,7 @@ static void SendQueueCommitMessage() { SendQueuePendingSends++; } -void Discord_UpdateConnection() +extern "C" void Discord_UpdateConnection() { if (!Connection->IsOpen()) { Connection->Open(); @@ -72,6 +75,7 @@ void Discord_UpdateConnection() } } +#ifndef DISCORD_DISABLE_IO_THREAD void DiscordRpcIo() { const std::chrono::duration maxWait{500LL}; @@ -83,10 +87,13 @@ void DiscordRpcIo() WaitForIOActivity.wait_for(lock, maxWait); } } +#endif void SignalIOActivity() { +#ifndef DISCORD_DISABLE_IO_THREAD WaitForIOActivity.notify_all(); +#endif } extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) @@ -108,7 +115,9 @@ extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandle WasJustDisconnected.exchange(true); }; +#ifndef DISCORD_DISABLE_IO_THREAD IoThread = std::thread(DiscordRpcIo); +#endif } extern "C" void Discord_Shutdown() @@ -116,11 +125,13 @@ extern "C" void Discord_Shutdown() Connection->onConnect = nullptr; Connection->onDisconnect = nullptr; Handlers = {}; +#ifndef DISCORD_DISABLE_IO_THREAD KeepRunning.exchange(false); SignalIOActivity(); if (IoThread.joinable()) { IoThread.join(); } +#endif RpcConnection::Destroy(Connection); } @@ -136,7 +147,7 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) } } -extern "C" void Discord_Update() +extern "C" void Discord_RunCallbacks() { if (WasJustDisconnected.exchange(false) && Handlers.disconnected) { Handlers.disconnected(LastErrorCode, LastErrorMessage);