-
Notifications
You must be signed in to change notification settings - Fork 0
/
Teeworlds.cpp
152 lines (134 loc) · 4.33 KB
/
Teeworlds.cpp
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
/* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar
* 14 rue de Plaisance, 75014 Paris, France
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include "Teeworlds.h"
#include <QHostInfo>
#include <QtDebug>
TeeworldsServer::TeeworldsServer(const QString ¶m)
{
QRegExp reg("^([^ ]+) ([0-9]+)$");
if(reg.indexIn(param) != -1)
{
bool ok;
int port = reg.cap(2).toInt(&ok, 10);
if(ok && port >= 1 && port <= 65535)
{
setup(reg.cap(1).toLatin1(), port);
return ;
}
}
throw ServerError(tr("TeeworldsServer: invalid configuration"));
}
TeeworldsServer::TeeworldsServer(const char *host, int port)
{
setup(host, port);
}
void TeeworldsServer::setup(const char *host, int port)
{
m_sHost = host; m_iPort = port; m_iNumPlayers = 0; m_iMaxPlayers = 0;
m_pUdpSocket = new QUdpSocket(this);
{
int port = 5000;
while(!m_pUdpSocket->bind(QHostAddress::Any, port))
{
delete m_pUdpSocket; m_pUdpSocket = new QUdpSocket(this); // FIXME
port++;
if(port == 5040)
throw ServerError(tr("TeeworldsServer: "
"can't listen: ") + m_pUdpSocket->errorString());
}
qDebug() << tr("TeeworldsServer: listening on port %1").arg(port);
}
connect(m_pUdpSocket, SIGNAL(readyRead()), this, SLOT(receiveData()));
m_pTimer = new QTimer(this);
m_pTimer->setSingleShot(false);
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(query()));
m_pTimer->start(30000);
query();
}
unsigned int TeeworldsServer::numPlayers() const
{
return m_iNumPlayers;
}
unsigned int TeeworldsServer::maxPlayers() const
{
return m_iMaxPlayers;
}
QString TeeworldsServer::map() const
{
return m_sMap;
}
QString TeeworldsServer::mode() const
{
return m_sMode;
}
void TeeworldsServer::query()
{
QHostInfo ns = QHostInfo::fromName(m_sHost);
if(!ns.addresses().isEmpty())
{
if(m_pUdpSocket->writeDatagram(
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffgief", 14,
ns.addresses().first(), m_iPort) == -1)
{
emit errorEncountered(m_pUdpSocket->errorString());
}
}
else
emit errorEncountered(ns.errorString());
}
void TeeworldsServer::refresh()
{
query();
m_pTimer->start();
}
void TeeworldsServer::receiveData()
{
while(m_pUdpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(m_pUdpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
m_pUdpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort);
QRegExp regexp("info([0-9]+\\.[0-9]+\\.[0-9]+)" // server version
"\\x0000(.*)" // server name
"\\x0000(.*)" // map name
"\\x0000(.*)" // game type
"\\x0000(-?[0-9]*)" // flags
"\\x0000(-?[0-9]*)" // progression
"\\x0000([0-9]*)" // player count
"\\x0000([0-9]*)" // max players
"\\x0000");
// We use fromAscii because the implicit cast QByteArray -> QString
// will stop the string at the first null byte
if(regexp.indexIn(
QString::fromAscii(datagram.constData(), datagram.size())) == -1)
{
emit errorEncountered(tr("Invalid answer"));
}
bool ok, changed = false;
unsigned int old_players = m_iNumPlayers;
changed = confirm_assign(&m_iNumPlayers,
(unsigned)regexp.cap(7).toInt(&ok, 10)) || changed;
changed = confirm_assign(&m_iMaxPlayers,
(unsigned)regexp.cap(8).toInt(&ok, 10)) || changed;
changed = confirm_assign(&m_sMap, regexp.cap(3)) || changed;
changed = confirm_assign(&m_sMode, regexp.cap(4)) || changed;
if(changed)
emit infosChanged(m_iNumPlayers, m_iMaxPlayers, m_sMap, m_sMode,
old_players == 0 && m_iNumPlayers > 0);
}
}