From e8f11470ac52bc89e955630d69ed8229a06acc79 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 15 Feb 2015 15:17:05 +0000 Subject: Tox: - refactoring - nodes settings in db are ansi now - added work with tox network git-svn-id: http://svn.miranda-ng.org/main/trunk@12124 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_network.cpp | 117 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 protocols/Tox/src/tox_network.cpp (limited to 'protocols/Tox/src/tox_network.cpp') diff --git a/protocols/Tox/src/tox_network.cpp b/protocols/Tox/src/tox_network.cpp new file mode 100644 index 0000000000..834f438304 --- /dev/null +++ b/protocols/Tox/src/tox_network.cpp @@ -0,0 +1,117 @@ +#include "common.h" + +bool CToxProto::IsOnline() +{ + return isConnected && m_iStatus > ID_STATUS_OFFLINE; +} + +void CToxProto::BootstrapNode(int index) +{ + bool isIPv4 = getBool("DisableIPv6", 0); + int nodeCount = db_get_w(NULL, "TOX", TOX_SETTINGS_NODE_COUNT, 0); + if (!nodeCount) + { + tox_bootstrap_from_address( + tox, isIPv4 ? "192.254.75.102" : "2607:5600:284::2", 33445, + ToxBinAddress("951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F")); + tox_bootstrap_from_address( + tox, "104.219.184.206", 443, + ToxBinAddress("8CD087E31C67568103E8C2A28653337E90E6B8EDA0D765D57C6B5172B4F1F04C")); + return; + } + int i = index % nodeCount; + char setting[MAX_PATH]; + mir_snprintf(setting, SIZEOF(setting), isIPv4 ? TOX_SETTINGS_NODE_IPV4 : TOX_SETTINGS_NODE_IPV6, i); + ptrA address(db_get_sa(NULL, "TOX", setting)); + if (address == NULL && !isIPv4) + { + mir_snprintf(setting, SIZEOF(setting), TOX_SETTINGS_NODE_IPV4, i); + address = db_get_sa(NULL, "TOX", setting); + } + mir_snprintf(setting, SIZEOF(setting), TOX_SETTINGS_NODE_PORT, i); + int port = db_get_w(NULL, "TOX", setting, 0); + mir_snprintf(setting, SIZEOF(setting), TOX_SETTINGS_NODE_PKEY, i); + ptrA pubKey(db_get_sa(NULL, "TOX", setting)); + tox_bootstrap_from_address(tox, address, port, ToxBinAddress(pubKey)); +} + +void CToxProto::TryConnect() +{ + if (tox_isconnected(tox)) + { + isConnected = true; + debugLogA("CToxProto::PollingThread: successfuly connected to DHT"); + m_iStatus = m_iDesiredStatus; + ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)ID_STATUS_CONNECTING, m_iStatus); + tox_set_user_status(tox, MirandaToToxStatus(m_iStatus)); + debugLogA("CToxProto::PollingThread: changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus); + } + else + { + BootstrapNode(m_iStatus); + if (m_iStatus++ > MAX_CONNECT_RETRIES) + { + SetStatus(ID_STATUS_OFFLINE); + ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, (HANDLE)NULL, LOGINERR_NONETWORK); + debugLogA("CToxProto::PollingThread: failed to connect to DHT"); + } + } +} + +void CToxProto::CheckConnection(int &retriesCount) +{ + if (!isConnected) + { + TryConnect(); + } + else + { + if (tox_isconnected(tox)) + { + retriesCount = TOX_MAX_DISCONNECT_RETRIES; + } + else if (!(--retriesCount)) + { + isConnected = false; + debugLogA("CToxProto::PollingThread: disconnected from DHT"); + SetStatus(ID_STATUS_OFFLINE); + } + } +} + +void CToxProto::DoTox() +{ + { + mir_cslock lock(toxLock); + tox_do(tox); + } + uint32_t interval = tox_do_interval(tox); + Sleep(interval); +} + +void CToxProto::PollingThread(void*) +{ + debugLogA("CToxProto::PollingThread: entering"); + + if (!InitToxCore()) + { + SetStatus(ID_STATUS_OFFLINE); + ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, (HANDLE)NULL, LOGINERR_WRONGPASSWORD); + debugLogA("CToxProto::PollingThread: leaving"); + return; + } + + int retriesCount = TOX_MAX_DISCONNECT_RETRIES; + isConnected = false; + + while (!isTerminated) + { + CheckConnection(retriesCount); + DoTox(); + } + + UninitToxCore(); + isConnected = false; + + debugLogA("CToxProto::PollingThread: leaving"); +} \ No newline at end of file -- cgit v1.2.3