summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_thread.h
blob: 59caa7ee304db0e90007b4f31e7949b27e22f96a (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
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef _TOX_THREAD_H_
#define _TOX_THREAD_H_

class CToxThread
{
private:
	Tox *tox;
	ToxAV *toxAV;
	bool isConnected;
	bool isTerminated;

	mir_cs toxLock;

	mir_cs lock;

public:
	CToxThread(Tox_Options *options, TOX_ERR_NEW *error = NULL) : tox(NULL), toxAV(NULL),
		isConnected(false), isTerminated(false)
	{
		tox = tox_new(options, error);
	}

	~CToxThread()
	{
		if (toxAV)
		{
			toxav_kill(toxAV);
			toxAV = NULL;
		}

		if (tox)
		{
			tox_kill(tox);
			tox = NULL;
		}
	}

	mir_cslock Lock()
	{
		return mir_cslock(lock);
	}

	Tox* Tox()
	{
		return tox;
	}

	ToxAV* ToxAV()
	{
		return toxAV;
	}

	bool IsConnected() const
	{
		return tox && isConnected;
	}

	void Connect()
	{
		Lock();

		isConnected = true;
	}

	void Disconnect()
	{
		Lock();

		isConnected = false;
	}

	void Iterate()
	{
		{
			mir_cslock lock(toxLock);
			tox_iterate(tox);
			//if (toxAV)
			//	toxav_iterate(toxAV);
		}
		uint32_t interval = tox_iteration_interval(tox);
		interval = interval ? interval : 50;
		Sleep(interval);
	}

	bool IsTerminated() const
	{
		return !tox || isTerminated;
	}

	void Stop() { isTerminated = true; }
};

#endif //_TOX_THREAD_H_