-
Notifications
You must be signed in to change notification settings - Fork 2
/
unv.h
225 lines (180 loc) · 6.82 KB
/
unv.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* This file is part of Osavul.
*
* Osavul 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, either version 3 of the License, or
* (at your option) any later version.
*
* Osavul 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 Osavul. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UNV_H
#define UNV_H
#include <QtNetwork/QUdpSocket>
#include <QtCore/QElapsedTimer>
#include <QtCore/QStringBuilder>
#include <QtCore/QDateTime>
#include <QtCore/QSettings>
#include <QtGui/QColor>
#include <QtGui/QTextDocument> // for Qt::escape()
#define FFFF "\xff\xff\xff\xff"
namespace unv {
class Server : public QObject
{
Q_OBJECT
public:
Server(const QHostAddress &host, quint16 port, const QByteArray &queryMessage);
Server(const QString &host, quint16 port, const QByteArray &queryMessage);
void addAddress(const QHostAddress &host, quint16 port);
QString host(int index = 0) const { return validIndex(index) ? sock[index].sock.peerName() : ""; }
quint16 port(int index = 0) const { return validIndex(index) ? sock[index].sock.peerPort() : 0; }
quint16 ping(int index = 0) const { return validIndex(index) ? sock[index].ping : 0; }
bool ipv4() const;
bool ipv6() const;
bool has(const QHostAddress &host, quint16 port) const;
const QString &infoString() const { return m_infoString; }
protected:
~Server();
virtual void processOOB(QByteArray st) = 0;
const QByteArray m_queryMessage;
struct {
QUdpSocket sock;
QElapsedTimer pingTimer;
int ping;
} sock[2];
bool validIndex(int index) const { return index >= 0 && index < 2; }
const QSettings settings;
private:
QString m_infoString;
public slots:
void query();
protected slots:
void receiveData();
};
class Player
{
public:
enum Team {Spectators = 0, Aliens = 1, Humans = 2};
Player(Team t = Spectators, const QString &name = "unknown",
qint16 score = 0, quint16 ping = 0, bool isBot = false)
: m_team(t), m_name(name), m_plainName(QString(name).replace(QRegExp("<[^>]*>"), ""))
, m_score(score), m_ping(ping), m_bot(isBot) { }
bool operator <(const Player &other) const { return m_score < other.score(); }
bool operator >(const Player &other) const { return m_score > other.score(); }
Team team() const { return m_team; }
const QString &name() const { return m_name; }
const QString &plainName() const { return m_plainName; }
qint16 score() const { return m_score; }
quint16 ping() const { return m_ping; }
bool isBot() const { return m_bot; }
private:
Team m_team;
QString m_name;
QString m_plainName;
qint16 m_score;
quint16 m_ping;
bool m_bot;
};
class GameServer : public Server
{
Q_OBJECT
public:
GameServer(const QHostAddress &host, quint16 port)
: Server(host, port, FFFF "getstatus"), info() { }
GameServer(const QString &host, quint16 port)
: Server(host, port, FFFF "getstatus"), info() { }
~GameServer() { }
QString game() const { return info.game; }
QString map() const { return info.mapname; }
QString name() const { return info.name; }
QString statsURL() const { return info.statsURL; }
const QList<Player> &players() const { return m_players; }
const QDateTime &lastUpdateTime() const { return m_lastUpdateTime; }
bool isPure() const { return info.pure; }
int botCount() const;
QString formattedAddress(bool usePreferred, const QString &fmt = "%1:%2") const;
QString formattedAddress(int which = 0, const QString &fmt = "%1:%2") const;
QString formattedClientCount(const QString &fmt = "[%1/%2]") const;
private:
Q_DISABLE_COPY(GameServer)
Player constructPlayer(Player::Team t, const QByteArray &entry, bool isBot = false);
void parsePB(const QByteArray &pString, const QByteArray &bString, const QList<QByteArray> &p2);
struct {
QString game;
QString mapname;
QString name;
QString statsURL;
quint8 clients;
quint8 maxclients;
bool pure;
} info;
QList<Player> m_players;
QDateTime m_lastUpdateTime;
void processOOB(QByteArray st);
signals:
void ready();
};
class LinkedAddressPair : public QObject
{
Q_OBJECT
public:
LinkedAddressPair(const QHostAddress &ip4, quint16 portno4, const QHostAddress &ip6, quint16 portno6)
: ipv4(ip4), ipv6(ip6), port4(portno4), port6(portno6)
{ }
~LinkedAddressPair() { }
bool contains(const QHostAddress &ip, quint16 port) const
{
return (ipv4 == ip && port4 == port ) || (ipv6 == ip && port6 == port);
}
const QHostAddress &getOtherAddress(const QHostAddress &ip) const
{
return ipv4 == ip ? ipv6 : ipv4;
}
quint16 getOtherPort(const QHostAddress &ip) const
{
return ipv4 == ip ? port6 : port4;
}
private:
QHostAddress ipv4, ipv6;
quint16 port4, port6;
};
class MasterServer : public Server
{
Q_OBJECT
public:
MasterServer(const QString &host, quint16 port, quint16 protocol)
: Server(host, port, FFFF "getserversExt UNVANQUISHED " % QByteArray::number(protocol) % " dual full empty")
{ }
~MasterServer() { }
const QList<GameServer *> &servers() { return gameServers; }
private:
Q_DISABLE_COPY(MasterServer)
QList<GameServer *> gameServers;
QList<LinkedAddressPair *> linkedAddressPairs;
void processOOB(QByteArray st);
QList<GameServer *> *parseServersOOB(const QByteArray &oob, int index, int length, bool extended);
signals:
void serverQueried(unv::GameServer *sv);
public slots:
void onGameSvReady();
};
struct FavoriteEntry {
QString name;
QString host;
quint16 port;
bool operator ==(const FavoriteEntry &other) const
{
return (name == other.name && host == other.host && port == other.port);
}
};
}
Q_DECLARE_METATYPE(unv::GameServer *)
Q_DECLARE_METATYPE(unv::FavoriteEntry *)
#undef FFFF
#endif // UNV_H