diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..707812f --- /dev/null +++ b/.clang-format @@ -0,0 +1,62 @@ +--- +Language: Cpp +AccessModifierOffset: -4 +AlignAfterOpenBracket: true +AlignEscapedNewlinesLeft: true +AlignOperands: false +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AlwaysBreakAfterDefinitionReturnType: false +AlwaysBreakTemplateDeclarations: true +AlwaysBreakBeforeMultilineStrings: false +BreakBeforeBraces: Stroustrup +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: true +BinPackParameters: false +BinPackArguments: false +ColumnLimit: 100 +CommentPragmas: '' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 2 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +ForEachMacros: [ foreach ] +IndentCaseLabels: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 4 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakString: 1000 +PenaltyBreakFirstLessLess: 120 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 9999999 +PointerAlignment: Left +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: true +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never +... diff --git a/examples/send-presence/send-presence.c b/examples/send-presence/send-presence.c index 0d1cad7..a761303 100644 --- a/examples/send-presence/send-presence.c +++ b/examples/send-presence/send-presence.c @@ -14,7 +14,8 @@ static const char* APPLICATION_ID = "338030514596216832"; static int FrustrationLevel = 0; -static void updateDiscordPresence() { +static void updateDiscordPresence() +{ char buffer[256]; DiscordRichPresence discordPresence; memset(&discordPresence, 0, sizeof(discordPresence)); @@ -24,24 +25,29 @@ static void updateDiscordPresence() { Discord_UpdatePresence(&discordPresence); } -static void handleDiscordReady() { +static void handleDiscordReady() +{ printf("\nDiscord: ready\n"); } -static void handleDiscordDisconnected(int errcode, const char* message) { +static void handleDiscordDisconnected(int errcode, const char* message) +{ printf("\nDiscord: disconnected (%d: %s)\n", errcode, message); } -static void handleDiscordError(int errcode, const char* message) { +static void handleDiscordError(int errcode, const char* message) +{ printf("\nDiscord: error (%d: %s)\n", errcode, message); } -static void handleDiscordPresenceRequested() { +static void handleDiscordPresenceRequested() +{ printf("\nDiscord: requests presence\n"); updateDiscordPresence(); } -static int prompt(char* line, size_t size) { +static int prompt(char* line, size_t size) +{ int res; char* nl; printf("\n> "); @@ -55,7 +61,8 @@ static int prompt(char* line, size_t size) { return res; } -static void gameLoop() { +static void gameLoop() +{ char line[512]; char* space; @@ -63,7 +70,8 @@ static void gameLoop() { while (prompt(line, sizeof(line))) { if (time(NULL) & 1) { printf("I don't understand that.\n"); - } else { + } + else { space = strchr(line, ' '); if (space) { *space = 0; @@ -72,9 +80,9 @@ static void gameLoop() { } ++FrustrationLevel; - + updateDiscordPresence(); - + #ifdef DISCORD_DISABLE_IO_THREAD Discord_UpdateConnection(); #endif @@ -82,7 +90,8 @@ static void gameLoop() { } } -int main() { +int main() +{ DiscordEventHandlers handlers; memset(&handlers, 0, sizeof(handlers)); handlers.ready = handleDiscordReady; @@ -92,8 +101,7 @@ int main() { Discord_Initialize(APPLICATION_ID, &handlers); gameLoop(); - + Discord_Shutdown(); return 0; } - diff --git a/include/discord-rpc.h b/include/discord-rpc.h index e61d1f6..37f389c 100644 --- a/include/discord-rpc.h +++ b/include/discord-rpc.h @@ -3,7 +3,7 @@ //#define DISCORD_DISABLE_IO_THREAD -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -47,6 +47,6 @@ void Discord_UpdateConnection(); void Discord_UpdatePresence(const DiscordRichPresence* presence); -#ifdef __cplusplus +#ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/backoff.h b/src/backoff.h index ad14143..cbf1c90 100644 --- a/src/backoff.h +++ b/src/backoff.h @@ -4,8 +4,7 @@ #include #include -struct Backoff -{ +struct Backoff { int64_t minAmount; int64_t maxAmount; int64_t current; @@ -13,16 +12,15 @@ struct Backoff std::mt19937_64 randGenerator; std::uniform_real_distribution<> randDistribution; - double rand01() { - return randDistribution(randGenerator); - } + double rand01() { return randDistribution(randGenerator); } Backoff(int64_t min, int64_t max) - : minAmount(min) - , maxAmount(max) - , current(min) - , fails(0) - {} + : minAmount(min) + , maxAmount(max) + , current(min) + , fails(0) + { + } void reset() { @@ -38,4 +36,3 @@ struct Backoff return current; } }; - diff --git a/src/connection_win.cpp b/src/connection_win.cpp index 47b7fec..55a1a39 100644 --- a/src/connection_win.cpp +++ b/src/connection_win.cpp @@ -16,7 +16,7 @@ struct BaseConnectionWin : public BaseConnection { }; static BaseConnectionWin Connection; -//static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc"; +// static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc"; static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc-0"; /*static*/ BaseConnection* BaseConnection::Create() @@ -35,7 +35,8 @@ bool BaseConnection::Open() { auto self = reinterpret_cast(this); for (;;) { - self->pipe = ::CreateFileW(PipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); + self->pipe = ::CreateFileW( + PipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); if (self->pipe != INVALID_HANDLE_VALUE) { return true; } @@ -77,4 +78,3 @@ bool BaseConnection::Read(void* data, size_t length) } return false; } - diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index 0f7be50..07271b9 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -53,10 +53,12 @@ static std::thread IoThread; static void UpdateReconnectTime() { - NextConnect = std::chrono::system_clock::now() + std::chrono::duration{ReconnectTimeMs.nextDelay()}; + NextConnect = std::chrono::system_clock::now() + + std::chrono::duration{ReconnectTimeMs.nextDelay()}; } -static QueuedMessage* SendQueueGetNextAddMessage() { +static QueuedMessage* SendQueueGetNextAddMessage() +{ // if we are falling behind, bail if (SendQueuePendingSends.load() >= MessageQueueSize) { return nullptr; @@ -64,11 +66,13 @@ static QueuedMessage* SendQueueGetNextAddMessage() { auto index = (SendQueueNextAdd++) % MessageQueueSize; return &SendQueue[index]; } -static QueuedMessage* SendQueueGetNextSendMessage() { +static QueuedMessage* SendQueueGetNextSendMessage() +{ auto index = (SendQueueNextSend++) % MessageQueueSize; return &SendQueue[index]; } -static void SendQueueCommitMessage() { +static void SendQueueCommitMessage() +{ SendQueuePendingSends++; } @@ -145,7 +149,7 @@ extern "C" void Discord_UpdateConnection() void DiscordRpcIo() { const std::chrono::duration maxWait{500LL}; - + while (KeepRunning.load()) { Discord_UpdateConnection(); @@ -166,7 +170,8 @@ bool RegisterForEvent(const char* evtName) { auto qmessage = SendQueueGetNextAddMessage(); if (qmessage) { - qmessage->length = JsonWriteSubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName); + qmessage->length = + JsonWriteSubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName); SendQueueCommitMessage(); SignalIOActivity(); return true; @@ -233,7 +238,8 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) { auto qmessage = SendQueueGetNextAddMessage(); if (qmessage) { - qmessage->length = JsonWriteRichPresenceObj(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, Pid, presence); + qmessage->length = JsonWriteRichPresenceObj( + qmessage->buffer, sizeof(qmessage->buffer), Nonce++, Pid, presence); SendQueueCommitMessage(); SignalIOActivity(); } diff --git a/src/rpc_connection.cpp b/src/rpc_connection.cpp index ececc3e..8bfef98 100644 --- a/src/rpc_connection.cpp +++ b/src/rpc_connection.cpp @@ -44,7 +44,8 @@ void RpcConnection::Open() if (evt == message.MemberEnd() || !evt->value.IsString()) { return; } - if (!strcmp(cmd->value.GetString(), "DISPATCH") && !strcmp(evt->value.GetString(), "READY")) { + if (!strcmp(cmd->value.GetString(), "DISPATCH") && + !strcmp(evt->value.GetString(), "READY")) { state = State::Connected; if (onConnect) { onConnect(); @@ -54,7 +55,8 @@ void RpcConnection::Open() } else { sendFrame.opcode = Opcode::Handshake; - sendFrame.length = JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId); + sendFrame.length = + JsonWriteHandshakeObj(sendFrame.message, sizeof(sendFrame.message), RpcVersion, appId); if (connection->Write(&sendFrame, sizeof(MessageFrameHeader) + sendFrame.length)) { state = State::SentHandshake; @@ -110,8 +112,7 @@ bool RpcConnection::Read(JsonDocument& message) } switch (readFrame.opcode) { - case Opcode::Close: - { + case Opcode::Close: { message.ParseInsitu(readFrame.message); lastErrorCode = message["code"].GetInt(); const auto& m = message["message"]; diff --git a/src/rpc_connection.h b/src/rpc_connection.h index 49ced0f..cafe8f5 100644 --- a/src/rpc_connection.h +++ b/src/rpc_connection.h @@ -3,7 +3,8 @@ #include "connection.h" #include "serialization.h" -// I took this from the buffer size libuv uses for named pipes; I suspect ours would usually be much smaller. +// I took this from the buffer size libuv uses for named pipes; I suspect ours would usually be much +// smaller. constexpr size_t MaxRpcFrameSize = 64 * 1024; struct RpcConnection { @@ -43,9 +44,7 @@ struct RpcConnection { static RpcConnection* Create(const char* applicationId); static void Destroy(RpcConnection*&); - inline bool IsOpen() const { - return state == State::Connected; - } + inline bool IsOpen() const { return state == State::Connected; } void Open(); void Close(); diff --git a/src/serialization.cpp b/src/serialization.cpp index 371db08..032ccc4 100644 --- a/src/serialization.cpp +++ b/src/serialization.cpp @@ -5,13 +5,15 @@ MallocAllocator MallocAllocatorInst; // it's ever so slightly faster to not have to strlen the key -template -void WriteKey(JsonWriter& w, T& k) { +template +void WriteKey(JsonWriter& w, T& k) +{ w.Key(k, sizeof(T) - 1); } -template -void WriteOptionalString(JsonWriter& w, T& k, const char* value) { +template +void WriteOptionalString(JsonWriter& w, T& k, const char* value) +{ if (value) { w.Key(k, sizeof(T) - 1); w.String(value); @@ -45,7 +47,11 @@ void JsonWriteCommandEnd(JsonWriter& writer) writer.EndObject(); // top level } -size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, int nonce, int pid, const DiscordRichPresence* presence) +size_t JsonWriteRichPresenceObj(char* dest, + size_t maxLen, + int nonce, + int pid, + const DiscordRichPresence* presence) { DirectStringBuffer sb(dest, maxLen); StackAllocator wa; @@ -79,7 +85,8 @@ size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, int nonce, int pid, c writer.EndObject(); } - if (presence->largeImageKey || presence->largeImageText || presence->smallImageKey || presence->smallImageText) { + if (presence->largeImageKey || presence->largeImageText || presence->smallImageKey || + presence->smallImageText) { WriteKey(writer, "assets"); writer.StartObject(); @@ -156,13 +163,13 @@ size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const cha writer.StartObject(); JsonWriteNonce(writer, nonce); - + WriteKey(writer, "cmd"); writer.String("SUBSCRIBE"); - + WriteKey(writer, "evt"); writer.String(evtName); - + writer.EndObject(); return sb.GetSize(); diff --git a/src/serialization.h b/src/serialization.h index 1d6f1bf..d15eb42 100644 --- a/src/serialization.h +++ b/src/serialization.h @@ -8,8 +8,9 @@ #include "rapidjson/internal/itoa.h" // if only there was a standard library function for this -template -inline size_t StringCopy(char (&dest)[Len], const char* src) { +template +inline size_t StringCopy(char (&dest)[Len], const char* src) +{ if (!dest || !src || !Len) { return 0; } @@ -26,20 +27,30 @@ size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* // Commands struct DiscordRichPresence; -size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, int nonce, int pid, const DiscordRichPresence* presence); +size_t JsonWriteRichPresenceObj(char* dest, + size_t maxLen, + int nonce, + int pid, + const DiscordRichPresence* presence); size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName); -// I want to use as few allocations as I can get away with, and to do that with RapidJson, you need to supply some of +// I want to use as few allocations as I can get away with, and to do that with RapidJson, you need +// to supply some of // your own allocators for stuff rather than use the defaults class LinearAllocator { public: char* buffer_; char* end_; - LinearAllocator() { + LinearAllocator() + { assert(0); // needed for some default case in rapidjson, should not use } - LinearAllocator(char* buffer, size_t size) : buffer_(buffer), end_(buffer + size) {} + LinearAllocator(char* buffer, size_t size) + : buffer_(buffer) + , end_(buffer + size) + { + } static const bool kNeedFree = false; void* Malloc(size_t size) { @@ -60,14 +71,17 @@ public: assert(!originalPtr && !originalSize); return Malloc(newSize); } - static void Free(void* ptr) { /* shrug */ } + static void Free(void* ptr) { /* shrug */} }; -template +template class FixedLinearAllocator : public LinearAllocator { public: char fixedBuffer_[Size]; - FixedLinearAllocator() : LinearAllocator(fixedBuffer_, Size) {} + FixedLinearAllocator() + : LinearAllocator(fixedBuffer_, Size) + { + } static const bool kNeedFree = false; }; @@ -80,10 +94,11 @@ public: char* current_; DirectStringBuffer(char* buffer, size_t maxLen) - : buffer_(buffer) - , end_(buffer + maxLen) - , current_(buffer) - {} + : buffer_(buffer) + , end_(buffer + maxLen) + , current_(buffer) + { + } void Put(char c) { @@ -92,10 +107,7 @@ public: } } void Flush() {} - size_t GetSize() const - { - return current_ - buffer_; - } + size_t GetSize() const { return current_ - buffer_; } }; using MallocAllocator = rapidjson::CrtAllocator; @@ -104,19 +116,25 @@ using UTF8 = rapidjson::UTF8; // Writer appears to need about 16 bytes per nested object level (with 64bit size_t) using StackAllocator = FixedLinearAllocator<2048>; constexpr size_t WriterNestingLevels = 2048 / (2 * sizeof(size_t)); -using JsonWriter = rapidjson::Writer; +using JsonWriter = + rapidjson::Writer; using JsonDocumentBase = rapidjson::GenericDocument; -class JsonDocument : public JsonDocumentBase -{ +class JsonDocument : public JsonDocumentBase { public: static const int kDefaultChunkCapacity = 32 * 1024; - // json parser will use this buffer first, then allocate more if needed; I seriously doubt we send any messages that would use all of this, though. + // json parser will use this buffer first, then allocate more if needed; I seriously doubt we + // send any messages that would use all of this, though. char parseBuffer_[32 * 1024]; MallocAllocator mallocAllocator_; PoolAllocator poolAllocator_; StackAllocator stackAllocator_; - JsonDocument() : JsonDocumentBase(rapidjson::kObjectType, &poolAllocator_, sizeof(stackAllocator_.fixedBuffer_), &stackAllocator_) - , poolAllocator_(parseBuffer_, sizeof(parseBuffer_), kDefaultChunkCapacity, &mallocAllocator_) - , stackAllocator_() - {} + JsonDocument() + : JsonDocumentBase(rapidjson::kObjectType, + &poolAllocator_, + sizeof(stackAllocator_.fixedBuffer_), + &stackAllocator_) + , poolAllocator_(parseBuffer_, sizeof(parseBuffer_), kDefaultChunkCapacity, &mallocAllocator_) + , stackAllocator_() + { + } };