Just keep a single queued presence message.

This commit is contained in:
Chris Marsh 2017-07-28 10:59:32 -07:00
parent a6a9b6259f
commit e69f9fbf71

View file

@ -7,6 +7,7 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <mutex>
#ifndef DISCORD_DISABLE_IO_THREAD #ifndef DISCORD_DISABLE_IO_THREAD
#include <condition_variable> #include <condition_variable>
@ -19,6 +20,14 @@ constexpr size_t MessageQueueSize{8};
struct QueuedMessage { struct QueuedMessage {
size_t length; size_t length;
char buffer[MaxMessageSize]; char buffer[MaxMessageSize];
void Copy(const QueuedMessage& other)
{
length = other.length;
if (length) {
memcpy(buffer, other.buffer, length);
}
}
}; };
static RpcConnection* Connection{nullptr}; static RpcConnection* Connection{nullptr};
@ -34,6 +43,8 @@ static int LastErrorCode{0};
static char LastErrorMessage[256]; static char LastErrorMessage[256];
static int LastDisconnectErrorCode{0}; static int LastDisconnectErrorCode{0};
static char LastDisconnectErrorMessage[256]; static char LastDisconnectErrorMessage[256];
static std::mutex PresenceMutex;
static QueuedMessage QueuedPresence{};
static QueuedMessage SendQueue[MessageQueueSize]{}; static QueuedMessage SendQueue[MessageQueueSize]{};
static std::atomic_uint SendQueueNextAdd{0}; static std::atomic_uint SendQueueNextAdd{0};
static std::atomic_uint SendQueueNextSend{0}; static std::atomic_uint SendQueueNextSend{0};
@ -139,6 +150,20 @@ extern "C" void Discord_UpdateConnection()
} }
// writes // writes
if (QueuedPresence.length) {
QueuedMessage local;
PresenceMutex.lock();
local.Copy(QueuedPresence);
QueuedPresence.length = 0;
PresenceMutex.unlock();
if (!Connection->Write(local.buffer, local.length)) {
// if we fail to send, requeue
PresenceMutex.lock();
QueuedPresence.Copy(local);
PresenceMutex.unlock();
}
}
while (SendQueuePendingSends.load()) { while (SendQueuePendingSends.load()) {
auto qmessage = SendQueueGetNextSendMessage(); auto qmessage = SendQueueGetNextSendMessage();
Connection->Write(qmessage->buffer, qmessage->length); Connection->Write(qmessage->buffer, qmessage->length);
@ -240,14 +265,12 @@ extern "C" void Discord_Shutdown()
extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
{ {
auto qmessage = SendQueueGetNextAddMessage(); PresenceMutex.lock();
if (qmessage) { QueuedPresence.length = JsonWriteRichPresenceObj(
qmessage->length = JsonWriteRichPresenceObj( QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
qmessage->buffer, sizeof(qmessage->buffer), Nonce++, Pid, presence); PresenceMutex.unlock();
SendQueueCommitMessage();
SignalIOActivity(); SignalIOActivity();
} }
}
extern "C" void Discord_RunCallbacks() extern "C" void Discord_RunCallbacks()
{ {