discord-rpc/src/discord-rpc.cpp

115 lines
2.8 KiB
C++
Raw Normal View History

2017-06-30 23:18:54 +00:00
#include "discord-rpc.h"
2017-07-17 16:28:54 +00:00
#include "rpc_connection.h"
2017-06-30 23:18:54 +00:00
#include "yolojson.h"
2017-07-13 15:32:08 +00:00
#include "rapidjson/document.h"
2017-06-30 23:18:54 +00:00
#include <stdio.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <thread>
static RpcConnection* Connection{nullptr};
2017-06-30 23:18:54 +00:00
static char ApplicationId[64]{};
static DiscordEventHandlers Handlers{};
static std::atomic_bool WasJustConnected{false};
static std::atomic_bool WasJustDisconnected{false};
static int LastErrorCode = 0;
2017-07-13 15:32:08 +00:00
static char LastErrorMessage[256];
static std::atomic_bool KeepRunning{true};
static std::mutex WaitForIOMutex;
static std::condition_variable WaitForIOActivity;
static std::thread IoThread;
void Discord_UpdateConnection()
{
if (!Connection->IsOpen()) {
Connection->Open();
}
else {
// reads
rapidjson::Document message;
while (Connection->Read(message)) {
// todo: do something...
printf("Hey, I got a message\n");
}
}
}
void DiscordRpcIo()
{
printf("Discord io thread start\n");
const std::chrono::duration<int64_t, std::milli> maxWait{500LL};
while (KeepRunning.load()) {
Discord_UpdateConnection();
std::unique_lock<std::mutex> lock(WaitForIOMutex);
WaitForIOActivity.wait_for(lock, maxWait);
}
Connection->Close();
printf("Discord io thread stop\n");
}
void SignalIOActivity()
{
WaitForIOActivity.notify_all();
}
2017-06-30 23:18:54 +00:00
2017-07-07 21:00:29 +00:00
extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
2017-06-30 23:18:54 +00:00
{
if (handlers) {
Handlers = *handlers;
}
else {
Handlers = {};
}
Connection = RpcConnection::Create(applicationId);
Connection->onConnect = []() {
WasJustConnected.exchange(true);
};
Connection->onDisconnect = [](int err, const char* message) {
2017-07-17 16:28:54 +00:00
LastErrorCode = err;
StringCopy(LastErrorMessage, message, sizeof(LastErrorMessage));
WasJustDisconnected.exchange(true);
2017-07-17 16:28:54 +00:00
};
IoThread = std::thread(DiscordRpcIo);
2017-06-30 23:18:54 +00:00
}
2017-07-07 21:00:29 +00:00
extern "C" void Discord_Shutdown()
2017-06-30 23:18:54 +00:00
{
Connection->onConnect = nullptr;
Connection->onDisconnect = nullptr;
2017-06-30 23:18:54 +00:00
Handlers = {};
KeepRunning.exchange(false);
SignalIOActivity();
if (IoThread.joinable()) {
IoThread.join();
}
RpcConnection::Destroy(Connection);
2017-06-30 23:18:54 +00:00
}
2017-07-07 21:00:29 +00:00
extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
2017-06-30 23:18:54 +00:00
{
char jsonBuffer[16 * 1024];
char* jsonWrite = jsonBuffer;
2017-06-30 23:18:54 +00:00
JsonWriteRichPresenceObj(jsonWrite, presence);
size_t length = jsonWrite - jsonBuffer;
Connection->Write(jsonBuffer, length);
SignalIOActivity();
2017-06-30 23:18:54 +00:00
}
2017-07-07 16:41:20 +00:00
2017-07-07 21:00:29 +00:00
extern "C" void Discord_Update()
2017-07-07 16:41:20 +00:00
{
if (WasJustDisconnected.exchange(false) && Handlers.disconnected) {
Handlers.disconnected(LastErrorCode, LastErrorMessage);
2017-07-07 16:41:20 +00:00
}
if (WasJustConnected.exchange(false) && Handlers.ready) {
2017-07-07 16:41:20 +00:00
Handlers.ready();
}
}