Fix up sending presence format

This commit is contained in:
Chris Marsh 2017-07-20 14:59:32 -07:00
parent 85fc57dea2
commit fa437ad897
3 changed files with 38 additions and 14 deletions

View File

@ -12,6 +12,8 @@
#include <thread> #include <thread>
#endif #endif
#include "rapidjson/internal/itoa.h"
constexpr size_t MaxMessageSize = 16 * 1024; constexpr size_t MaxMessageSize = 16 * 1024;
constexpr size_t MessageQueueSize = 8; constexpr size_t MessageQueueSize = 8;
@ -33,6 +35,8 @@ static std::atomic_uint SendQueueNextSend{0};
static std::atomic_uint SendQueuePendingSends{0}; static std::atomic_uint SendQueuePendingSends{0};
static Backoff ReconnectTimeMs(500, 60 * 1000); static Backoff ReconnectTimeMs(500, 60 * 1000);
static auto NextConnect{std::chrono::system_clock::now()}; static auto NextConnect{std::chrono::system_clock::now()};
static int Pid = 0;
static int Nonce = 1;
#ifndef DISCORD_DISABLE_IO_THREAD #ifndef DISCORD_DISABLE_IO_THREAD
static std::atomic_bool KeepRunning{ true }; static std::atomic_bool KeepRunning{ true };
@ -110,6 +114,8 @@ void SignalIOActivity()
extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
{ {
Pid = GetProcessId();
if (handlers) { if (handlers) {
Handlers = *handlers; Handlers = *handlers;
} }
@ -153,7 +159,9 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
{ {
auto qmessage = SendQueueGetNextAddMessage(); auto qmessage = SendQueueGetNextAddMessage();
if (qmessage) { if (qmessage) {
qmessage->length = JsonWriteRichPresenceObj(qmessage->buffer, sizeof(qmessage->buffer), presence); char nonce[32]{};
rapidjson::internal::i32toa(Nonce++, nonce);
qmessage->length = JsonWriteRichPresenceObj(qmessage->buffer, sizeof(qmessage->buffer), nonce, Pid, presence);
SendQueueCommitMessage(); SendQueueCommitMessage();
SignalIOActivity(); SignalIOActivity();
} }

View File

@ -93,19 +93,36 @@ void WriteOptionalString(JsonWriter& w, T& k, const char* value) {
} }
} }
size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, const DiscordRichPresence* presence) void JsonWriteCommandStart(JsonWriter& writer, const char* nonce, const char* cmd)
{
writer.StartObject();
WriteKey(writer, "nonce");
writer.String(nonce);
WriteKey(writer, "cmd");
writer.String(cmd);
WriteKey(writer, "args");
writer.StartObject();
}
void JsonWriteCommandEnd(JsonWriter& writer)
{
writer.EndObject(); // args
writer.EndObject(); // top level
}
size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, char* nonce, int pid, const DiscordRichPresence* presence)
{ {
DirectStringBuffer sb(dest, maxLen); DirectStringBuffer sb(dest, maxLen);
WriterAllocator wa; WriterAllocator wa;
JsonWriter writer(sb, &wa, WriterNestingLevels); JsonWriter writer(sb, &wa, WriterNestingLevels);
// const args = {pid, activity}; JsonWriteCommandStart(writer, nonce, "SET_ACTIVITY");
// this.socket.write(encode(OPCODES.FRAME, { nonce: uuid(), cmd : 'SET_ACTIVITY', args })
writer.StartObject(); WriteKey(writer, "pid");
writer.Int(pid);
WriteKey(writer, "args");
writer.StartObject();
WriteKey(writer, "activity"); WriteKey(writer, "activity");
writer.StartObject(); writer.StartObject();
@ -177,9 +194,7 @@ size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, const DiscordRichPres
writer.EndObject(); // activity writer.EndObject(); // activity
writer.EndObject(); // args JsonWriteCommandEnd(writer);
writer.EndObject(); // top level
return sb.GetSize(); return sb.GetSize();
} }

View File

@ -17,7 +17,8 @@ inline size_t StringCopy(char (&dest)[Len], const char* src) {
return copied - 1; return copied - 1;
} }
struct DiscordRichPresence;
size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, const DiscordRichPresence* presence);
size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId); size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId);
struct DiscordRichPresence;
size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, char* nonce, int pid, const DiscordRichPresence* presence);