discord_rpc: Use anonymous namespace

This commit is contained in:
Jonas Kvinge
2025-04-13 21:34:40 +02:00
parent 3746915ae7
commit c5e38b71f7
4 changed files with 47 additions and 48 deletions

View File

@@ -34,7 +34,11 @@
#include "discord_rpc_connection.h"
#include "discord_serialization.h"
namespace discord_rpc {
using namespace discord_rpc;
static void Discord_UpdateConnection();
namespace {
constexpr size_t MaxMessageSize { 16 * 1024 };
constexpr size_t MessageQueueSize { 8 };
@@ -94,7 +98,6 @@ static auto NextConnect = std::chrono::system_clock::now();
static int Pid { 0 };
static int Nonce { 1 };
static void Discord_UpdateConnection(void);
class IoThreadHolder {
private:
std::atomic_bool keepRunning { true };
@@ -128,6 +131,7 @@ class IoThreadHolder {
~IoThreadHolder() { Stop(); }
};
static IoThreadHolder *IoThread { nullptr };
static void UpdateReconnectTime() {
@@ -136,6 +140,44 @@ static void UpdateReconnectTime() {
}
static void SignalIOActivity() {
if (IoThread != nullptr) {
IoThread->Notify();
}
}
static bool RegisterForEvent(const char *evtName) {
auto qmessage = SendQueue.GetNextAddMessage();
if (qmessage) {
qmessage->length = JsonWriteSubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName);
SendQueue.CommitAdd();
SignalIOActivity();
return true;
}
return false;
}
static bool DeregisterForEvent(const char *evtName) {
auto qmessage = SendQueue.GetNextAddMessage();
if (qmessage) {
qmessage->length = JsonWriteUnsubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName);
SendQueue.CommitAdd();
SignalIOActivity();
return true;
}
return false;
}
} // namespace
static void Discord_UpdateConnection() {
if (!Connection) {
@@ -242,42 +284,6 @@ static void Discord_UpdateConnection() {
}
static void SignalIOActivity() {
if (IoThread != nullptr) {
IoThread->Notify();
}
}
static bool RegisterForEvent(const char *evtName) {
auto qmessage = SendQueue.GetNextAddMessage();
if (qmessage) {
qmessage->length = JsonWriteSubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName);
SendQueue.CommitAdd();
SignalIOActivity();
return true;
}
return false;
}
static bool DeregisterForEvent(const char *evtName) {
auto qmessage = SendQueue.GetNextAddMessage();
if (qmessage) {
qmessage->length = JsonWriteUnsubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName);
SendQueue.CommitAdd();
SignalIOActivity();
return true;
}
return false;
}
extern "C" void Discord_Initialize(const char *applicationId, DiscordEventHandlers *handlers, const int autoRegister) {
IoThread = new (std::nothrow) IoThreadHolder();
@@ -348,7 +354,7 @@ extern "C" void Discord_Initialize(const char *applicationId, DiscordEventHandle
}
extern "C" void Discord_Shutdown(void) {
extern "C" void Discord_Shutdown() {
if (!Connection) {
return;
@@ -502,5 +508,3 @@ extern "C" void Discord_UpdateHandlers(DiscordEventHandlers *newHandlers) {
}
}
} // namespace discord_rpc

View File

@@ -26,8 +26,6 @@
#include <cstdint>
namespace discord_rpc {
#ifdef __cplusplus
extern "C" {
#endif
@@ -92,6 +90,4 @@ void Discord_UpdateHandlers(DiscordEventHandlers *handlers);
} /* extern "C" */
#endif
} // namespace discord_rpc
#endif // DISCORD_RPC_H

View File

@@ -28,6 +28,8 @@
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
struct DiscordRichPresence;
namespace discord_rpc {
// if only there was a standard library function for this
@@ -48,7 +50,6 @@ inline size_t StringCopy(char (&dest)[Len], const char *src) {
size_t JsonWriteHandshakeObj(char *dest, size_t maxLen, int version, const char *applicationId);
// Commands
struct DiscordRichPresence;
size_t JsonWriteRichPresenceObj(char *dest, const size_t maxLen, const int nonce, const int pid, const DiscordRichPresence *presence);
size_t JsonWriteSubscribeCommand(char *dest, size_t maxLen, int nonce, const char *evtName);