Start using rapidjson
This commit is contained in:
parent
1b65e53da7
commit
930cd722d5
10 changed files with 45 additions and 42 deletions
|
@ -19,8 +19,11 @@ if (NOT RAPIDJSON)
|
|||
file(REMOVE ${RJ_TAR_FILE})
|
||||
endif(NOT RAPIDJSON)
|
||||
|
||||
add_library(rapidjson STATIC IMPORTED ${RAPIDJSON})
|
||||
|
||||
add_subdirectory(src)
|
||||
add_subdirectory(examples/simple)
|
||||
add_subdirectory(examples/simpleSync)
|
||||
add_subdirectory(examples/simplest)
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
add_executable(simple-async-client simple.c)
|
||||
target_link_libraries(simple-async-client discord-rpc)
|
||||
add_executable(simple-client-async simple.c)
|
||||
target_link_libraries(simple-client-async discord-rpc)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
add_executable(simple-client simpleSync.c)
|
||||
target_link_libraries(simple-client discord-rpc-sync)
|
||||
add_executable(simple-client-sync simpleSync.c)
|
||||
target_link_libraries(simple-client-sync discord-rpc-sync)
|
||||
|
|
|
@ -28,7 +28,7 @@ static void handleDiscordReady() {
|
|||
printf("\nDiscord: ready\n");
|
||||
}
|
||||
|
||||
static void handleDiscordDisconnected() {
|
||||
static void handleDiscordDisconnected(int errcode, const char* msg) {
|
||||
printf("\nDiscord: disconnected\n");
|
||||
}
|
||||
|
||||
|
|
|
@ -6,5 +6,9 @@ set(BASE_RPC_SRC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc.cpp yol
|
|||
|
||||
if(WIN32)
|
||||
add_library(discord-rpc-sync STATIC ${BASE_RPC_SRC} connection_win_sync.cpp)
|
||||
target_include_directories(discord-rpc-sync PRIVATE ${RAPIDJSON}/include)
|
||||
|
||||
add_library(discord-rpc STATIC ${BASE_RPC_SRC} connection_win.cpp)
|
||||
target_include_directories(discord-rpc PRIVATE ${RAPIDJSON}/include)
|
||||
endif(WIN32)
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ struct RpcMessageFrame {
|
|||
|
||||
struct RpcConnection {
|
||||
void (*onConnect)() = nullptr;
|
||||
void (*onDisconnect)(int errorcode, const char* message) = nullptr;
|
||||
void (*onDisconnect)() = nullptr;
|
||||
char appId[64];
|
||||
|
||||
static RpcConnection* Create(const char* applicationId);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "connection.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include "rapidjson/document.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMCX
|
||||
|
@ -12,23 +13,11 @@
|
|||
|
||||
const int RpcVersion = 1;
|
||||
const int NumFrames = 3;
|
||||
static int LastErrorCode = 0;
|
||||
static const char* LastErrorMessage = "";
|
||||
|
||||
struct WinRpcConnection : public RpcConnection {
|
||||
HANDLE pipe{INVALID_HANDLE_VALUE};
|
||||
RpcMessageFrame frames[NumFrames];
|
||||
int nextFrame{0};
|
||||
int lastErrorCode{0};
|
||||
char lastErrorMessage[1024];
|
||||
|
||||
void HandleError(RpcMessageFrame* frame) {
|
||||
if (frame->opcode == OPCODE::CLOSE) {
|
||||
lastErrorCode = 1; // todo
|
||||
StringCopy(lastErrorMessage, frame->message, sizeof(lastErrorMessage));
|
||||
printf("got a close message: %d: %s\n", lastErrorCode, lastErrorMessage);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc";
|
||||
|
@ -85,9 +74,7 @@ void RpcConnection::Close()
|
|||
::CloseHandle(self->pipe);
|
||||
self->pipe = INVALID_HANDLE_VALUE;
|
||||
if (self->onDisconnect) {
|
||||
self->onDisconnect(LastErrorCode, LastErrorMessage);
|
||||
LastErrorCode = 0;
|
||||
LastErrorMessage = "";
|
||||
self->onDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,8 +93,6 @@ void RpcConnection::Write(const void* data, size_t length)
|
|||
if (success) {
|
||||
break;
|
||||
}
|
||||
LastErrorCode = -1;
|
||||
LastErrorMessage = "Pipe closed";
|
||||
self->Close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,16 +18,6 @@ struct WinRpcConnection : public RpcConnection {
|
|||
RpcMessageFrame readFrame;
|
||||
RpcMessageFrame frames[NumFrames];
|
||||
int nextFrame{0};
|
||||
int lastErrorCode{0};
|
||||
char lastErrorMessage[1024];
|
||||
|
||||
void HandleError(RpcMessageFrame* frame) {
|
||||
if (frame->opcode == OPCODE::CLOSE) {
|
||||
lastErrorCode = 1; // todo
|
||||
StringCopy(lastErrorMessage, frame->message, sizeof(lastErrorMessage));
|
||||
printf("got a close message: %d: %s\n", lastErrorCode, lastErrorMessage);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc";
|
||||
|
@ -84,9 +74,7 @@ void RpcConnection::Close()
|
|||
::CloseHandle(self->pipe);
|
||||
self->pipe = INVALID_HANDLE_VALUE;
|
||||
if (self->onDisconnect) {
|
||||
self->onDisconnect(self->lastErrorCode, self->lastErrorMessage);
|
||||
self->lastErrorCode = 0;
|
||||
self->lastErrorMessage[0] = 0;
|
||||
self->onDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,10 +94,12 @@ void RpcConnection::Write(const void* data, size_t length)
|
|||
break;
|
||||
}
|
||||
|
||||
/* hmm
|
||||
RpcMessageFrame* frame = self->Read();
|
||||
if (frame) {
|
||||
self->HandleError(frame);
|
||||
}
|
||||
*/
|
||||
|
||||
self->Close();
|
||||
}
|
||||
|
|
|
@ -322,3 +322,7 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
|
|||
Frame.length = jsonWrite - Frame.message;
|
||||
ConnectionWrite(&Frame);
|
||||
}
|
||||
|
||||
extern "C" void Discord_Update()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "connection.h"
|
||||
#include "yolojson.h"
|
||||
#include "rapidjson/document.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -11,7 +12,7 @@ static DiscordEventHandlers Handlers{};
|
|||
static bool WasJustConnected = false;
|
||||
static bool WasJustDisconnected = false;
|
||||
static int LastErrorCode = 0;
|
||||
static const char* LastErrorMessage = "";
|
||||
static char LastErrorMessage[256];
|
||||
|
||||
extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
|
||||
{
|
||||
|
@ -24,17 +25,15 @@ extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandle
|
|||
|
||||
MyConnection = RpcConnection::Create(applicationId);
|
||||
MyConnection->onConnect = []() { WasJustConnected = true; };
|
||||
MyConnection->onDisconnect = [](int errorCode, const char* message) {
|
||||
LastErrorCode = errorCode;
|
||||
LastErrorMessage = message;
|
||||
WasJustDisconnected = true;
|
||||
};
|
||||
MyConnection->onDisconnect = []() { WasJustDisconnected = true; };
|
||||
MyConnection->Open();
|
||||
}
|
||||
|
||||
extern "C" void Discord_Shutdown()
|
||||
{
|
||||
Handlers = {};
|
||||
MyConnection->onConnect = nullptr;
|
||||
MyConnection->onDisconnect = nullptr;
|
||||
MyConnection->Close();
|
||||
RpcConnection::Destroy(MyConnection);
|
||||
}
|
||||
|
@ -53,6 +52,24 @@ extern "C" void Discord_Update()
|
|||
{
|
||||
while (auto frame = MyConnection->Read()) {
|
||||
printf("got a message %d, %d, %s\n", frame->opcode, frame->length, frame->message);
|
||||
rapidjson::Document d;
|
||||
if (frame->length > 0) {
|
||||
d.ParseInsitu(frame->message);
|
||||
}
|
||||
|
||||
switch (frame->opcode) {
|
||||
case OPCODE::HANDSHAKE:
|
||||
// does this happen?
|
||||
break;
|
||||
case OPCODE::CLOSE:
|
||||
LastErrorCode = d["code"].GetInt();
|
||||
StringCopy(LastErrorMessage, d["code"].GetString(), sizeof(LastErrorMessage));
|
||||
MyConnection->Close();
|
||||
break;
|
||||
case OPCODE::FRAME:
|
||||
// todo
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// fire callbacks
|
||||
|
|
Loading…
Reference in a new issue