diff --git a/examples/send-presence/send-presence.c b/examples/send-presence/send-presence.c index 184717a..d27d6d0 100644 --- a/examples/send-presence/send-presence.c +++ b/examples/send-presence/send-presence.c @@ -16,6 +16,7 @@ static int FrustrationLevel = 0; static int64_t StartTime; static char SendPresence = 1; static char SendButtons = 0; +static char Debug = 1; static int prompt(char* line, size_t size) { @@ -132,16 +133,46 @@ static void handleDiscordJoinRequest(const DiscordUser* request) } } +static void handleDebug(char isOut, const char* opcodeName, const char* message, uint32_t messageLength) +{ + unsigned int len = max(messageLength, 7u) + 6 + 7 + 7 + 1; + char* message = (char*)malloc(len); + char* direction = isOut ? "send" : "receive"; + if (messageLength || !message || !message[0]) { + sprintf_s(message, len, "[%s] [%s] ", direction, opcodeName); + } + else { + int written = sprintf_s(message, len, "[%s] [%s] ", direction, opcodeName); + strncpy_s(message + written, len - written, message, messageLength); + } + printf("[DEBUG] %s\n", message); + free(message); +} + +static void populateHandlers(DiscordEventHandlers* handlers) +{ + memset(handlers, 0, sizeof(handlers)); + handlers->ready = handleDiscordReady; + handlers->disconnected = handleDiscordDisconnected; + handlers->errored = handleDiscordError; + handlers->joinGame = handleDiscordJoin; + handlers->spectateGame = handleDiscordSpectate; + handlers->joinRequest = handleDiscordJoinRequest; + if (Debug) + handlers->debug = handleDebug; +} + +static void discordUpdateHandlers() +{ + DiscordEventHandlers handlers; + populateHandlers(&handlers); + Discord_UpdateHandlers(&handlers); +} + static void discordInit() { DiscordEventHandlers handlers; - memset(&handlers, 0, sizeof(handlers)); - handlers.ready = handleDiscordReady; - handlers.disconnected = handleDiscordDisconnected; - handlers.errored = handleDiscordError; - handlers.joinGame = handleDiscordJoin; - handlers.spectateGame = handleDiscordSpectate; - handlers.joinRequest = handleDiscordJoinRequest; + populateHandlers(&handlers); Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL); } @@ -223,6 +254,13 @@ static void gameLoop() } } + if (line[0] == 'd') { + printf("Turning debug %s\n", Debug ? "off" : "on"); + Debug = !Debug; + discordUpdateHandlers(); + continue; + } + if (time(NULL) & 1) { printf("I don't understand that.\n"); } @@ -250,6 +288,8 @@ int main(int argc, char* argv[]) { discordInit(); + updateDiscordPresence(); + gameLoop(); Discord_Shutdown(); diff --git a/include/discord_rpc.h b/include/discord_rpc.h index c66995f..96c311f 100644 --- a/include/discord_rpc.h +++ b/include/discord_rpc.h @@ -59,6 +59,7 @@ typedef struct DiscordEventHandlers { void (*ready)(const DiscordUser* request); void (*disconnected)(int errorCode, const char* message); void (*errored)(int errorCode, const char* message); + void (*debug)(char isOut, const char* opcodeName, const char* message, uint32_t messageLength); void (*joinGame)(const char* joinSecret); void (*spectateGame)(const char* spectateSecret); void (*joinRequest)(const DiscordUser* request); diff --git a/src/discord_rpc.cpp b/src/discord_rpc.cpp index f1007f1..f3d6369 100644 --- a/src/discord_rpc.cpp +++ b/src/discord_rpc.cpp @@ -6,6 +6,8 @@ #include "rpc_connection.h" #include "serialization.h" +#include + #include #include #include @@ -343,6 +345,29 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId, WasJustDisconnected.exchange(true); UpdateReconnectTime(); }; + Connection->onDebug = [](bool out, RpcConnection::MessageFrame* frame) { + if (Handlers.debug) { + char* opcode = "Unknown"; + switch (frame->opcode) { + case RpcConnection::Opcode::Handshake: + opcode = "Handshake"; + break; + case RpcConnection::Opcode::Frame: + opcode = "Frame"; + break; + case RpcConnection::Opcode::Close: + opcode = "Close"; + break; + case RpcConnection::Opcode::Ping: + opcode = "Ping"; + break; + case RpcConnection::Opcode::Pong: + opcode = "Pong"; + break; + } + Handlers.debug(out, opcode, frame->message, frame->length); + } + }; IoThread->Start(); } @@ -521,6 +546,8 @@ extern "C" DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* newH HANDLE_EVENT_REGISTRATION(joinGame, "ACTIVITY_JOIN") HANDLE_EVENT_REGISTRATION(spectateGame, "ACTIVITY_SPECTATE") HANDLE_EVENT_REGISTRATION(joinRequest, "ACTIVITY_JOIN_REQUEST") + RegisterForEvent("ACTIVITY_INVITE"); + // HANDLE_EVENT_REGISTRATION(invited, "ACTIVITY_INVITE") #undef HANDLE_EVENT_REGISTRATION diff --git a/src/rpc_connection.cpp b/src/rpc_connection.cpp index 0933162..f3dc948 100644 --- a/src/rpc_connection.cpp +++ b/src/rpc_connection.cpp @@ -48,6 +48,9 @@ void RpcConnection::Open() sendFrame.length = (uint32_t)JsonWriteHandshakeObj( sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId); + if (onDebug) + onDebug(true, &sendFrame); + if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) { state = State::SentHandshake; } @@ -71,6 +74,8 @@ bool RpcConnection::Write(const void* data, size_t length) sendFrame.opcode = Opcode::Frame; memcpy(sendFrame.message, data, length); sendFrame.length = (uint32_t)length; + if (onDebug) + onDebug(true, &sendFrame); if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) { Close(); return false; @@ -106,6 +111,9 @@ bool RpcConnection::Read(JsonDocument& message) readFrame.message[readFrame.length] = 0; } + if (onDebug) + onDebug(false, &readFrame); + switch (readFrame.opcode) { case Opcode::Close: { message.ParseInsitu(readFrame.message); @@ -119,6 +127,8 @@ bool RpcConnection::Read(JsonDocument& message) return true; case Opcode::Ping: readFrame.opcode = Opcode::Pong; + if (onDebug) + onDebug(true, &readFrame); if (!connection->Write(&readFrame, sizeof(MessageFrameHeader) + readFrame.length)) { Close(); } diff --git a/src/rpc_connection.h b/src/rpc_connection.h index bbdd05c..f7a8200 100644 --- a/src/rpc_connection.h +++ b/src/rpc_connection.h @@ -42,10 +42,11 @@ struct RpcConnection { State state{State::Disconnected}; void (*onConnect)(JsonDocument& message){nullptr}; void (*onDisconnect)(int errorCode, const char* message){nullptr}; + void (*onDebug)(bool out, MessageFrame* frame){nullptr}; char appId[64]{}; int lastErrorCode{0}; char lastErrorMessage[256]{}; - RpcConnection::MessageFrame sendFrame; + MessageFrame sendFrame; static RpcConnection* Create(const char* applicationId); static void Destroy(RpcConnection*&);