From 2e80e1093ab6de23c33fbc1f258ce281f1e4dae6 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sat, 9 Aug 2014 08:07:07 +0000 Subject: Tox: - added pooling thread - added test code to check connection git-svn-id: http://svn.miranda-ng.org/main/trunk@10133 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/Tox_12.vcxproj | 3 +- protocols/Tox/Tox_12.vcxproj.filters | 3 ++ protocols/Tox/src/common.h | 4 +++ protocols/Tox/src/tox_events.cpp | 17 +++++++++++ protocols/Tox/src/tox_proto.cpp | 56 +++++++++++++++++++++++------------- protocols/Tox/src/tox_proto.h | 28 ++++++++++-------- protocols/Tox/src/tox_utils.cpp | 49 +++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 33 deletions(-) create mode 100644 protocols/Tox/src/tox_utils.cpp diff --git a/protocols/Tox/Tox_12.vcxproj b/protocols/Tox/Tox_12.vcxproj index be437aec1f..f6ae91e2b8 100644 --- a/protocols/Tox/Tox_12.vcxproj +++ b/protocols/Tox/Tox_12.vcxproj @@ -97,7 +97,7 @@ $(IntDir)$(TargetName).lib false false - libtox.lib;%(AdditionalDependencies) + ws2_32.lib;libtox.lib;%(AdditionalDependencies) _DEBUG;%(PreprocessorDefinitions) @@ -215,6 +215,7 @@ + diff --git a/protocols/Tox/Tox_12.vcxproj.filters b/protocols/Tox/Tox_12.vcxproj.filters index 327ae80404..986d1f856a 100644 --- a/protocols/Tox/Tox_12.vcxproj.filters +++ b/protocols/Tox/Tox_12.vcxproj.filters @@ -59,6 +59,9 @@ Source Files + + Source Files + diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index 04d3106560..c8bb724f73 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -20,6 +20,10 @@ extern HINSTANCE g_hInstance; +#define BOOTSTRAP_ADDRESS "23.226.230.47" +#define BOOTSTRAP_PORT 33445 +#define BOOTSTRAP_KEY "A09162D68618E742FFBCA1C2C70385E6679604B2D80EA6E84AD0996A1AC8A074" + #include "tox_proto.h" #endif //_COMMON_H_ \ No newline at end of file diff --git a/protocols/Tox/src/tox_events.cpp b/protocols/Tox/src/tox_events.cpp index 550cdd2999..dc45cb34a6 100644 --- a/protocols/Tox/src/tox_events.cpp +++ b/protocols/Tox/src/tox_events.cpp @@ -1,5 +1,22 @@ #include "common.h" +void CToxProto::PollingThread(void*) +{ + debugLogA("CToxProto::PollingThread: entering"); + while (!isTerminated) + { + uint32_t interval = 1000; + { + //mir_cslock lock(tox_lock); + + tox_do(tox); + interval = tox_do_interval(tox); + } + Sleep(interval); + } + debugLogA("CToxProto::PollingThread: leaving"); +} + void CToxProto::OnFriendRequest(Tox *tox, const uint8_t *userId, const uint8_t *message, const uint16_t messageSize, void *arg) { } diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 00303c8f0d..73bed91d17 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -1,30 +1,24 @@ #include "common.h" CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : - PROTO(protoName, userName), - _tox(tox_new(0)) + PROTO(protoName, userName) { - tox_callback_friend_request(_tox, OnFriendRequest, this); - tox_callback_friend_message(_tox, OnFriendMessage, this); - tox_callback_friend_action(_tox, OnAction, this); - tox_callback_name_change(_tox, OnFriendNameChange, this); - tox_callback_status_message(_tox, OnStatusMessageChanged, this); - tox_callback_user_status(_tox, OnUserStatusChanged, this); - tox_callback_connection_status(_tox, OnConnectionStatusChanged, this); + tox = tox_new(0); + + tox_callback_friend_request(tox, OnFriendRequest, this); + tox_callback_friend_message(tox, OnFriendMessage, this); + tox_callback_friend_action(tox, OnAction, this); + tox_callback_name_change(tox, OnFriendNameChange, this); + tox_callback_status_message(tox, OnStatusMessageChanged, this); + tox_callback_user_status(tox, OnUserStatusChanged, this); + tox_callback_connection_status(tox, OnConnectionStatusChanged, this); CreateProtoService(PS_CREATEACCMGRUI, &CToxProto::CreateAccMgrUI); } CToxProto::~CToxProto() { - tox_kill(_tox); -} - -void CALLBACK CToxProto::TimerProc(HWND, UINT, UINT_PTR idEvent, DWORD) -{ - CToxProto *ppro = (CToxProto*)idEvent; - - tox_do(ppro->_tox); + tox_kill(tox); } MCONTACT __cdecl CToxProto::AddToList(int flags, PROTOSEARCHRESULT* psr) @@ -62,6 +56,8 @@ DWORD_PTR __cdecl CToxProto::GetCaps(int type, MCONTACT hContact) return (INT_PTR)"User Id"; case PFLAG_UNIQUEIDSETTING: return (DWORD_PTR)"UserId"; + case PFLAG_MAXLENOFMESSAGE: + return TOX_MAX_MESSAGE_LENGTH; } return 0; @@ -104,7 +100,8 @@ int __cdecl CToxProto::SetStatus(int iNewStatus) if (iNewStatus == ID_STATUS_OFFLINE) { - // lgout + // logout + isTerminated = true; m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); @@ -118,7 +115,26 @@ int __cdecl CToxProto::SetStatus(int iNewStatus) m_iStatus = ID_STATUS_CONNECTING; // login - //_timer = SetTimer(NULL, (UINT_PTR)this, 30, (TIMERPROC)CToxProto::TimerProc); + isTerminated = false; + char *name = "my_nickname"; + int res = tox_set_name(tox, (uint8_t*)name, strlen(name)); + + uint8_t *pub_key = HexToBinString(BOOTSTRAP_KEY); + res = tox_bootstrap_from_address(tox, BOOTSTRAP_ADDRESS, 0, htons(BOOTSTRAP_PORT), pub_key); + //mir_free(pub_key); + if (!res) + { + SetStatus(ID_STATUS_OFFLINE); + } + + res = tox_isconnected(tox); + if (!res) + { + SetStatus(ID_STATUS_OFFLINE); + return 0; + } + + poolingThread = ForkThreadEx(&CToxProto::PollingThread, 0, NULL); } else { @@ -139,7 +155,7 @@ int __cdecl CToxProto::SetStatus(int iNewStatus) userstatus = TOX_USERSTATUS_INVALID; break; } - tox_set_user_status(_tox, userstatus); + tox_set_user_status(tox, userstatus); ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)old_status, m_iStatus); diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 37fee20593..5319744a2f 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -71,36 +71,40 @@ public: static void UninitInstances(); private: + Tox *tox; + mir_cs tox_lock; + HANDLE poolingThread; + bool isTerminated; // instances static LIST instanceList; static int CompareProtos(const CToxProto *p1, const CToxProto *p2); - static void CALLBACK TimerProc(HWND, UINT, UINT_PTR idEvent, DWORD); - - // Instance data: - Tox *_tox; - UINT_PTR _timer; - //services INT_PTR __cdecl CreateAccMgrUI(WPARAM, LPARAM); //events + void __cdecl PollingThread(void*); + static void OnFriendRequest(Tox *tox, const uint8_t *userId, const uint8_t *message, const uint16_t messageSize, void *arg); - static void OnFriendMessage(Tox *tox, const int friendId, const uint8_t *message, const uint16_t messageSize, void *arg); - static void OnFriendNameChange(Tox *tox, const int friendId, const uint8_t *name, const uint16_t nameSize, void *arg); - static void OnStatusMessageChanged(Tox *tox, const int friendId, const uint8_t* message, const uint16_t messageSize, void *arg); + static void OnFriendMessage(Tox *tox, const int friendId, const uint8_t *message, const uint16_t messageSize, void *arg); + static void OnFriendNameChange(Tox *tox, const int friendId, const uint8_t *name, const uint16_t nameSize, void *arg); + static void OnStatusMessageChanged(Tox *tox, const int friendId, const uint8_t* message, const uint16_t messageSize, void *arg); static void OnUserStatusChanged(Tox *tox, int32_t friendnumber, uint8_t TOX_USERSTATUS, void *userdata); - static void OnConnectionStatusChanged(Tox *tox, const int friendId, const uint8_t status, void *arg); - static void OnAction(Tox *tox, const int friendId, const uint8_t *message, const uint16_t messageSize, void *arg); + static void OnConnectionStatusChanged(Tox *tox, const int friendId, const uint8_t status, void *arg); + static void OnAction(Tox *tox, const int friendId, const uint8_t *message, const uint16_t messageSize, void *arg); // contacts bool IsProtoContact(MCONTACT hContact); MCONTACT GetContactByUserId(const wchar_t *userId); MCONTACT CToxProto::AddContact(const wchar_t*userId, const wchar_t *nick, bool isHidden = false); - + void __cdecl SearchByUidAsync(void* arg); + // utils + uint8_t *HexToBinString(char *hex_string); + char *BinToHexString(uint8_t *bin_string); + // dialogs static INT_PTR CALLBACK AccountManagerProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); }; diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp new file mode 100644 index 0000000000..093becbce4 --- /dev/null +++ b/protocols/Tox/src/tox_utils.cpp @@ -0,0 +1,49 @@ +#include "common.h" + +uint8_t *CToxProto::HexToBinString(char *hex_string) +{ + // byte is represented by exactly 2 hex digits, so lenth of binary string + // is half of that of the hex one. only hex string with even length + // valid. the more proper implementation would be to check if strlen(hex_string) + // is odd and return error code if it is. we assume strlen is even. if it's not + // then the last byte just won't be written in 'ret'. + size_t i, len = (strlen(hex_string) / 2); + uint8_t *ret = (uint8_t*)mir_alloc(len); + char *pos = hex_string; + + for (i = 0; i < len; i++, pos += 2) + { + sscanf(pos, "%2hhx", &ret[i]); + } + + return ret; +} + +char *CToxProto::BinToHexString(uint8_t *bin_string) +{ + uint32_t i, delta = 0, pos_extra, sum_extra = 0; + char *ret = (char*)mir_alloc(TOX_FRIEND_ADDRESS_SIZE); + + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) + { + sprintf(&ret[2 * i + delta], "%02hhX", bin_string[i]); + + if ((i + 1) == TOX_CLIENT_ID_SIZE) + pos_extra = 2 * (i + 1) + delta; + + if (i >= TOX_CLIENT_ID_SIZE) + sum_extra |= bin_string[i]; + + /*if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) { + id_str[2 * (i + 1) + delta] = ' '; + delta++; + }*/ + } + + ret[2 * i + delta] = 0; + + if (!sum_extra) + ret[pos_extra] = 0; + + return ret; +} \ No newline at end of file -- cgit v1.2.3