From 647bdc5cbdde93e499f4f2b63a7b8b99e250e203 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Wed, 20 May 2020 16:10:46 +0200 Subject: [PATCH] (Discord RPC) Cleanups --- deps/discord-rpc/src/discord_rpc.cpp | 101 +++++++++++++------------- deps/discord-rpc/src/rpc_connection.h | 4 +- 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/deps/discord-rpc/src/discord_rpc.cpp b/deps/discord-rpc/src/discord_rpc.cpp index 41a9161db4..f3f9e92c5f 100644 --- a/deps/discord-rpc/src/discord_rpc.cpp +++ b/deps/discord-rpc/src/discord_rpc.cpp @@ -26,14 +26,10 @@ void Discord_RegisterSteamGame(const char *a, const char *b); } #endif -constexpr size_t MaxMessageSize{16 * 1024}; -constexpr size_t MessageQueueSize{8}; -constexpr size_t JoinQueueSize{8}; - struct QueuedMessage { size_t length; - char buffer[MaxMessageSize]; + char buffer[16384]; void Copy(const QueuedMessage& other) { @@ -61,25 +57,32 @@ struct User * from future changes in these sizes */ }; +static int Pid{0}; +static int Nonce{1}; +static int LastErrorCode{0}; +static int LastDisconnectErrorCode{0}; + +static char JoinGameSecret[256]; +static char SpectateGameSecret[256]; +static char LastErrorMessage[256]; +static char LastDisconnectErrorMessage[256]; + static RpcConnection* Connection{nullptr}; + static DiscordEventHandlers QueuedHandlers{}; 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 WasJoinGame{false}; static std::atomic_bool WasSpectateGame{false}; -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 std::mutex PresenceMutex; static std::mutex HandlerMutex; static QueuedMessage QueuedPresence{}; -static MsgQueue SendQueue; -static MsgQueue JoinAskQueue; +static MsgQueue SendQueue; +static MsgQueue JoinAskQueue; static User connectedUser; /* We want to auto connect, and retry on failure, @@ -87,57 +90,57 @@ static User connectedUser; * backoff from 0.5 seconds to 1 minute */ static Backoff ReconnectTimeMs(500, 60 * 1000); static auto NextConnect = std::chrono::system_clock::now(); -static int Pid{0}; -static int Nonce{1}; #ifndef DISCORD_DISABLE_IO_THREAD static void Discord_UpdateConnection(void); -class IoThreadHolder { -private: - std::atomic_bool keepRunning{true}; - std::mutex waitForIOMutex; - std::condition_variable waitForIOActivity; - std::thread ioThread; +class IoThreadHolder +{ + private: + std::atomic_bool keepRunning{true}; + std::mutex waitForIOMutex; + std::condition_variable waitForIOActivity; + std::thread ioThread; -public: - void Start() - { - keepRunning.store(true); - ioThread = std::thread([&]() { - const std::chrono::duration maxWait{500LL}; - Discord_UpdateConnection(); - while (keepRunning.load()) { - std::unique_lock lock(waitForIOMutex); - waitForIOActivity.wait_for(lock, maxWait); - Discord_UpdateConnection(); - } - }); - } + public: + void Start() + { + keepRunning.store(true); + ioThread = std::thread([&]() { + const std::chrono::duration maxWait{500LL}; + Discord_UpdateConnection(); + while (keepRunning.load()) { + std::unique_lock lock(waitForIOMutex); + waitForIOActivity.wait_for(lock, maxWait); + Discord_UpdateConnection(); + } + }); + } - void Notify() { waitForIOActivity.notify_all(); } + void Notify() { waitForIOActivity.notify_all(); } - void Stop() - { - keepRunning.exchange(false); - Notify(); - if (ioThread.joinable()) + void Stop() + { + keepRunning.exchange(false); + Notify(); + if (ioThread.joinable()) ioThread.join(); - } + } - ~IoThreadHolder() { Stop(); } + ~IoThreadHolder() { Stop(); } }; #else -class IoThreadHolder { -public: - void Start() {} - void Stop() {} - void Notify() {} +class IoThreadHolder +{ + public: + void Start() {} + void Stop() {} + void Notify() {} }; #endif /* DISCORD_DISABLE_IO_THREAD */ static IoThreadHolder* IoThread{nullptr}; -static void UpdateReconnectTime() +static void UpdateReconnectTime(void) { NextConnect = std::chrono::system_clock::now() + std::chrono::duration{ReconnectTimeMs.nextDelay()}; diff --git a/deps/discord-rpc/src/rpc_connection.h b/deps/discord-rpc/src/rpc_connection.h index a4ccb82638..6fb69c9ee0 100644 --- a/deps/discord-rpc/src/rpc_connection.h +++ b/deps/discord-rpc/src/rpc_connection.h @@ -5,7 +5,7 @@ /* I took this from the buffer size libuv uses for named pipes; * I suspect ours would usually be much smaller. */ -constexpr size_t MaxRpcFrameSize = 64 * 1024; +#define MAX_RPC_FRAMESIZE 65536 struct RpcConnection { @@ -33,7 +33,7 @@ struct RpcConnection struct MessageFrame : public MessageFrameHeader { - char message[MaxRpcFrameSize - sizeof(MessageFrameHeader)]; + char message[MAX_RPC_FRAMESIZE - sizeof(MessageFrameHeader)]; }; enum class State : uint32_t