From 036a125b6b499f87072b32310ac5acfab697be84 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 2 Nov 2015 11:35:01 +0000 Subject: Tox: ok, let there be a thread git-svn-id: http://svn.miranda-ng.org/main/trunk@15668 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_accounts.cpp | 2 ++ protocols/Tox/src/tox_messages.cpp | 57 +++++++++++++++++++++++++++----------- protocols/Tox/src/tox_proto.cpp | 3 +- protocols/Tox/src/tox_proto.h | 6 +++- 4 files changed, 50 insertions(+), 18 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/tox_accounts.cpp b/protocols/Tox/src/tox_accounts.cpp index fd86624318..109a4532f6 100644 --- a/protocols/Tox/src/tox_accounts.cpp +++ b/protocols/Tox/src/tox_accounts.cpp @@ -42,6 +42,8 @@ int CToxProto::OnAccountLoaded(WPARAM, LPARAM) int CToxProto::OnAccountRenamed(WPARAM, LPARAM) { + mir_cslock locker(profileLock); + ptrT newPath(GetToxProfilePath()); TCHAR oldPath[MAX_PATH]; mir_sntprintf(oldPath, MAX_PATH, _T("%s\\%s.tox"), VARST(_T("%miranda_userdata%")), accountName); diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp index 0ce57c1a35..5b2867cbe6 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -33,40 +33,64 @@ void CToxProto::OnFriendMessage(Tox*, uint32_t friendNumber, TOX_MESSAGE_TYPE ty /* MESSAGE SENDING */ // outcoming message flow -int CToxProto::OnSendMessage(MCONTACT hContact, const char *szMessage) +struct SendMessageParam { - if (!IsOnline()) - { - ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, NULL, (LPARAM)Translate("You cannot send when you are offline.")); - return 0; - } + MCONTACT hContact; + UINT hMessage; + char *message; +}; - int32_t friendNumber = GetToxFriendNumber(hContact); +void CToxProto::SendMessageAsync(void *arg) +{ + SendMessageParam *param = (SendMessageParam*)arg; + + int32_t friendNumber = GetToxFriendNumber(param->hContact); if (friendNumber == UINT32_MAX) - return 0; + ProtoBroadcastAck(param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)param->hMessage, 0); - size_t msgLen = mir_strlen(szMessage); - uint8_t *msg = (uint8_t*)szMessage; + size_t msgLen = mir_strlen(param->message); + uint8_t *msg = (uint8_t*)param->message; TOX_MESSAGE_TYPE type = TOX_MESSAGE_TYPE_NORMAL; - if (strncmp(szMessage, "/me ", 4) == 0) + if (strncmp(param->message, "/me ", 4) == 0) { msg += 4; msgLen -= 4; type = TOX_MESSAGE_TYPE_ACTION; } TOX_ERR_FRIEND_SEND_MESSAGE sendError; - int messageId = tox_friend_send_message(toxThread->tox, friendNumber, type, msg, msgLen, &sendError); + int messageNumber = tox_friend_send_message(toxThread->tox, friendNumber, type, msg, msgLen, &sendError); if (sendError != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { logger->Log(__FUNCTION__": failed to send message for %d (%d)", friendNumber, sendError); + ProtoBroadcastAck(param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, (HANDLE)param->hMessage, 0); + } + + uint64_t messageId = (((int64_t)friendNumber) << 32) | ((int64_t)messageNumber); + messages[messageId] = param->hMessage; +} + +int CToxProto::OnSendMessage(MCONTACT hContact, const char *szMessage) +{ + if (!IsOnline()) + { + ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, NULL, (LPARAM)Translate("You cannot send when you are offline.")); return 0; } - return messageId; + UINT hMessage = InterlockedIncrement(&hMessageProcess); + + SendMessageParam *param = (SendMessageParam*)mir_calloc(sizeof(SendMessageParam)); + param->hContact = hContact; + param->hMessage = hMessage; + param->message = mir_strdup(szMessage); + + ForkThread(&CToxProto::SendMessageAsync, param); + + return hMessage; } // message is received by the other side -void CToxProto::OnReadReceipt(Tox*, uint32_t friendNumber, uint32_t messageId, void *arg) +void CToxProto::OnReadReceipt(Tox*, uint32_t friendNumber, uint32_t messageNumber, void *arg) { CToxProto *proto = (CToxProto*)arg; @@ -74,8 +98,9 @@ void CToxProto::OnReadReceipt(Tox*, uint32_t friendNumber, uint32_t messageId, v if (hContact == NULL) return; - proto->ProtoBroadcastAck( - hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)messageId, 0); + uint64_t messageId = (((int64_t)friendNumber) << 32) | ((int64_t)messageNumber); + UINT hMessage = proto->messages[messageId]; + proto->ProtoBroadcastAck(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)hMessage, 0); } // preparing message/action to writing into db diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index a95ec968e3..c7de660715 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -2,7 +2,8 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : PROTO(protoName, userName), - hPollingThread(NULL), hOutDevice(NULL), toxThread(NULL) + hPollingThread(NULL), toxThread(NULL), + hOutDevice(NULL), hMessageProcess(1) { InitNetlib(); diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 7075a281d1..fcb66f8177 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -72,6 +72,7 @@ private: HANDLE hNetlib, hPollingThread; CTransferList transfers; CLogger *logger; + ULONG hMessageProcess; static HANDLE hProfileFolderPath; @@ -202,10 +203,13 @@ private: static INT_PTR CALLBACK ChatRoomInviteProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam); // messages + std::map messages; + + void __cdecl SendMessageAsync(void *arg); int OnSendMessage(MCONTACT hContact, const char *message); static void OnFriendMessage(Tox *tox, uint32_t friendNumber, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *arg); - static void OnReadReceipt(Tox *tox, uint32_t friendNumber, uint32_t messageId, void *arg); + static void OnReadReceipt(Tox *tox, uint32_t friendNumber, uint32_t messageNumber, void *arg); void __cdecl GetStatusMessageAsync(void* arg); -- cgit v1.2.3