Make io thread optional

This commit is contained in:
Chris Marsh 2017-07-18 09:47:33 -07:00
parent 6168a36201
commit 0d0485444d
4 changed files with 31 additions and 9 deletions

View file

@ -70,7 +70,11 @@ static void gameLoop() {
++FrustrationLevel;
updateDiscordPresence();
Discord_Update();
#ifdef DISCORD_DISABLE_IO_THREAD
Discord_UpdateConnection();
#endif
Discord_RunCallbacks();
}
}

View file

@ -1,6 +1,8 @@
#pragma once
#include <stdint.h>
//#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" */

View file

@ -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)

View file

@ -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);