Remove CurrentPresence and add boolean instead
This removes the need for having 2 big buffers in favor of using a small boolean
This commit is contained in:
parent
d2eaaa8e41
commit
311c21b520
@ -54,6 +54,7 @@ static std::atomic_bool WasJustDisconnected{false};
|
|||||||
static std::atomic_bool GotErrorMessage{false};
|
static std::atomic_bool GotErrorMessage{false};
|
||||||
static std::atomic_bool WasJoinGame{false};
|
static std::atomic_bool WasJoinGame{false};
|
||||||
static std::atomic_bool WasSpectateGame{false};
|
static std::atomic_bool WasSpectateGame{false};
|
||||||
|
static std::atomic_bool UpdatePresence{false};
|
||||||
static char JoinGameSecret[256];
|
static char JoinGameSecret[256];
|
||||||
static char SpectateGameSecret[256];
|
static char SpectateGameSecret[256];
|
||||||
static int LastErrorCode{0};
|
static int LastErrorCode{0};
|
||||||
@ -62,7 +63,6 @@ static int LastDisconnectErrorCode{0};
|
|||||||
static char LastDisconnectErrorMessage[256];
|
static char LastDisconnectErrorMessage[256];
|
||||||
static std::mutex PresenceMutex;
|
static std::mutex PresenceMutex;
|
||||||
static std::mutex HandlerMutex;
|
static std::mutex HandlerMutex;
|
||||||
static QueuedMessage CurrentPresence{};
|
|
||||||
static QueuedMessage QueuedPresence{};
|
static QueuedMessage QueuedPresence{};
|
||||||
static MsgQueue<QueuedMessage, MessageQueueSize> SendQueue;
|
static MsgQueue<QueuedMessage, MessageQueueSize> SendQueue;
|
||||||
static MsgQueue<User, JoinQueueSize> JoinAskQueue;
|
static MsgQueue<User, JoinQueueSize> JoinAskQueue;
|
||||||
@ -215,17 +215,17 @@ static void Discord_UpdateConnection(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// writes
|
// writes
|
||||||
if (QueuedPresence.length) {
|
if (UpdatePresence.exchange(false) && QueuedPresence.length) {
|
||||||
QueuedMessage local;
|
QueuedMessage local;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(PresenceMutex);
|
std::lock_guard<std::mutex> guard(PresenceMutex);
|
||||||
local.Copy(QueuedPresence);
|
local.Copy(QueuedPresence);
|
||||||
QueuedPresence.length = 0;
|
|
||||||
}
|
}
|
||||||
if (!Connection->Write(local.buffer, local.length)) {
|
if (!Connection->Write(local.buffer, local.length)) {
|
||||||
// if we fail to send, requeue
|
// if we fail to send, requeue
|
||||||
std::lock_guard<std::mutex> guard(PresenceMutex);
|
std::lock_guard<std::mutex> guard(PresenceMutex);
|
||||||
QueuedPresence.Copy(local);
|
QueuedPresence.Copy(local);
|
||||||
|
UpdatePresence.exchange(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -311,8 +311,8 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
|
|||||||
Connection = RpcConnection::Create(applicationId);
|
Connection = RpcConnection::Create(applicationId);
|
||||||
Connection->onConnect = [](JsonDocument& readyMessage) {
|
Connection->onConnect = [](JsonDocument& readyMessage) {
|
||||||
Discord_UpdateHandlers(&QueuedHandlers);
|
Discord_UpdateHandlers(&QueuedHandlers);
|
||||||
if (CurrentPresence.length > 0) {
|
if (QueuedPresence.length > 0) {
|
||||||
QueuedPresence.Copy(CurrentPresence);
|
UpdatePresence.exchange(true);
|
||||||
SignalIOActivity();
|
SignalIOActivity();
|
||||||
}
|
}
|
||||||
auto data = GetObjMember(&readyMessage, "data");
|
auto data = GetObjMember(&readyMessage, "data");
|
||||||
@ -355,8 +355,8 @@ extern "C" DISCORD_EXPORT void Discord_Shutdown(void)
|
|||||||
Connection->onConnect = nullptr;
|
Connection->onConnect = nullptr;
|
||||||
Connection->onDisconnect = nullptr;
|
Connection->onDisconnect = nullptr;
|
||||||
Handlers = {};
|
Handlers = {};
|
||||||
CurrentPresence.length = 0;
|
|
||||||
QueuedPresence.length = 0;
|
QueuedPresence.length = 0;
|
||||||
|
UpdatePresence.exchange(false);
|
||||||
if (IoThread != nullptr) {
|
if (IoThread != nullptr) {
|
||||||
IoThread->Stop();
|
IoThread->Stop();
|
||||||
delete IoThread;
|
delete IoThread;
|
||||||
@ -372,12 +372,7 @@ extern "C" DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence*
|
|||||||
std::lock_guard<std::mutex> guard(PresenceMutex);
|
std::lock_guard<std::mutex> guard(PresenceMutex);
|
||||||
QueuedPresence.length = JsonWriteRichPresenceObj(
|
QueuedPresence.length = JsonWriteRichPresenceObj(
|
||||||
QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
|
QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
|
||||||
}
|
UpdatePresence.exchange(true);
|
||||||
if (presence) {
|
|
||||||
CurrentPresence.Copy(QueuedPresence);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
CurrentPresence.length = 0;
|
|
||||||
}
|
}
|
||||||
SignalIOActivity();
|
SignalIOActivity();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user