summaryrefslogtreecommitdiffstats
path: root/src/client/net/serverdiscovery.h
blob: a41c946d38a1fec9b8583312e5120d085594b04f (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
#ifndef SERVERDISCOVERY_H
#define SERVERDISCOVERY_H

#include <QObject>
#include <QUdpSocket>
#include <QTimer>
#include "../../shared/networkmessage.h"

class ServerDiscovery : public QObject
{
		Q_OBJECT

	public:
		enum class ErrorType{
			InvalidIpList,
			InvalidHash
		};

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

		void start(const QByteArray& sessionName, QString mgrIP);
		void stop();
		inline bool isActive(){ return _discoveryTimer.isActive(); }

	private:
		QTimer     _discoveryTimer;
		const int  _minDiscoveryInterval;
		const int  _maxDiscoveryInterval;
		int        _hashErrorCount;
		int        _ipErrorCount;
		QByteArray _nameBytes;
		QByteArray _salt2;
		QUdpSocket _discoverySocket;
		NetworkMessage _packet;

		QHostAddress _mgrIP;

		static const int UDPBUFSIZ = 9000;
		static const int SALT_LEN = 18;

	signals:
		void serverDetected(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect);
		void error(ErrorType e, int count);

	public slots:

	private slots:
		void doDiscovery();
		void onUdpReadyRead();

};

#endif // SERVERDISCOVERY_H