From a60896051a51130a99cf1b3030c6e58163fe3dd2 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 10 Aug 2023 20:01:34 +0300 Subject: fixes #3611 (Tox plugin makes the whole UI hang) --- protocols/Tox/src/stdafx.h | 2 +- protocols/Tox/src/tox_connection.cpp | 20 +++++++++--------- protocols/Tox/src/tox_proto.cpp | 39 ++++++++++++++++++------------------ protocols/Tox/src/tox_proto.h | 36 +++++++++++++++++++++++++++------ protocols/Tox/src/version.h | 6 +++--- 5 files changed, 62 insertions(+), 41 deletions(-) (limited to 'protocols') diff --git a/protocols/Tox/src/stdafx.h b/protocols/Tox/src/stdafx.h index 8e7afcb0be..a30c3155ee 100644 --- a/protocols/Tox/src/stdafx.h +++ b/protocols/Tox/src/stdafx.h @@ -42,7 +42,7 @@ #include #include -struct CToxProto; +class CToxProto; #define now() time(0) diff --git a/protocols/Tox/src/tox_connection.cpp b/protocols/Tox/src/tox_connection.cpp index a602bb88a9..aaa9b0ff29 100644 --- a/protocols/Tox/src/tox_connection.cpp +++ b/protocols/Tox/src/tox_connection.cpp @@ -58,26 +58,24 @@ void CToxProto::CheckConnection() } } -void CToxProto::OnToxCheck(void *arg, uint8_t) +void CToxProto::OnToxCheck() { - CToxProto *proto = (CToxProto*)arg; - if (proto->m_tox == nullptr) + if (m_tox == nullptr) return; // int retriesCount = proto->getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES); - if (proto->m_iStatus < ID_STATUS_ONLINE) - proto->TryConnect(); + if (m_iStatus < ID_STATUS_ONLINE) + TryConnect(); else - proto->CheckConnection(); + CheckConnection(); } -void CToxProto::OnToxPoll(void *arg, uint8_t) +void CToxProto::OnToxPoll() { - CToxProto *proto = (CToxProto*)arg; - if (proto->m_tox) - tox_iterate(proto->m_tox, arg); + if (m_tox) + tox_iterate(m_tox, this); - /*uint32_t interval = tox_iteration_interval(proto->m_tox); + /*uint32_t interval = tox_iteration_interval(m_tox); interval = interval ? interval : TOX_DEFAULT_INTERVAL;*/ diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index d2e236cbd8..b7770dd10b 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -1,11 +1,9 @@ #include "stdafx.h" -CToxProto::CToxProto(const char* protoName, const wchar_t* userName) - : PROTO(protoName, userName), +CToxProto::CToxProto(const char* protoName, const wchar_t* userName) : + PROTO(protoName, userName), m_tox(nullptr), - m_hTimerQueue(nullptr), - m_hPollingTimer(nullptr), - m_hCheckingTimer(nullptr), + m_impl(*this), hMessageProcess(1) { InitNetlib(); @@ -38,13 +36,10 @@ CToxProto::CToxProto(const char* protoName, const wchar_t* userName) HookProtoEvent(ME_CLIST_PREBUILDCONTACTMENU, &CToxProto::OnPrebuildContactMenu); HookProtoEvent(ME_OPT_INITIALISE, &CToxProto::OnOptionsInit); HookProtoEvent(ME_PROTO_ACCLISTCHANGED, &CToxProto::OnAccountRenamed); - - m_hTimerQueue = CreateTimerQueue(); } CToxProto::~CToxProto() { - DeleteTimerQueue(m_hTimerQueue); } void CToxProto::OnModulesLoaded() @@ -154,6 +149,13 @@ HANDLE CToxProto::SendFile(MCONTACT hContact, const wchar_t *msg, wchar_t **ppsz return OnSendFile(m_tox, hContact, msg, ppszFiles); } +///////////////////////////////////////////////////////////////////////////////////////// + +void CToxProto::InitThread(void *) +{ + InitToxCore(m_tox); +} + int CToxProto::SetStatus(int iNewStatus) { if (iNewStatus == m_iDesiredStatus) @@ -168,15 +170,9 @@ int CToxProto::SetStatus(int iNewStatus) // logout if (iNewStatus == ID_STATUS_OFFLINE) { - /*if (m_toxThread != nullptr) { - m_toxThread->Terminate(); - SetEvent(hTerminateEvent); - }*/ - - DeleteTimerQueueTimer(m_hTimerQueue, m_hCheckingTimer, nullptr); - DeleteTimerQueueTimer(m_hTimerQueue, m_hPollingTimer, nullptr); - m_hPollingTimer = nullptr; - m_hCheckingTimer = nullptr; + m_impl.timerPoll.Stop(); + m_impl.timerCheck.Stop(); + if (m_tox) { UninitToxCore(m_tox); tox_kill(m_tox); @@ -218,9 +214,10 @@ int CToxProto::SetStatus(int iNewStatus) return 0; } - InitToxCore(m_tox); - CreateTimerQueueTimer(&m_hPollingTimer, m_hTimerQueue, &CToxProto::OnToxPoll, this, TOX_DEFAULT_INTERVAL, TOX_DEFAULT_INTERVAL, WT_EXECUTEINPERSISTENTTHREAD); - CreateTimerQueueTimer(&m_hCheckingTimer, m_hTimerQueue, &CToxProto::OnToxCheck, this, TOX_CHECKING_INTERVAL, TOX_CHECKING_INTERVAL, WT_EXECUTEINPERSISTENTTHREAD); + m_impl.timerPoll.Start(TOX_DEFAULT_INTERVAL); + m_impl.timerCheck.Start(TOX_CHECKING_INTERVAL); + + ForkThread(&CToxProto::InitThread); return 0; } @@ -232,6 +229,8 @@ int CToxProto::SetStatus(int iNewStatus) return 0; } +///////////////////////////////////////////////////////////////////////////////////////// + HANDLE CToxProto::GetAwayMsg(MCONTACT hContact) { if (IsOnline()) { diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 3bdd6eaaf3..1f90091503 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -1,7 +1,7 @@ #ifndef _TOX_PROTO_H_ #define _TOX_PROTO_H_ -struct CToxProto : public PROTO +class CToxProto : public PROTO { friend class CToxEnterPasswordDlg; friend class CToxCreatePasswordDlg; @@ -9,6 +9,32 @@ struct CToxProto : public PROTO friend class CToxOptionsMain; friend class CToxOptionsNodeList; + class Impl + { + friend class CToxProto; + + CToxProto &m_proto; + CTimer timerCheck, timerPoll; + + void OnCheck(CTimer *) { + m_proto.OnToxCheck(); + } + + void OnPoll(CTimer *) { + m_proto.OnToxPoll(); + } + + Impl(CToxProto &ppro) : + m_proto(ppro), + timerPoll(Miranda_GetSystemWindow(), UINT_PTR(this)), + timerCheck(Miranda_GetSystemWindow(), UINT_PTR(this) + 1) + { + timerPoll.OnEvent = Callback(this, &Impl::OnPoll); + timerCheck.OnEvent = Callback(this, &Impl::OnCheck); + } + } + m_impl; + public: ////////////////////////////////////////////////////////////////////////////////////// // Ctors @@ -70,9 +96,6 @@ private: ULONG hMessageProcess; int m_retriesCount; - HANDLE m_hTimerQueue; - HANDLE m_hPollingTimer; - HANDLE m_hCheckingTimer; static HANDLE hProfileFolderPath; @@ -108,12 +131,13 @@ private: // tox connection bool IsOnline(); + void __cdecl InitThread(void *); void TryConnect(); void CheckConnection(); - static void __stdcall OnToxCheck(void*, uint8_t); - static void __stdcall OnToxPoll(void*, uint8_t); + void OnToxCheck(); + void OnToxPoll(); // accounts int __cdecl OnAccountRenamed(WPARAM, LPARAM); diff --git a/protocols/Tox/src/version.h b/protocols/Tox/src/version.h index e4140a30e9..377016b9ab 100644 --- a/protocols/Tox/src/version.h +++ b/protocols/Tox/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 -#define __MINOR_VERSION 11 -#define __RELEASE_NUM 3 -#define __BUILD_NUM 2 +#define __MINOR_VERSION 96 +#define __RELEASE_NUM 4 +#define __BUILD_NUM 1 #include -- cgit v1.2.3