From d87186978ec3448eacc8eed94efc35ae1c67bfba Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 3 Jan 2020 16:55:12 +0300 Subject: Discord to use system window to maintain its timers --- protocols/Discord/src/connection.cpp | 7 +------ protocols/Discord/src/dispatch.cpp | 13 +------------ protocols/Discord/src/main.cpp | 13 ------------- protocols/Discord/src/proto.cpp | 18 ++++++++---------- protocols/Discord/src/proto.h | 35 +++++++++++++++++++++++++++++------ protocols/Discord/src/stdafx.h | 1 - 6 files changed, 39 insertions(+), 48 deletions(-) (limited to 'protocols') diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index 1ffa407a36..758d2a05a9 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -73,11 +73,6 @@ void CDiscordProto::OnLoggedIn() ForkThread(&CDiscordProto::GatewayThread, nullptr); } -static void __stdcall sttKillTimer(void *param) -{ - KillTimer(g_hwndHeartbeat, (UINT_PTR)param); -} - void CDiscordProto::OnLoggedOut() { debugLogA("CDiscordProto::OnLoggedOut"); @@ -85,7 +80,7 @@ void CDiscordProto::OnLoggedOut() m_bTerminated = true; m_iGatewaySeq = 0; - CallFunctionAsync(sttKillTimer, this); + m_impl.m_heartBeat.Stop(); ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_OFFLINE); m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index d6293170bf..dccb94995e 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -460,21 +460,10 @@ void CDiscordProto::OnCommandPresence(const JSONNode &pRoot) ///////////////////////////////////////////////////////////////////////////////////////// // gateway session start -void CALLBACK CDiscordProto::HeartbeatTimerProc(HWND, UINT, UINT_PTR id, DWORD) -{ - ((CDiscordProto*)id)->GatewaySendHeartbeat(); -} - -static void __stdcall sttStartTimer(void *param) -{ - CDiscordProto *ppro = (CDiscordProto*)param; - SetTimer(g_hwndHeartbeat, (UINT_PTR)param, ppro->getHeartbeatInterval(), &CDiscordProto::HeartbeatTimerProc); -} - void CDiscordProto::OnCommandReady(const JSONNode &pRoot) { GatewaySendHeartbeat(); - CallFunctionAsync(sttStartTimer, this); + m_impl.m_heartBeat.Start(m_iHartbeatInterval); m_szGatewaySessionId = pRoot["session_id"].as_mstring(); diff --git a/protocols/Discord/src/main.cpp b/protocols/Discord/src/main.cpp index 7273663ecb..559a0da4da 100644 --- a/protocols/Discord/src/main.cpp +++ b/protocols/Discord/src/main.cpp @@ -19,8 +19,6 @@ along with this program. If not, see . CMPlugin g_plugin; -HWND g_hwndHeartbeat; - ///////////////////////////////////////////////////////////////////////////////////////// PLUGININFOEX pluginInfoEx = { @@ -58,17 +56,6 @@ IconItem g_iconList[] = int CMPlugin::Load() { - g_hwndHeartbeat = CreateWindowEx(0, L"STATIC", nullptr, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr); - g_plugin.registerIcon("Protocols/Discord", g_iconList); return 0; } - -///////////////////////////////////////////////////////////////////////////////////////// -// Unload - -int CMPlugin::Unload() -{ - DestroyWindow(g_hwndHeartbeat); - return 0; -} diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index fa348808f1..bc15c1b22e 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -39,6 +39,7 @@ static int compareGuilds(const CDiscordGuild *p1, const CDiscordGuild *p2) CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) : PROTO(proto_name, username), + m_impl(*this), m_arHttpQueue(10, compareRequests), m_evRequestsQueue(CreateEvent(nullptr, FALSE, FALSE, nullptr)), arUsers(10, compareUsers), @@ -469,18 +470,15 @@ int CDiscordProto::UserIsTyping(MCONTACT hContact, int type) ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::MarkReadTimerProc(HWND hwnd, UINT, UINT_PTR id, DWORD) +void CDiscordProto::SendMarkRead() { - CDiscordProto *ppro = (CDiscordProto*)(id - 1); - - mir_cslock lck(ppro->csMarkReadQueue); - while (ppro->arMarkReadQueue.getCount()) { - CDiscordUser *pUser = ppro->arMarkReadQueue[0]; + mir_cslock lck(csMarkReadQueue); + while (arMarkReadQueue.getCount()) { + CDiscordUser *pUser = arMarkReadQueue[0]; CMStringA szUrl(FORMAT, "/channels/%lld/messages/%lld/ack", pUser->channelId, pUser->lastMsgId); - ppro->Push(new AsyncHttpRequest(ppro, REQUEST_POST, szUrl, nullptr)); - ppro->arMarkReadQueue.remove(0); + Push(new AsyncHttpRequest(this, REQUEST_POST, szUrl, nullptr)); + arMarkReadQueue.remove(0); } - KillTimer(hwnd, id); } int CDiscordProto::OnDbEventRead(WPARAM, LPARAM hDbEvent) @@ -495,7 +493,7 @@ int CDiscordProto::OnDbEventRead(WPARAM, LPARAM hDbEvent) return 0; if (m_bOnline) { - SetTimer(g_hwndHeartbeat, UINT_PTR(this) + 1, 200, &CDiscordProto::MarkReadTimerProc); + m_impl.m_markRead.Start(200); CDiscordUser *pUser = FindUser(getId(hContact, DB_KEY_ID)); if (pUser != nullptr) { diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 2190ee31a7..63dc029fb3 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -118,6 +118,34 @@ class CDiscordProto : public PROTO friend struct AsyncHttpRequest; friend class CDiscardAccountOptions; + class CDiscordProtoImpl + { + friend class CDiscordProto; + CDiscordProto &m_proto; + + CTimer m_heartBeat, m_markRead; + void OnHeartBeat(CTimer *) { + m_proto.GatewaySendHeartbeat(); + } + + void OnMarkRead(CTimer *pTimer) { + m_proto.SendMarkRead(); + pTimer->Stop(); + } + + public: + CDiscordProtoImpl(CDiscordProto &pro) : + m_proto(pro), + m_markRead(Miranda_GetSystemWindow(), UINT_PTR(this)), + m_heartBeat(Miranda_GetSystemWindow(), UINT_PTR(this) + 1) + { + m_markRead.OnEvent = Callback(this, &CDiscordProtoImpl::OnMarkRead); + m_heartBeat.OnEvent = Callback(this, &CDiscordProtoImpl::OnHeartBeat); + } + }; + + CDiscordProtoImpl m_impl; + ////////////////////////////////////////////////////////////////////////////////////// // threads @@ -360,16 +388,12 @@ public: ////////////////////////////////////////////////////////////////////////////////////// // Misc + void SendMarkRead(void); void SetServerStatus(int iStatus); void RemoveFriend(SnowFlake id); CMStringW GetAvatarFilename(MCONTACT hContact); void CheckAvatarChange(MCONTACT hContact, const CMStringW &wszNewHash); - - __forceinline int getHeartbeatInterval() const { return m_iHartbeatInterval; } - - static void CALLBACK HeartbeatTimerProc(HWND hwnd, UINT msg, UINT_PTR id, DWORD); - static void CALLBACK MarkReadTimerProc(HWND hwnd, UINT msg, UINT_PTR id, DWORD); }; ///////////////////////////////////////////////////////////////////////////////////////// @@ -379,5 +403,4 @@ struct CMPlugin : public ACCPROTOPLUGIN CMPlugin(); int Load() override; - int Unload() override; }; diff --git a/protocols/Discord/src/stdafx.h b/protocols/Discord/src/stdafx.h index ae55f4ab7e..568f5db2ed 100644 --- a/protocols/Discord/src/stdafx.h +++ b/protocols/Discord/src/stdafx.h @@ -47,7 +47,6 @@ #include "../../libs/zlib/src/zlib.h" extern IconItem g_iconList[]; -extern HWND g_hwndHeartbeat; #define DB_KEY_ID "id" #define DB_KEY_PASSWORD "Password" -- cgit v1.2.3