summaryrefslogtreecommitdiff
path: root/protocols/Discord/src
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2025-02-16 16:56:14 +0300
committerGeorge Hazan <george.hazan@gmail.com>2025-02-16 16:56:14 +0300
commit2331374d1ffe2432496e484443a58e859a7f560f (patch)
tree8c8cf662bb9e8df29b7fb1969e016dcc9632a04a /protocols/Discord/src
parent65783c3a344eaf555e46ab77dc9f3603cc07ce48 (diff)
Discord: the safer way of handling web sockets
Diffstat (limited to 'protocols/Discord/src')
-rw-r--r--protocols/Discord/src/connection.cpp4
-rw-r--r--protocols/Discord/src/gateway.cpp14
-rw-r--r--protocols/Discord/src/proto.cpp5
-rw-r--r--protocols/Discord/src/proto.h7
4 files changed, 15 insertions, 15 deletions
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 <http://www.gnu.org/licenses/>.
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<CDiscordProto> 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<CDiscordProto>(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<CDiscordProto>
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<CDiscordProto>
m_szWSCookie; // cookie used for establishing websocket connection
HNETLIBUSER m_hGatewayNetlibUser; // the separate netlib user handle for gateways
- JsonWebSocket<CDiscordProto> *m_ws;
+ JsonWebSocket<CDiscordProto> m_ws;
void __cdecl GatewayThread(void*);
bool GatewayThreadWorker(void);