summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_connection.cpp
blob: 3b0d3c9d46f0d22a2fb447c2dac218aff169b21b (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
#include "stdafx.h"

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

/////////////////////////////////////////////////////////////////////////////////////////

void CToxProto::OnLoggedIn()
{
	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);
}

/////////////////////////////////////////////////////////////////////////////////////////
// Tox polling callback

static void sttScheduledDisconnect(void *param)
{
	auto *ppro = (CToxProto *)param;
	ppro->SetStatus(ID_STATUS_OFFLINE);
	ppro->ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, LOGINERR_NONETWORK);
	ppro->debugLogA(__FUNCTION__": failed to connect to DHT");
}

void CToxProto::OnConnectionStatus(Tox*, Tox_Connection iNewStatus, void *pUserData)
{
	auto *ppro = (CToxProto *)pUserData;
	if (ppro->m_tox == nullptr)
		return;

	if (iNewStatus == TOX_CONNECTION_NONE) {
		// we cannot destroy Tox object inside its hook... #3649
		Miranda_WaitOnHandleEx(sttScheduledDisconnect, ppro);
	}
	else ppro->OnLoggedIn();
}

void CToxProto::OnToxPoll()
{
	if (m_tox)
		tox_iterate(m_tox, this);
}