summaryrefslogtreecommitdiffstats
path: root/pica-client/directlistener.h
blob: 0e69d16c5c0cd4901c1e99075c9571207f0e2485 (plain) (blame)
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
/*
	(c) Copyright  2012 - 2026 Anton Sviridenko
	https://picapica.im

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, version 3.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef DIRECTLISTENER_H
#define DIRECTLISTENER_H

#include <QObject>
#include <QString>
#include <QTimer>

struct PICA_upnp_ctx;

// Opens the listener for incoming direct connections and keeps its UPnP port
// mapping up for as long as the client runs.
//
// Talking to a UPnP router blocks - discovery for two seconds, every request
// for as long as the router takes to answer - so this lives on a thread of
// its own instead of SkyNet's, which carries the media of calls. The
// listener it opens is handed over to SkyNet and used on SkyNet's thread
// only; what changes about the mapping later on is reported with
// upnpChanged(), for SkyNet to pass on to the listener.
class DirectListener : public QObject
{
	Q_OBJECT
public:
	// Ports tried in automatic configuration, from DirectC2CSettings::DefaultPort
	// on, until one can be opened for TCP and UDP and - with UPnP - forwarded.
	enum
	{
		PortTries = 16
	};

	explicit DirectListener(QObject *parent = 0);
	~DirectListener();

public slots:
	// Opens the listener as the "Direct connections" settings say and reports
	// the outcome with ready(). addrList is DirectC2CSettings' stored form.
	void setup(quintptr acc, int addrConfig, bool useUpnp, int localPort, QString addrList);

	// Deletes the port mapping. Called once, at shutdown.
	void release();

signals:
	// listener is a struct PICA_listener *, 0 if none could be opened.
	// upnpAddr is empty when no port mapping was made.
	void ready(quintptr listener, QString upnpAddr, int upnpPort);

	// The external address or port of the mapping changed - the router got a
	// new address from the provider - or the mapping was lost, in which case
	// upnpAddr is empty.
	void upnpChanged(QString upnpAddr, int upnpPort);

	void statusMsg(QString msg, bool isCritical);

private slots:
	void refresh();

private:
	struct PICA_upnp_ctx *upnp;
	QString upnpAddr;
	// the port the mapping is for, the same number as the listening port
	int upnpPort;
	bool upnpLost;
	QTimer *refreshTimer;

	bool discoverUpnp();
	void dropUpnp();
	void scheduleRefresh();
};

#endif // DIRECTLISTENER_H