Make io thread optional
This commit is contained in:
@ -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)
|
||||
|
@ -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<int64_t, std::milli> 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);
|
||||
|
Reference in New Issue
Block a user