/
icserver.h
68 lines (54 loc) · 1.69 KB
/
icserver.h
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
#ifndef ICSERVER_H
#define ICSERVER_H
#include <QObject>
#include <QUdpSocket>
#include <QSettings>
#include <QTimer>
#include "icmessagehandler.h"
class QHostAddress;
class ICQuestion;
class ICAnswer;
class ICServer : public QObject
{
Q_OBJECT
public:
explicit ICServer(QObject *parent = 0);
void setUp(QString identifier, QHostAddress address = QHostAddress());
// broadcast identifier to 8027, listen on port 8026
// you need to connect identifier collision signal. 3000ms
bool startSidTest(int duration = 3000);
// listen on port 8027, broadcast question to 8026
bool listen();
// stop the server
void stop();
// need to set current question first in store object
void broadcastQuestion();
QString getIdentifier() const;
signals:
void identifierCollision(QString identifer);
void newAnswerArrived(QString uid);
void connectionClosed();
public slots:
// clientAddress requests a question
void processRequest(QHostAddress clientAddress);
// a connection offer with identifier comes
void processSidCollision(QString identifier);
// a server discovery with specified identifier comes
void processDiscovery(QHostAddress clientAddress, QString identifier);
// an answer come with sender's uid
void processAnswer(ICAnswer answer, QString uid);
private slots:
void socketClosed(QAbstractSocket::SocketError);
void stopSidTest();
void processPendingDatagrams();
private:
QSettings *settings;
QHostAddress ipAddress;
// whether this server is serving connection
bool isRunning;
QUdpSocket *udpSocket;
QUdpSocket *testUdpSocket;
QTimer *timer;
QString sid;
};
#endif // ICSERVER_H