Skip to content

Commit

Permalink
add ability to snoop on the IPC messages
Browse files Browse the repository at this point in the history
  • Loading branch information
p0358 committed Nov 2, 2023
1 parent ae35306 commit f9ec8b7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
54 changes: 47 additions & 7 deletions examples/send-presence/send-presence.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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] <empty>", 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);
}

Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -250,6 +288,8 @@ int main(int argc, char* argv[])
{
discordInit();

updateDiscordPresence();

gameLoop();

Discord_Shutdown();
Expand Down
1 change: 1 addition & 0 deletions include/discord_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions src/discord_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "rpc_connection.h"
#include "serialization.h"

#include <stdio.h>

#include <atomic>
#include <chrono>
#include <mutex>
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions src/rpc_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/rpc_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*&);
Expand Down

0 comments on commit f9ec8b7

Please sign in to comment.