From c31a30a806845a930087834a9b9667abccba156f Mon Sep 17 00:00:00 2001 From: aunsane Date: Thu, 30 Aug 2018 23:35:31 +0300 Subject: Tox: using timers instead of threads --- protocols/Tox/src/stdafx.h | 1 - protocols/Tox/src/tox_avatars.cpp | 4 +- protocols/Tox/src/tox_connection.cpp | 117 +++++++++++------------------------ protocols/Tox/src/tox_contacts.cpp | 16 ++--- protocols/Tox/src/tox_core.cpp | 29 +++++---- protocols/Tox/src/tox_messages.cpp | 8 +-- protocols/Tox/src/tox_options.cpp | 21 ++++--- protocols/Tox/src/tox_profile.cpp | 6 +- protocols/Tox/src/tox_proto.cpp | 51 +++++++++++---- protocols/Tox/src/tox_proto.h | 18 +++--- protocols/Tox/src/tox_services.cpp | 2 +- protocols/Tox/src/tox_thread.h | 43 ------------- protocols/Tox/src/tox_transfer.cpp | 16 ++--- 13 files changed, 135 insertions(+), 197 deletions(-) delete mode 100644 protocols/Tox/src/tox_thread.h (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/stdafx.h b/protocols/Tox/src/stdafx.h index 1e444e9b9d..b1d360aae5 100644 --- a/protocols/Tox/src/stdafx.h +++ b/protocols/Tox/src/stdafx.h @@ -79,7 +79,6 @@ struct CToxProto; #include "version.h" #include "resource.h" #include "tox_menus.h" -#include "tox_thread.h" #include "tox_address.h" #include "tox_dialogs.h" #include "tox_profile.h" diff --git a/protocols/Tox/src/tox_avatars.cpp b/protocols/Tox/src/tox_avatars.cpp index eda669fb7e..0e1aa6aa97 100644 --- a/protocols/Tox/src/tox_avatars.cpp +++ b/protocols/Tox/src/tox_avatars.cpp @@ -77,7 +77,7 @@ void CToxProto::SetToxAvatar(const wchar_t* path) debugLogA(__FUNCTION__": send avatar to friend (%d)", friendNumber); TOX_ERR_FILE_SEND error; - uint32_t fileNumber = tox_file_send(m_toxThread->Tox(), friendNumber, TOX_FILE_KIND_AVATAR, length, hash, nullptr, 0, &error); + uint32_t fileNumber = tox_file_send(m_tox, friendNumber, TOX_FILE_KIND_AVATAR, length, hash, nullptr, 0, &error); if (error != TOX_ERR_FILE_SEND_OK) { mir_free(data); debugLogA(__FUNCTION__": failed to set new avatar (%d)", error); @@ -173,7 +173,7 @@ INT_PTR CToxProto::SetMyAvatar(WPARAM, LPARAM lParam) debugLogA(__FUNCTION__": unset avatar for friend (%d)", friendNumber); TOX_ERR_FILE_SEND error; - tox_file_send(m_toxThread->Tox(), friendNumber, TOX_FILE_KIND_AVATAR, 0, nullptr, nullptr, 0, &error); + tox_file_send(m_tox, friendNumber, TOX_FILE_KIND_AVATAR, 0, nullptr, nullptr, 0, &error); if (error != TOX_ERR_FILE_SEND_OK) { debugLogA(__FUNCTION__": failed to unset avatar (%d)", error); return 0; diff --git a/protocols/Tox/src/tox_connection.cpp b/protocols/Tox/src/tox_connection.cpp index 41fca21d6e..a175126a9a 100644 --- a/protocols/Tox/src/tox_connection.cpp +++ b/protocols/Tox/src/tox_connection.cpp @@ -2,24 +2,24 @@ bool CToxProto::IsOnline() { - return m_toxThread && m_iStatus >= ID_STATUS_ONLINE; + return m_tox && m_iStatus >= ID_STATUS_ONLINE; } -void CToxProto::TryConnect(Tox *tox) +void CToxProto::TryConnect() { - TOX_CONNECTION connectionStatus = tox_self_get_connection_status(tox); + TOX_CONNECTION connectionStatus = tox_self_get_connection_status(m_tox); if (connectionStatus != TOX_CONNECTION_NONE) { 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(tox, MirandaToToxStatus(m_iStatus)); + tox_self_set_status(m_tox, MirandaToToxStatus(m_iStatus)); debugLogA(__FUNCTION__": changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus); UpdateStatusMenu(NULL, NULL); - LoadFriendList(tox); + LoadFriendList(m_tox); return; } @@ -32,97 +32,50 @@ void CToxProto::TryConnect(Tox *tox) } } -void CToxProto::CheckConnection(Tox *tox, int &retriesCount) +void CToxProto::CheckConnection() { int maxReconnectRetries = getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES); - TOX_CONNECTION connectionStatus = tox_self_get_connection_status(tox); + + TOX_CONNECTION connectionStatus = tox_self_get_connection_status(m_tox); if (connectionStatus != TOX_CONNECTION_NONE) { - if (retriesCount < maxReconnectRetries) { + if (m_retriesCount < maxReconnectRetries) { debugLogA(__FUNCTION__": restored connection with DHT"); - retriesCount = maxReconnectRetries; - } - } - else { - if (retriesCount == maxReconnectRetries) { - retriesCount--; - debugLogA(__FUNCTION__": lost connection with DHT"); - } - else if (!(--retriesCount)) { - debugLogA(__FUNCTION__": disconnected from DHT"); - SetStatus(ID_STATUS_OFFLINE); - return; + m_retriesCount = maxReconnectRetries; } + return; } -} - -void CToxProto::CheckingThread(void *arg) -{ - Thread_SetName(MODULE ": CheckingThread"); - - debugLogA(__FUNCTION__": entering"); - - CToxThread *thread = (CToxThread*)arg; - int retriesCount = getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES); - while (!thread->IsTerminated()) { - if (m_iStatus < ID_STATUS_ONLINE) - TryConnect(thread->Tox()); - else - CheckConnection(thread->Tox(), retriesCount); - - WaitForSingleObject(hTerminateEvent, TOX_CHECKING_INTERVAL); - } - - hCheckingThread = nullptr; - debugLogA(__FUNCTION__": leaving"); -} - -void CToxProto::PollingThread(void*) -{ - Thread_SetName(MODULE ": PollingThread"); - - debugLogA(__FUNCTION__": entering"); - - Tox_Options *options = GetToxOptions(); - if (!options) { - SetStatus(ID_STATUS_OFFLINE); - ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr); - debugLogA(__FUNCTION__": leaving"); + if (m_retriesCount == maxReconnectRetries) { + m_retriesCount--; + debugLogA(__FUNCTION__": lost connection with DHT"); return; } - TOX_ERR_NEW error; - CToxThread toxThread(options, &error); - if (error != TOX_ERR_NEW_OK) { + if (!(--m_retriesCount)) { + debugLogA(__FUNCTION__": disconnected from DHT"); SetStatus(ID_STATUS_OFFLINE); - debugLogA(__FUNCTION__": failed to initialize tox core (%d)", error); - ShowNotification(TranslateT("Unable to initialize Tox core"), ToxErrorToString(error), MB_ICONERROR); - tox_options_free(options); - debugLogA(__FUNCTION__": leaving"); return; } - tox_options_free(options); - - m_toxThread = &toxThread; - InitToxCore(toxThread.Tox()); - BootstrapNodes(toxThread.Tox()); - hCheckingThread = ForkThreadEx(&CToxProto::CheckingThread, &toxThread, nullptr); +} - while (!toxThread.IsTerminated()) { - tox_iterate(toxThread.Tox(), this); - uint32_t interval = tox_iteration_interval(toxThread.Tox()); - interval = interval - ? interval - : TOX_DEFAULT_INTERVAL; - WaitForSingleObject(hTerminateEvent, interval); - } +void CToxProto::OnToxCheck(void *arg, BYTE) +{ + CToxProto *proto = (CToxProto*)arg; - SetEvent(hTerminateEvent); - WaitForSingleObject(hCheckingThread, TOX_CHECKING_INTERVAL); + int retriesCount = proto->getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES); + if (proto->m_iStatus < ID_STATUS_ONLINE) + proto->TryConnect(); + else + proto->CheckConnection(); +} - UninitToxCore(toxThread.Tox()); - m_toxThread = nullptr; - hPollingThread = nullptr; +void CToxProto::OnToxPoll(void *arg, BYTE) +{ + CToxProto *proto = (CToxProto*)arg; - debugLogA(__FUNCTION__": leaving"); -} \ No newline at end of file + tox_iterate(proto->m_tox, arg); + /*uint32_t interval = tox_iteration_interval(proto->m_tox); + interval = interval + ? interval + : TOX_DEFAULT_INTERVAL;*/ +} diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 298f9256a7..0954745f34 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -105,7 +105,7 @@ uint32_t CToxProto::GetToxFriendNumber(MCONTACT hContact) { ToxBinAddress pubKey(ptrA(getStringA(hContact, TOX_SETTINGS_ID))); TOX_ERR_FRIEND_BY_PUBLIC_KEY error; - uint32_t friendNumber = tox_friend_by_public_key(m_toxThread->Tox(), pubKey.GetPubKey(), &error); + uint32_t friendNumber = tox_friend_by_public_key(m_tox, pubKey.GetPubKey(), &error); if (error != TOX_ERR_FRIEND_BY_PUBLIC_KEY_OK) debugLogA(__FUNCTION__": failed to get friend number (%d)", error); return friendNumber; @@ -159,7 +159,7 @@ INT_PTR CToxProto::OnRequestAuth(WPARAM hContact, LPARAM lParam) ToxBinAddress address(ptrA(getStringA(hContact, TOX_SETTINGS_ID))); TOX_ERR_FRIEND_ADD addFriendResult; - /*int32_t friendNumber = */tox_friend_add(m_toxThread->Tox(), address, (uint8_t*)reason, length, &addFriendResult); + /*int32_t friendNumber = */tox_friend_add(m_tox, address, (uint8_t*)reason, length, &addFriendResult); if (addFriendResult != TOX_ERR_FRIEND_ADD_OK) { debugLogA(__FUNCTION__": failed to request auth(%d)", addFriendResult); return addFriendResult; @@ -185,7 +185,7 @@ INT_PTR CToxProto::OnGrantAuth(WPARAM hContact, LPARAM) ToxBinAddress pubKey(ptrA(getStringA(hContact, TOX_SETTINGS_ID))); TOX_ERR_FRIEND_ADD error; - tox_friend_add_norequest(m_toxThread->Tox(), pubKey, &error); + tox_friend_add_norequest(m_tox, pubKey, &error); if (error != TOX_ERR_FRIEND_ADD_OK) { debugLogA(__FUNCTION__": failed to grant auth (%d)", error); return error; @@ -194,7 +194,7 @@ INT_PTR CToxProto::OnGrantAuth(WPARAM hContact, LPARAM) db_unset(hContact, "CList", "NotOnList"); delSetting(hContact, "Grant"); - SaveToxProfile(m_toxThread->Tox()); + SaveToxProfile(m_tox); return 0; } @@ -207,11 +207,11 @@ void CToxProto::OnContactDeleted(MCONTACT hContact) if (!isChatRoom(hContact)) { int32_t friendNumber = GetToxFriendNumber(hContact); TOX_ERR_FRIEND_DELETE error; - if (!tox_friend_delete(m_toxThread->Tox(), friendNumber, &error)) { + if (!tox_friend_delete(m_tox, friendNumber, &error)) { debugLogA(__FUNCTION__": failed to delete friend (%d)", error); return; } - SaveToxProfile(m_toxThread->Tox()); + SaveToxProfile(m_tox); } } @@ -321,7 +321,7 @@ void CToxProto::OnConnectionStatusChanged(Tox *tox, uint32_t friendNumber, TOX_C proto->debugLogA(__FUNCTION__": send avatar to friend (%d)", friendNumber); TOX_ERR_FILE_SEND error; - uint32_t fileNumber = tox_file_send(proto->m_toxThread->Tox(), friendNumber, TOX_FILE_KIND_AVATAR, length, hash, nullptr, 0, &error); + uint32_t fileNumber = tox_file_send(proto->m_tox, friendNumber, TOX_FILE_KIND_AVATAR, length, hash, nullptr, 0, &error); if (error != TOX_ERR_FILE_SEND_OK) { Netlib_Logf(proto->m_hNetlibUser, __FUNCTION__": failed to set new avatar"); fclose(hFile); @@ -337,7 +337,7 @@ void CToxProto::OnConnectionStatusChanged(Tox *tox, uint32_t friendNumber, TOX_C } else { proto->debugLogA(__FUNCTION__": unset avatar for friend (%d)", friendNumber); - tox_file_send(proto->m_toxThread->Tox(), friendNumber, TOX_FILE_KIND_AVATAR, 0, nullptr, nullptr, 0, nullptr); + tox_file_send(proto->m_tox, friendNumber, TOX_FILE_KIND_AVATAR, 0, nullptr, nullptr, 0, nullptr); } } diff --git a/protocols/Tox/src/tox_core.cpp b/protocols/Tox/src/tox_core.cpp index b1139c6c4f..df7ec7bb43 100644 --- a/protocols/Tox/src/tox_core.cpp +++ b/protocols/Tox/src/tox_core.cpp @@ -50,6 +50,9 @@ void CToxProto::InitToxCore(Tox *tox) { debugLogA(__FUNCTION__": initializing tox core"); + if (tox == nullptr) + return; + tox_callback_friend_request(tox, OnFriendRequest); tox_callback_friend_message(tox, OnFriendMessage); tox_callback_friend_read_receipt(tox, OnReadReceipt); @@ -70,25 +73,27 @@ void CToxProto::InitToxCore(Tox *tox) ToxHexAddress address(data); setString(TOX_SETTINGS_ID, address); - TOX_ERR_SET_INFO error; + TOX_ERR_SET_INFO setInfoError; /*uint8_t nick[TOX_MAX_NAME_LENGTH] = { 0 }; tox_self_get_name(toxThread->Tox(), nick); setWString("Nick", ptrW(mir_utf8decodeW((char*)nick)));*/ ptrA nick(mir_utf8encodeW(ptrW(getWStringA("Nick")))); - tox_self_set_name(tox, (uint8_t*)(char*)nick, mir_strlen(nick), &error); - if (error != TOX_ERR_SET_INFO_OK) - debugLogA(__FUNCTION__": failed to set self name (%d)", error); + tox_self_set_name(tox, (uint8_t*)(char*)nick, mir_strlen(nick), &setInfoError); + if (setInfoError != TOX_ERR_SET_INFO_OK) + debugLogA(__FUNCTION__": failed to set self name (%d)", setInfoError); /*uint8_t statusMessage[TOX_MAX_STATUS_MESSAGE_LENGTH] = { 0 }; tox_self_get_status_message(toxThread->Tox(), statusMessage); setWString("StatusMsg", ptrW(mir_utf8decodeW((char*)statusMessage)));*/ ptrA statusMessage(mir_utf8encodeW(ptrW(getWStringA("StatusMsg")))); - tox_self_set_status_message(tox, (uint8_t*)(char*)statusMessage, mir_strlen(statusMessage), &error); - if (error != TOX_ERR_SET_INFO_OK) - debugLogA(__FUNCTION__": failed to set self status message (%d)", error); + 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) @@ -101,12 +106,6 @@ void CToxProto::OnToxLog(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t l { CToxProto *proto = (CToxProto*)user_data; -#ifdef _DEBUG - if (level < TOX_LOG_LEVEL_INFO) -#else - if (level < TOX_LOG_LEVEL_ERROR) -#endif - return; - - proto->debugLogA("TOXCORE: %s at %s(...) in %s:%u", message, func, file, line); + 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_messages.cpp b/protocols/Tox/src/tox_messages.cpp index 3ca2d81ca6..7d53d4b19e 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -96,7 +96,7 @@ void CToxProto::SendMessageAsync(void *arg) } TOX_ERR_FRIEND_SEND_MESSAGE sendError; - int messageNumber = tox_friend_send_message(m_toxThread->Tox(), friendNumber, type, msg, msgLen, &sendError); + int messageNumber = tox_friend_send_message(m_tox, friendNumber, type, msg, msgLen, &sendError); if (sendError != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { debugLogA(__FUNCTION__": failed to send message for %d (%d)", friendNumber, sendError); ProtoBroadcastAck(param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)param->hMessage, (LPARAM)_T2A(ToxErrorToString(sendError))); @@ -176,7 +176,7 @@ void CToxProto::GetStatusMessageAsync(void* arg) } TOX_ERR_FRIEND_QUERY error; - size_t size = tox_friend_get_status_message_size(m_toxThread->Tox(), friendNumber, &error); + size_t size = tox_friend_get_status_message_size(m_tox, friendNumber, &error); if (error != TOX_ERR_FRIEND_QUERY::TOX_ERR_FRIEND_QUERY_OK) { debugLogA(__FUNCTION__": failed to get status message for (%d) (%d)", friendNumber, error); ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_FAILED, (HANDLE)hContact, 0); @@ -184,7 +184,7 @@ void CToxProto::GetStatusMessageAsync(void* arg) } ptrA statusMessage((char*)mir_calloc(size + 1)); - if (!tox_friend_get_status_message(m_toxThread->Tox(), friendNumber, (uint8_t*)(char*)statusMessage, &error)) { + if (!tox_friend_get_status_message(m_tox, friendNumber, (uint8_t*)(char*)statusMessage, &error)) { debugLogA(__FUNCTION__": failed to get status message for (%d) (%d)", friendNumber, error); ProtoBroadcastAck(hContact, ACKTYPE_AWAYMSG, ACKRESULT_FAILED, (HANDLE)hContact, 0); return; @@ -202,7 +202,7 @@ int CToxProto::OnUserIsTyping(MCONTACT hContact, int type) return 0; TOX_ERR_SET_TYPING error; - if (!tox_self_set_typing(m_toxThread->Tox(), friendNumber, type == PROTOTYPE_SELFTYPING_ON, &error)) + if (!tox_self_set_typing(m_tox, friendNumber, type == PROTOTYPE_SELFTYPING_ON, &error)) debugLogA(__FUNCTION__": failed to send typing (%d)", error); return 0; diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 682ee2cc5d..d3fb835dfb 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -113,11 +113,6 @@ void CToxOptionsMain::ToxAddressCopy_OnClick(CCtrlButton*) void CToxOptionsMain::ProfileCreate_OnClick(CCtrlButton*) { - Tox_Options *options = nullptr; - tox_options_default(options); - Tox *tox = tox_new(options, nullptr); - tox_options_free(options); - ptrW profilePath(m_proto->GetToxProfilePath()); if (!m_proto->IsFileExists(profilePath)) { HANDLE hProfile = CreateFile(profilePath, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); @@ -128,6 +123,9 @@ void CToxOptionsMain::ProfileCreate_OnClick(CCtrlButton*) CloseHandle(hProfile); } + Tox_Options *options = nullptr; + tox_options_default(options); + Tox *tox = tox_new(options, nullptr); m_proto->InitToxCore(tox); m_proto->UninitToxCore(tox); tox_kill(tox); @@ -171,10 +169,11 @@ void CToxOptionsMain::ProfileImport_OnClick(CCtrlButton*) Tox_Options *options = tox_options_new(nullptr); if (m_proto->LoadToxProfile(options)) { - CToxThread toxThread(options); + Tox *tox = tox_new(options, nullptr); + tox_options_free(options); uint8_t data[TOX_ADDRESS_SIZE]; - tox_self_get_address(toxThread.Tox(), data); + tox_self_get_address(tox, data); ToxHexAddress address(data); m_proto->setString(TOX_SETTINGS_ID, address); @@ -182,13 +181,13 @@ void CToxOptionsMain::ProfileImport_OnClick(CCtrlButton*) m_toxAddress.SetTextA(address); uint8_t nick[TOX_MAX_NAME_LENGTH] = { 0 }; - tox_self_get_name(toxThread.Tox(), nick); + tox_self_get_name(tox, nick); ptrW nickname(mir_utf8decodeW((char*)nick)); m_proto->setWString("Nick", nickname); m_nickname.SetText(nickname); uint8_t statusMessage[TOX_MAX_STATUS_MESSAGE_LENGTH] = { 0 }; - tox_self_get_status_message(toxThread.Tox(), statusMessage); + tox_self_get_status_message(tox, statusMessage); m_proto->setWString("StatusMsg", ptrW(mir_utf8decodeW((char*)statusMessage))); ShowWindow(m_profileCreate.GetHwnd(), FALSE); @@ -196,6 +195,8 @@ void CToxOptionsMain::ProfileImport_OnClick(CCtrlButton*) ShowWindow(m_toxAddressCopy.GetHwnd(), TRUE); ShowWindow(m_profileExport.GetHwnd(), TRUE); + + tox_kill(tox); } tox_options_free(options); } @@ -241,7 +242,7 @@ bool CToxOptionsMain::OnApply() // todo: add checkbox //m_proto->setWString("Password", pass_ptrW(m_password.GetText())); - m_proto->SaveToxProfile(m_proto->m_toxThread->Tox()); + m_proto->SaveToxProfile(m_proto->m_tox); } return true; } diff --git a/protocols/Tox/src/tox_profile.cpp b/protocols/Tox/src/tox_profile.cpp index d90c1a08f9..c514dacf3c 100644 --- a/protocols/Tox/src/tox_profile.cpp +++ b/protocols/Tox/src/tox_profile.cpp @@ -184,7 +184,7 @@ INT_PTR CToxProto::OnRemovePassword(WPARAM, LPARAM) int result = MessageBox(nullptr, message, TranslateT("Remove password"), MB_YESNO | MB_ICONQUESTION); if (result == IDYES) { delSetting(TOX_SETTINGS_PASSWORD); - SaveToxProfile(m_toxThread->Tox()); + SaveToxProfile(m_tox); } return 0; } @@ -266,7 +266,7 @@ void CToxCreatePasswordDlg::Password_OnChange(CCtrlBase*) void CToxCreatePasswordDlg::OnOk(CCtrlButton*) { m_proto->setWString(TOX_SETTINGS_PASSWORD, pass_ptrW(m_newPassword.GetText())); - m_proto->SaveToxProfile(m_proto->m_toxThread->Tox()); + m_proto->SaveToxProfile(m_proto->m_tox); EndDialog(m_hwnd, 1); } @@ -329,6 +329,6 @@ void CToxChangePasswordDlg::Password_OnChange(CCtrlBase*) void CToxChangePasswordDlg::OnOk(CCtrlButton*) { m_proto->setWString(TOX_SETTINGS_PASSWORD, pass_ptrW(m_newPassword.GetText())); - m_proto->SaveToxProfile(m_proto->m_toxThread->Tox()); + m_proto->SaveToxProfile(m_proto->m_tox); EndDialog(m_hwnd, 1); } diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 8cdcd6c56f..144b2dc69b 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -2,9 +2,10 @@ CToxProto::CToxProto(const char* protoName, const wchar_t* userName) : PROTO(protoName, userName), - m_toxThread(nullptr), - hCheckingThread(nullptr), - hPollingThread(nullptr), + m_tox(nullptr), + m_hTimerQueue(nullptr), + m_hPollingTimer(nullptr), + m_hCheckingTimer(nullptr), hMessageProcess(1) { InitNetlib(); @@ -37,11 +38,12 @@ CToxProto::CToxProto(const char* protoName, const wchar_t* userName) HookProtoEvent(ME_CLIST_PREBUILDCONTACTMENU, &CToxProto::OnPrebuildContactMenu); HookProtoEvent(ME_PROTO_ACCLISTCHANGED, &CToxProto::OnAccountRenamed); - hTerminateEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); + m_hTimerQueue = CreateTimerQueue(); } CToxProto::~CToxProto() { + DeleteTimerQueue(m_hTimerQueue); UninitNetlib(); } @@ -112,7 +114,7 @@ int CToxProto::AuthRequest(MCONTACT hContact, const wchar_t *szMessage) HANDLE CToxProto::FileAllow(MCONTACT hContact, HANDLE hTransfer, const wchar_t *tszPath) { - return OnFileAllow(m_toxThread->Tox(), hContact, hTransfer, tszPath); + return OnFileAllow(m_tox, hContact, hTransfer, tszPath); } int CToxProto::FileCancel(MCONTACT hContact, HANDLE hTransfer) @@ -127,7 +129,7 @@ int CToxProto::FileDeny(MCONTACT hContact, HANDLE hTransfer, const wchar_t*) int CToxProto::FileResume(HANDLE hTransfer, int *action, const wchar_t **szFilename) { - return OnFileResume(m_toxThread->Tox(), hTransfer, action, szFilename); + return OnFileResume(m_tox, hTransfer, action, szFilename); } HWND CToxProto::SearchAdvanced(HWND owner) @@ -147,7 +149,7 @@ int CToxProto::SendMsg(MCONTACT hContact, int, const char *msg) HANDLE CToxProto::SendFile(MCONTACT hContact, const wchar_t *msg, wchar_t **ppszFiles) { - return OnSendFile(m_toxThread->Tox(), hContact, msg, ppszFiles); + return OnSendFile(m_tox, hContact, msg, ppszFiles); } int CToxProto::SetStatus(int iNewStatus) @@ -164,9 +166,19 @@ int CToxProto::SetStatus(int iNewStatus) // logout if (iNewStatus == ID_STATUS_OFFLINE) { - if (m_toxThread != nullptr) { + /*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; + if (m_tox) { + UninitToxCore(m_tox); + tox_kill(m_tox); + m_tox = nullptr; } if (!Miranda_IsTerminated()) { @@ -189,13 +201,30 @@ int CToxProto::SetStatus(int iNewStatus) m_iStatus = ID_STATUS_CONNECTING; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); - hPollingThread = ForkThreadEx(&CToxProto::PollingThread, nullptr, nullptr); + 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; + } + + 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); return 0; } // change status m_iStatus = iNewStatus; - tox_self_set_status(m_toxThread->Tox(), MirandaToToxStatus(iNewStatus)); + tox_self_set_status(m_tox, MirandaToToxStatus(iNewStatus)); ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); return 0; @@ -216,7 +245,7 @@ int CToxProto::SetAwayMsg(int, const wchar_t *msg) if (IsOnline()) { T2Utf statusMessage(msg); TOX_ERR_SET_INFO error; - if (!tox_self_set_status_message(m_toxThread->Tox(), (uint8_t*)(char*)statusMessage, min(TOX_MAX_STATUS_MESSAGE_LENGTH, mir_strlen(statusMessage)), &error)) + if (!tox_self_set_status_message(m_tox, (uint8_t*)(char*)statusMessage, min(TOX_MAX_STATUS_MESSAGE_LENGTH, mir_strlen(statusMessage)), &error)) debugLogA(__FUNCTION__": failed to set status status message %s (%d)", msg, error); } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 9ea063c4ab..c7c41174c2 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -60,7 +60,7 @@ public: static INT_PTR ParseToxUri(WPARAM, LPARAM lParam); private: - CToxThread *m_toxThread; + Tox *m_tox; mir_cs m_profileLock; ptrW m_accountName; ptrW m_defaultGroup; @@ -68,10 +68,10 @@ private: CTransferList transfers; ULONG hMessageProcess; - HANDLE hConnectingThread; - HANDLE hCheckingThread; - HANDLE hPollingThread; - HANDLE hTerminateEvent; + int m_retriesCount; + HANDLE m_hTimerQueue; + HANDLE m_hPollingTimer; + HANDLE m_hCheckingTimer; static HANDLE hProfileFolderPath; @@ -108,11 +108,11 @@ private: // tox connection bool IsOnline(); - void TryConnect(Tox *tox); - void CheckConnection(Tox *tox, int &retriesCount); + void TryConnect(); + void CheckConnection(); - void __cdecl CheckingThread(void*); - void __cdecl PollingThread(void*); + static void __stdcall OnToxCheck(void*, BYTE); + static void __stdcall OnToxPoll(void*, BYTE); // accounts int __cdecl OnAccountRenamed(WPARAM, LPARAM); diff --git a/protocols/Tox/src/tox_services.cpp b/protocols/Tox/src/tox_services.cpp index a2c9e549a9..08d9013c02 100644 --- a/protocols/Tox/src/tox_services.cpp +++ b/protocols/Tox/src/tox_services.cpp @@ -8,7 +8,7 @@ INT_PTR CToxProto::SetMyNickname(WPARAM wParam, LPARAM lParam) if (IsOnline()) { T2Utf szNick8(nickname); TOX_ERR_SET_INFO error; - if (!tox_self_set_name(m_toxThread->Tox(), szNick8, mir_strlen(szNick8), &error)) + if (!tox_self_set_name(m_tox, szNick8, mir_strlen(szNick8), &error)) debugLogA(__FUNCTION__": failed to set nick name"); } diff --git a/protocols/Tox/src/tox_thread.h b/protocols/Tox/src/tox_thread.h deleted file mode 100644 index a56de4bab2..0000000000 --- a/protocols/Tox/src/tox_thread.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef _TOX_THREAD_H_ -#define _TOX_THREAD_H_ - -class CToxThread -{ -private: - Tox *tox; - bool isTerminated; - -public: - CToxThread(Tox_Options *options, TOX_ERR_NEW *error = nullptr) - : tox(nullptr), isTerminated(false) - { - tox = tox_new(options, error); - } - - ~CToxThread() - { - - if (tox) - { - tox_kill(tox); - tox = nullptr; - } - } - - Tox* Tox() - { - return tox; - } - - bool IsTerminated() - { - return isTerminated; - } - - void Terminate() - { - isTerminated = true; - } -}; - -#endif //_TOX_THREAD_H_ \ No newline at end of file diff --git a/protocols/Tox/src/tox_transfer.cpp b/protocols/Tox/src/tox_transfer.cpp index 1b5e747b04..9558633893 100644 --- a/protocols/Tox/src/tox_transfer.cpp +++ b/protocols/Tox/src/tox_transfer.cpp @@ -167,7 +167,7 @@ void CToxProto::OnDataReceiving(Tox *tox, uint32_t friendNumber, uint32_t fileNu if (fwrite(data, sizeof(uint8_t), length, transfer->hFile) != length) { proto->debugLogA(__FUNCTION__": failed write to file (%d)", fileNumber); - tox_file_control(proto->m_toxThread->Tox(), friendNumber, fileNumber, TOX_FILE_CONTROL_CANCEL, nullptr); + tox_file_control(proto->m_tox, friendNumber, fileNumber, TOX_FILE_CONTROL_CANCEL, nullptr); proto->ProtoBroadcastAck(transfer->pfts.hContact, ACKTYPE_FILE, ACKRESULT_FAILED, (HANDLE)transfer, 0); proto->transfers.Remove(transfer); return; @@ -330,7 +330,7 @@ void CToxProto::OnFileSendData(Tox *tox, uint32_t friendNumber, uint32_t fileNum } TOX_ERR_FILE_SEND_CHUNK error; - if (!tox_file_send_chunk(proto->m_toxThread->Tox(), friendNumber, fileNumber, position, data, length, &error)) { + if (!tox_file_send_chunk(proto->m_tox, friendNumber, fileNumber, position, data, length, &error)) { if (error == TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_CONNECTED) return; proto->debugLogA(__FUNCTION__": failed to send file chunk (%d) to %s (%d) cause (%d)", fileNumber, (const char*)pubKey, friendNumber, error); @@ -350,7 +350,7 @@ int CToxProto::CancelTransfer(MCONTACT, HANDLE hTransfer) { FileTransferParam *transfer = (FileTransferParam*)hTransfer; debugLogA(__FUNCTION__": transfer (%d) is canceled", transfer->fileNumber); - tox_file_control(m_toxThread->Tox(), transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_CANCEL, nullptr); + tox_file_control(m_tox, transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_CANCEL, nullptr); transfers.Remove(transfer); return 0; @@ -362,13 +362,13 @@ void CToxProto::PauseOutgoingTransfers(uint32_t friendNumber) // only for sending FileTransferParam *transfer = transfers.GetAt(i); if (transfer->friendNumber == friendNumber && transfer->GetDirection() == 0) { - ToxHexAddress pubKey = GetContactPublicKey(m_toxThread->Tox(), friendNumber); + ToxHexAddress pubKey = GetContactPublicKey(m_tox, friendNumber); debugLogA(__FUNCTION__": sending ask to pause the transfer of file (%d) to %s (%d)", transfer->fileNumber, (const char*)pubKey, transfer->friendNumber); TOX_ERR_FILE_CONTROL error; - if (!tox_file_control(m_toxThread->Tox(), transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_PAUSE, &error)) { + if (!tox_file_control(m_tox, transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_PAUSE, &error)) { debugLogA(__FUNCTION__": failed to pause the transfer (%d) to %s (%d) cause(%d)", transfer->fileNumber, (const char*)pubKey, transfer->friendNumber, error); - tox_file_control(m_toxThread->Tox(), transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_CANCEL, nullptr); + tox_file_control(m_tox, transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_CANCEL, nullptr); } } } @@ -380,11 +380,11 @@ void CToxProto::ResumeIncomingTransfers(uint32_t friendNumber) // only for receiving FileTransferParam *transfer = transfers.GetAt(i); if (transfer->friendNumber == friendNumber && transfer->GetDirection() == 1) { - ToxHexAddress pubKey = GetContactPublicKey(m_toxThread->Tox(), friendNumber); + ToxHexAddress pubKey = GetContactPublicKey(m_tox, friendNumber); debugLogA(__FUNCTION__": sending ask to resume the transfer of file (%d) from %s (%d) cause (%d)", transfer->fileNumber, (const char*)pubKey, transfer->friendNumber); TOX_ERR_FILE_CONTROL error; - if (!tox_file_control(m_toxThread->Tox(), transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_RESUME, &error)) { + if (!tox_file_control(m_tox, transfer->friendNumber, transfer->fileNumber, TOX_FILE_CONTROL_RESUME, &error)) { debugLogA(__FUNCTION__": failed to resume the transfer (%d) from %s (%d) cause (%d)", transfer->fileNumber, (const char*)pubKey, transfer->friendNumber, error); CancelTransfer(NULL, transfer); } -- cgit v1.2.3