-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameSpy.cpp
152 lines (134 loc) · 4.21 KB
/
GameSpy.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 "GameSpy.h"
#include <QHostInfo>
#include <QtDebug>
GameSpyServer::GameSpyServer(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("GameSpyServer: invalid configuration"));
}
GameSpyServer::GameSpyServer(const char *host, int port)
{
setup(host, port);
}
void GameSpyServer::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(QString("GameSpyServer: "
"can't listen: ") + m_pUdpSocket->errorString());
}
qDebug() << tr("GameSpyServer: 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 GameSpyServer::numPlayers() const
{
return m_iNumPlayers;
}
unsigned int GameSpyServer::maxPlayers() const
{
return m_iMaxPlayers;
}
QString GameSpyServer::map() const
{
return m_sMap;
}
QString GameSpyServer::mode() const
{
return m_sMode;
}
void GameSpyServer::query()
{
QHostInfo ns = QHostInfo::fromName(m_sHost);
if(!ns.addresses().isEmpty())
{
if(m_pUdpSocket->writeDatagram("\\basic\\\\info\\", 13,
ns.addresses().first(), m_iPort) == -1)
{
emit errorEncountered(m_pUdpSocket->errorString());
}
}
else
emit errorEncountered(ns.errorString());
}
void GameSpyServer::refresh()
{
query();
m_pTimer->start();
}
void GameSpyServer::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);
QMap<QString, QString> infos;
QRegExp regexp("\\\\([^\\\\]+)\\\\");
int pos = 0;
while((pos = regexp.indexIn(datagram, pos)) != -1)
{
QString key = regexp.cap(1);
pos += regexp.matchedLength() - 1;
if( (key != "basic" && key != "info")
&& ((pos = regexp.indexIn(datagram, pos)) != -1) )
{
infos[key] = regexp.cap(1);
pos += regexp.matchedLength() - 1;
}
}
bool ok, changed = false;
unsigned int old_players = m_iNumPlayers;
changed = confirm_assign(&m_iNumPlayers,
(unsigned)infos["numplayers"].toInt(&ok, 10)) || changed;
if(infos["numplayers"] != "0")
changed = confirm_assign(&m_iMaxPlayers,
(unsigned)infos["maxplayers"].toInt(&ok, 10)) || changed;
if(!infos["mapname"].isEmpty())
changed = confirm_assign(&m_sMap, infos["mapname"]) || changed;
if(!infos["gametype"].isEmpty())
changed = confirm_assign(&m_sMode, infos["gametype"]) || changed;
if(changed)
emit infosChanged(m_iNumPlayers, m_iMaxPlayers, m_sMap, m_sMode,
old_players == 0 && m_iNumPlayers > 0);
}
}