From 2331374d1ffe2432496e484443a58e859a7f560f Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 16 Feb 2025 16:56:14 +0300 Subject: Discord: the safer way of handling web sockets --- protocols/Discord/src/connection.cpp | 4 ++-- protocols/Discord/src/gateway.cpp | 14 ++++++-------- protocols/Discord/src/proto.cpp | 5 +++-- protocols/Discord/src/proto.h | 7 ++++--- 4 files changed, 15 insertions(+), 15 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index ff7e765ecb..4ac9cead47 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -93,8 +93,8 @@ void CDiscordProto::ShutdownSession() pMfaDialog->Close(); if (m_hWorkerThread) SetEvent(m_evRequestsQueue); - if (m_ws) - m_ws->terminate(); + if (m_bConnected) + m_ws.terminate(); if (m_hAPIConnection) Netlib_Shutdown(m_hAPIConnection); diff --git a/protocols/Discord/src/gateway.cpp b/protocols/Discord/src/gateway.cpp index d6b7846eeb..eb29043394 100644 --- a/protocols/Discord/src/gateway.cpp +++ b/protocols/Discord/src/gateway.cpp @@ -22,12 +22,12 @@ along with this program. If not, see . bool CDiscordProto::GatewaySend(const JSONNode &pRoot) { - if (m_ws == nullptr) + if (!m_bConnected) return false; json_string szText = pRoot.write(); debugLogA("Gateway send: %s", szText.c_str()); - m_ws->sendText(szText.c_str()); + m_ws.sendText(szText.c_str()); return true; } @@ -51,8 +51,7 @@ bool CDiscordProto::GatewayThreadWorker() hdrs.AddHeader("Cookie", m_szWSCookie); } - JsonWebSocket ws(this); - NLHR_PTR pReply(ws.connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", &hdrs)); + NLHR_PTR pReply(m_ws.connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", &hdrs)); if (pReply == nullptr) { debugLogA("Gateway connection failed, exiting"); return false; @@ -74,10 +73,9 @@ bool CDiscordProto::GatewayThreadWorker() // succeeded! debugLogA("Gateway connection succeeded"); - m_ws = &ws; - ws.run(); - - m_ws = nullptr; + m_bConnected = true; + m_ws.run(); + m_bConnected = false; return true; } diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index f1df2c2a12..5a33a9d6f5 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -44,6 +44,7 @@ static int compareCalls(const CDiscordVoiceCall *p1, const CDiscordVoiceCall *p2 CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) : PROTO(proto_name, username), + m_ws(this), m_impl(*this), m_arHttpQueue(10, compareRequests), m_evRequestsQueue(CreateEvent(nullptr, FALSE, FALSE, nullptr)), @@ -174,8 +175,8 @@ void CDiscordProto::OnShutdown() for (auto &it : arGuilds) it->SaveToFile(); - if (m_ws) - m_ws->terminate(); + if (m_bConnected) + m_ws.terminate(); if (g_plugin.bVoiceService) CallService(MS_VOICESERVICE_UNREGISTER, (WPARAM)m_szModuleName, 0); diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 8342ba5724..a147124a43 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -364,9 +364,10 @@ class CDiscordProto : public PROTO HANDLE m_hWorkerThread; // worker thread handle HNETLIBCONN m_hAPIConnection; // working connection - bool + bool m_bOnline, // protocol is online - m_bTerminated; // Miranda's going down + m_bTerminated, // Miranda's going down + m_bConnected; // web socket is connected ////////////////////////////////////////////////////////////////////////////////////// // gateway @@ -380,7 +381,7 @@ class CDiscordProto : public PROTO m_szWSCookie; // cookie used for establishing websocket connection HNETLIBUSER m_hGatewayNetlibUser; // the separate netlib user handle for gateways - JsonWebSocket *m_ws; + JsonWebSocket m_ws; void __cdecl GatewayThread(void*); bool GatewayThreadWorker(void); -- cgit v1.2.3