From 55ac1013817d7b1759e73635c27c02d5a9776390 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 17 Aug 2023 14:29:10 +0300 Subject: fixes #3611 (Tox plugin makes the whole UI hang) --- protocols/Tox/src/tox_connection.cpp | 83 ++++++++++++++++-------------------- protocols/Tox/src/tox_core.cpp | 33 ++++++++++++-- protocols/Tox/src/tox_options.cpp | 2 +- protocols/Tox/src/tox_proto.cpp | 23 ---------- protocols/Tox/src/tox_proto.h | 5 ++- 5 files changed, 69 insertions(+), 77 deletions(-) (limited to 'protocols') diff --git a/protocols/Tox/src/tox_connection.cpp b/protocols/Tox/src/tox_connection.cpp index aaa9b0ff29..6a3d9309df 100644 --- a/protocols/Tox/src/tox_connection.cpp +++ b/protocols/Tox/src/tox_connection.cpp @@ -5,56 +5,48 @@ bool CToxProto::IsOnline() return m_tox && m_iStatus >= ID_STATUS_ONLINE; } -void CToxProto::TryConnect() +///////////////////////////////////////////////////////////////////////////////////////// + +void CToxProto::OnLoggedIn() { - TOX_CONNECTION connectionStatus = tox_self_get_connection_status(m_tox); - if (connectionStatus != TOX_CONNECTION_NONE) { - debugLogA(__FUNCTION__": successfuly connected to DHT"); + debugLogA(__FUNCTION__": successfuly connected to DHT"); - m_iStatus = m_iDesiredStatus; - ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_CONNECTING, m_iStatus); - tox_self_set_status(m_tox, MirandaToToxStatus(m_iStatus)); + m_iStatus = m_iDesiredStatus; + ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_CONNECTING, m_iStatus); + tox_self_set_status(m_tox, MirandaToToxStatus(m_iStatus)); - debugLogA(__FUNCTION__": changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus); + debugLogA(__FUNCTION__": changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus); - UpdateStatusMenu(NULL, NULL); + UpdateStatusMenu(NULL, NULL); - LoadFriendList(m_tox); - return; - } + m_impl.timerPoll.Start(TOX_DEFAULT_INTERVAL); - int maxConnectRetries = getByte("MaxConnectRetries", TOX_MAX_CONNECT_RETRIES); - if (m_iStatus++ > maxConnectRetries) { - SetStatus(ID_STATUS_OFFLINE); - ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_NONETWORK); - debugLogA(__FUNCTION__": failed to connect to DHT"); - return; - } + LoadFriendList(m_tox); } -void CToxProto::CheckConnection() +void CToxProto::OnLoggedFail() { - int maxReconnectRetries = getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES); + int maxConnectRetries = getByte("MaxConnectRetries", TOX_MAX_CONNECT_RETRIES); - TOX_CONNECTION connectionStatus = tox_self_get_connection_status(m_tox); - if (connectionStatus != TOX_CONNECTION_NONE) { - if (m_retriesCount < maxReconnectRetries) { - debugLogA(__FUNCTION__": restored connection with DHT"); - m_retriesCount = maxReconnectRetries; + if (m_iStatus > ID_STATUS_OFFLINE) { + if (m_retriesCount == maxConnectRetries) { + m_retriesCount--; + debugLogA(__FUNCTION__": lost connection with DHT"); + return; } - return; - } - if (m_retriesCount == maxReconnectRetries) { - m_retriesCount--; - debugLogA(__FUNCTION__": lost connection with DHT"); - return; + if (!(--m_retriesCount)) { + debugLogA(__FUNCTION__": disconnected from DHT"); + SetStatus(ID_STATUS_OFFLINE); + return; + } } - - if (!(--m_retriesCount)) { - debugLogA(__FUNCTION__": disconnected from DHT"); - SetStatus(ID_STATUS_OFFLINE); - return; + else { + if (m_iStatus++ > maxConnectRetries) { + SetStatus(ID_STATUS_OFFLINE); + ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_NONETWORK); + debugLogA(__FUNCTION__": failed to connect to DHT"); + } } } @@ -63,20 +55,17 @@ void CToxProto::OnToxCheck() if (m_tox == nullptr) return; - // int retriesCount = proto->getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES); - if (m_iStatus < ID_STATUS_ONLINE) - TryConnect(); - else - CheckConnection(); + int iStatus = tox_self_get_connection_status(m_tox); + if (iStatus == TOX_CONNECTION_NONE) + OnLoggedFail(); + else if (iStatus != m_prevToxStatus) + OnLoggedIn(); + + m_prevToxStatus = iStatus; } void CToxProto::OnToxPoll() { if (m_tox) tox_iterate(m_tox, this); - - /*uint32_t interval = tox_iteration_interval(m_tox); - interval = interval - ? interval - : TOX_DEFAULT_INTERVAL;*/ } diff --git a/protocols/Tox/src/tox_core.cpp b/protocols/Tox/src/tox_core.cpp index df7ec7bb43..c9888f34cf 100644 --- a/protocols/Tox/src/tox_core.cpp +++ b/protocols/Tox/src/tox_core.cpp @@ -46,6 +46,32 @@ Tox_Options* CToxProto::GetToxOptions() return nullptr; } +///////////////////////////////////////////////////////////////////////////////////////// + +void CToxProto::InitThread(void *) +{ + Tox_Options *options = GetToxOptions(); + + TOX_ERR_NEW error; + m_tox = tox_new(options, &error); + tox_options_free(options); + if (error != TOX_ERR_NEW_OK) { + debugLogA(__FUNCTION__": failed to initialize tox core (%d)", error); + m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; + ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr); + ShowNotification(TranslateT("Unable to initialize Tox core"), ToxErrorToString(error), MB_ICONERROR); + return; + } + + InitToxCore(m_tox); + BootstrapNodes(m_tox); + + m_prevToxStatus = TOX_CONNECTION_NONE; + m_impl.timerCheck.StartSafe(TOX_CHECKING_INTERVAL); +} + +///////////////////////////////////////////////////////////////////////////////////////// + void CToxProto::InitToxCore(Tox *tox) { debugLogA(__FUNCTION__": initializing tox core"); @@ -57,11 +83,12 @@ void CToxProto::InitToxCore(Tox *tox) tox_callback_friend_message(tox, OnFriendMessage); tox_callback_friend_read_receipt(tox, OnReadReceipt); tox_callback_friend_typing(tox, OnTypingChanged); - // + tox_callback_friend_name(tox, OnFriendNameChange); tox_callback_friend_status_message(tox, OnStatusMessageChanged); tox_callback_friend_status(tox, OnUserStatusChanged); tox_callback_friend_connection_status(tox, OnConnectionStatusChanged); + // transfers tox_callback_file_recv_control(tox, OnFileRequest); tox_callback_file_recv(tox, OnFriendFile); @@ -92,8 +119,6 @@ void CToxProto::InitToxCore(Tox *tox) tox_self_set_status_message(tox, (uint8_t*)(char*)statusMessage, mir_strlen(statusMessage), &setInfoError); if (setInfoError != TOX_ERR_SET_INFO_OK) debugLogA(__FUNCTION__": failed to set self status message (%d)", setInfoError); - - BootstrapNodes(tox); } void CToxProto::UninitToxCore(Tox *tox) @@ -108,4 +133,4 @@ void CToxProto::OnToxLog(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t l if (level == TOX_LOG_LEVEL_ERROR) proto->debugLogA("TOXCORE: %s at %s(...) in %s:%u", message, func, file, line); -} \ No newline at end of file +} diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index c8803d0f48..befeb245eb 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -121,7 +121,7 @@ void CToxOptionsMain::ProfileCreate_OnClick(CCtrlButton*) tox_options_default(options); Tox *tox = tox_new(options, nullptr); m_proto->InitToxCore(tox); - m_proto->UninitToxCore(tox); + m_proto->SaveToxProfile(tox); tox_kill(tox); m_toxAddress.Enable(); diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index b7770dd10b..f8f9ec4001 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -149,13 +149,6 @@ 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) @@ -201,22 +194,6 @@ int CToxProto::SetStatus(int iNewStatus) m_retriesCount = getByte("MaxConnectRetries", TOX_MAX_CONNECT_RETRIES); - Tox_Options *options = GetToxOptions(); - - TOX_ERR_NEW error; - m_tox = tox_new(options, &error); - tox_options_free(options); - if (error != TOX_ERR_NEW_OK) { - debugLogA(__FUNCTION__": failed to initialize tox core (%d)", error); - m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; - ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr); - ShowNotification(TranslateT("Unable to initialize Tox core"), ToxErrorToString(error), MB_ICONERROR); - return 0; - } - - m_impl.timerPoll.Start(TOX_DEFAULT_INTERVAL); - m_impl.timerCheck.Start(TOX_CHECKING_INTERVAL); - ForkThread(&CToxProto::InitThread); return 0; } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 1f90091503..362f8980df 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -96,6 +96,7 @@ private: ULONG hMessageProcess; int m_retriesCount; + int m_prevToxStatus = TOX_CONNECTION_NONE; static HANDLE hProfileFolderPath; @@ -133,8 +134,8 @@ private: bool IsOnline(); void __cdecl InitThread(void *); - void TryConnect(); - void CheckConnection(); + void OnLoggedIn(); + void OnLoggedFail(); void OnToxCheck(); void OnToxPoll(); -- cgit v1.2.3