summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_connection.cpp
blob: a175126a9aa8be821d2e98115f69c1bf98d1f26d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "stdafx.h"

bool CToxProto::IsOnline()
{
	return m_tox && m_iStatus >= ID_STATUS_ONLINE;
}

void CToxProto::TryConnect()
{
	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(m_tox, MirandaToToxStatus(m_iStatus));

		debugLogA(__FUNCTION__": changing status from %i to %i", ID_STATUS_CONNECTING, m_iDesiredStatus);

		UpdateStatusMenu(NULL, NULL);

		LoadFriendList(m_tox);
		return;
	}

	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;
	}
}

void CToxProto::CheckConnection()
{
	int maxReconnectRetries = getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_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;
		}
		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;
	}
}

void CToxProto::OnToxCheck(void *arg, BYTE)
{
	CToxProto *proto = (CToxProto*)arg;

	int retriesCount = proto->getByte("MaxReconnectRetries", TOX_MAX_RECONNECT_RETRIES);
	if (proto->m_iStatus < ID_STATUS_ONLINE)
		proto->TryConnect();
	else
		proto->CheckConnection();
}

void CToxProto::OnToxPoll(void *arg, BYTE)
{
	CToxProto *proto = (CToxProto*)arg;

	tox_iterate(proto->m_tox, arg);
	/*uint32_t interval = tox_iteration_interval(proto->m_tox);
	interval = interval
		? interval
		: TOX_DEFAULT_INTERVAL;*/
}