Start using rapidjson

This commit is contained in:
Chris Marsh 2017-07-13 08:32:08 -07:00
parent 1b65e53da7
commit 930cd722d5
10 changed files with 45 additions and 42 deletions

View file

@ -19,8 +19,11 @@ if (NOT RAPIDJSON)
file(REMOVE ${RJ_TAR_FILE}) file(REMOVE ${RJ_TAR_FILE})
endif(NOT RAPIDJSON) endif(NOT RAPIDJSON)
add_library(rapidjson STATIC IMPORTED ${RAPIDJSON})
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(examples/simple) add_subdirectory(examples/simple)
add_subdirectory(examples/simpleSync) add_subdirectory(examples/simpleSync)
add_subdirectory(examples/simplest) add_subdirectory(examples/simplest)

View file

@ -1,3 +1,3 @@
include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(simple-async-client simple.c) add_executable(simple-client-async simple.c)
target_link_libraries(simple-async-client discord-rpc) target_link_libraries(simple-client-async discord-rpc)

View file

@ -1,3 +1,3 @@
include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(simple-client simpleSync.c) add_executable(simple-client-sync simpleSync.c)
target_link_libraries(simple-client discord-rpc-sync) target_link_libraries(simple-client-sync discord-rpc-sync)

View file

@ -28,7 +28,7 @@ static void handleDiscordReady() {
printf("\nDiscord: ready\n"); printf("\nDiscord: ready\n");
} }
static void handleDiscordDisconnected() { static void handleDiscordDisconnected(int errcode, const char* msg) {
printf("\nDiscord: disconnected\n"); printf("\nDiscord: disconnected\n");
} }

View file

@ -6,5 +6,9 @@ set(BASE_RPC_SRC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc.cpp yol
if(WIN32) if(WIN32)
add_library(discord-rpc-sync STATIC ${BASE_RPC_SRC} connection_win_sync.cpp) 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) add_library(discord-rpc STATIC ${BASE_RPC_SRC} connection_win.cpp)
target_include_directories(discord-rpc PRIVATE ${RAPIDJSON}/include)
endif(WIN32) endif(WIN32)

View file

@ -18,7 +18,7 @@ struct RpcMessageFrame {
struct RpcConnection { struct RpcConnection {
void (*onConnect)() = nullptr; void (*onConnect)() = nullptr;
void (*onDisconnect)(int errorcode, const char* message) = nullptr; void (*onDisconnect)() = nullptr;
char appId[64]; char appId[64];
static RpcConnection* Create(const char* applicationId); static RpcConnection* Create(const char* applicationId);

View file

@ -1,6 +1,7 @@
#include "connection.h" #include "connection.h"
#include <stdio.h> #include <stdio.h>
#include "rapidjson/document.h"
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#define NOMCX #define NOMCX
@ -12,23 +13,11 @@
const int RpcVersion = 1; const int RpcVersion = 1;
const int NumFrames = 3; const int NumFrames = 3;
static int LastErrorCode = 0;
static const char* LastErrorMessage = "";
struct WinRpcConnection : public RpcConnection { struct WinRpcConnection : public RpcConnection {
HANDLE pipe{INVALID_HANDLE_VALUE}; HANDLE pipe{INVALID_HANDLE_VALUE};
RpcMessageFrame frames[NumFrames]; RpcMessageFrame frames[NumFrames];
int nextFrame{0}; 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"; static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc";
@ -85,9 +74,7 @@ void RpcConnection::Close()
::CloseHandle(self->pipe); ::CloseHandle(self->pipe);
self->pipe = INVALID_HANDLE_VALUE; self->pipe = INVALID_HANDLE_VALUE;
if (self->onDisconnect) { if (self->onDisconnect) {
self->onDisconnect(LastErrorCode, LastErrorMessage); self->onDisconnect();
LastErrorCode = 0;
LastErrorMessage = "";
} }
} }
@ -106,8 +93,6 @@ void RpcConnection::Write(const void* data, size_t length)
if (success) { if (success) {
break; break;
} }
LastErrorCode = -1;
LastErrorMessage = "Pipe closed";
self->Close(); self->Close();
} }
} }

View file

@ -18,16 +18,6 @@ struct WinRpcConnection : public RpcConnection {
RpcMessageFrame readFrame; RpcMessageFrame readFrame;
RpcMessageFrame frames[NumFrames]; RpcMessageFrame frames[NumFrames];
int nextFrame{0}; 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"; static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc";
@ -84,9 +74,7 @@ void RpcConnection::Close()
::CloseHandle(self->pipe); ::CloseHandle(self->pipe);
self->pipe = INVALID_HANDLE_VALUE; self->pipe = INVALID_HANDLE_VALUE;
if (self->onDisconnect) { if (self->onDisconnect) {
self->onDisconnect(self->lastErrorCode, self->lastErrorMessage); self->onDisconnect();
self->lastErrorCode = 0;
self->lastErrorMessage[0] = 0;
} }
} }
@ -106,10 +94,12 @@ void RpcConnection::Write(const void* data, size_t length)
break; break;
} }
/* hmm
RpcMessageFrame* frame = self->Read(); RpcMessageFrame* frame = self->Read();
if (frame) { if (frame) {
self->HandleError(frame); self->HandleError(frame);
} }
*/
self->Close(); self->Close();
} }

View file

@ -322,3 +322,7 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
Frame.length = jsonWrite - Frame.message; Frame.length = jsonWrite - Frame.message;
ConnectionWrite(&Frame); ConnectionWrite(&Frame);
} }
extern "C" void Discord_Update()
{
}

View file

@ -2,6 +2,7 @@
#include "connection.h" #include "connection.h"
#include "yolojson.h" #include "yolojson.h"
#include "rapidjson/document.h"
#include <stdio.h> #include <stdio.h>
@ -11,7 +12,7 @@ static DiscordEventHandlers Handlers{};
static bool WasJustConnected = false; static bool WasJustConnected = false;
static bool WasJustDisconnected = false; static bool WasJustDisconnected = false;
static int LastErrorCode = 0; static int LastErrorCode = 0;
static const char* LastErrorMessage = ""; static char LastErrorMessage[256];
extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) 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 = RpcConnection::Create(applicationId);
MyConnection->onConnect = []() { WasJustConnected = true; }; MyConnection->onConnect = []() { WasJustConnected = true; };
MyConnection->onDisconnect = [](int errorCode, const char* message) { MyConnection->onDisconnect = []() { WasJustDisconnected = true; };
LastErrorCode = errorCode;
LastErrorMessage = message;
WasJustDisconnected = true;
};
MyConnection->Open(); MyConnection->Open();
} }
extern "C" void Discord_Shutdown() extern "C" void Discord_Shutdown()
{ {
Handlers = {}; Handlers = {};
MyConnection->onConnect = nullptr;
MyConnection->onDisconnect = nullptr;
MyConnection->Close(); MyConnection->Close();
RpcConnection::Destroy(MyConnection); RpcConnection::Destroy(MyConnection);
} }
@ -53,6 +52,24 @@ extern "C" void Discord_Update()
{ {
while (auto frame = MyConnection->Read()) { while (auto frame = MyConnection->Read()) {
printf("got a message %d, %d, %s\n", frame->opcode, frame->length, frame->message); 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 // fire callbacks