From 930cd722d5d2864479f4c9d66d2ed983a3e20e8a Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Thu, 13 Jul 2017 08:32:08 -0700 Subject: [PATCH] Start using rapidjson --- CMakeLists.txt | 3 +++ examples/simple/CMakeLists.txt | 4 ++-- examples/simpleSync/CMakeLists.txt | 4 ++-- examples/simpleSync/simpleSync.c | 2 +- src/CMakeLists.txt | 4 ++++ src/connection.h | 2 +- src/connection_win.cpp | 19 ++----------------- src/connection_win_sync.cpp | 16 +++------------- src/discord-rpc-simple.cpp | 4 ++++ src/discord-rpc.cpp | 29 +++++++++++++++++++++++------ 10 files changed, 45 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 786e8a3..efa3d59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) + diff --git a/examples/simple/CMakeLists.txt b/examples/simple/CMakeLists.txt index d045889..6072b7f 100644 --- a/examples/simple/CMakeLists.txt +++ b/examples/simple/CMakeLists.txt @@ -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) diff --git a/examples/simpleSync/CMakeLists.txt b/examples/simpleSync/CMakeLists.txt index 3f0bb21..da023f5 100644 --- a/examples/simpleSync/CMakeLists.txt +++ b/examples/simpleSync/CMakeLists.txt @@ -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) diff --git a/examples/simpleSync/simpleSync.c b/examples/simpleSync/simpleSync.c index 05ef986..57619c6 100644 --- a/examples/simpleSync/simpleSync.c +++ b/examples/simpleSync/simpleSync.c @@ -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"); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a242eb5..5a3de97 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) + diff --git a/src/connection.h b/src/connection.h index f7814d3..5649e44 100644 --- a/src/connection.h +++ b/src/connection.h @@ -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); diff --git a/src/connection_win.cpp b/src/connection_win.cpp index d7160fc..22060fb 100644 --- a/src/connection_win.cpp +++ b/src/connection_win.cpp @@ -1,6 +1,7 @@ #include "connection.h" #include +#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(); } } diff --git a/src/connection_win_sync.cpp b/src/connection_win_sync.cpp index 490f7a2..13235fc 100644 --- a/src/connection_win_sync.cpp +++ b/src/connection_win_sync.cpp @@ -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(); } diff --git a/src/discord-rpc-simple.cpp b/src/discord-rpc-simple.cpp index dfce211..fee3bd1 100644 --- a/src/discord-rpc-simple.cpp +++ b/src/discord-rpc-simple.cpp @@ -322,3 +322,7 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) Frame.length = jsonWrite - Frame.message; ConnectionWrite(&Frame); } + +extern "C" void Discord_Update() +{ +} diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index 64b9fc8..b44dd49 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -2,6 +2,7 @@ #include "connection.h" #include "yolojson.h" +#include "rapidjson/document.h" #include @@ -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