author | unc0rr |
Mon, 05 Feb 2007 22:39:01 +0000 | |
changeset 395 | 641ed71c7c93 |
parent 391 | e7565bb852a2 |
child 399 | c7da1bd32b4e |
permissions | -rw-r--r-- |
315 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2006 Ulyanov Igor <iulyanov@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include "netserver.h" |
|
20 |
||
21 |
#include <QTcpServer> |
|
22 |
#include <QTcpSocket> |
|
23 |
#include <QMessageBox> |
|
24 |
||
25 |
#include <algorithm> |
|
26 |
||
27 |
#include <QDebug> |
|
28 |
||
29 |
const quint16 HWNetServer::ds_port=46631; |
|
30 |
||
31 |
extern char delimeter; |
|
32 |
||
33 |
void HWNetServer::StartServer() |
|
34 |
{ |
|
35 |
IPCServer = new QTcpServer(this); |
|
347 | 36 |
if (!IPCServer->listen(QHostAddress::Any, ds_port)) { |
315 | 37 |
QMessageBox::critical(0, tr("Error"), |
38 |
tr("Unable to start the server: %1.") |
|
39 |
.arg(IPCServer->errorString())); |
|
40 |
} |
|
326 | 41 |
|
315 | 42 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
43 |
} |
|
44 |
||
45 |
void HWNetServer::StopServer() |
|
46 |
{ |
|
383 | 47 |
QList<HWConnectedClient*>::iterator it; |
48 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
49 |
ClientDisconnect(*it); |
|
50 |
} |
|
315 | 51 |
IPCServer->close(); |
52 |
} |
|
53 |
||
54 |
void HWNetServer::NewConnection() |
|
55 |
{ |
|
56 |
QTcpSocket* client = IPCServer->nextPendingConnection(); |
|
57 |
if(!client) return; |
|
58 |
connclients.push_back(new HWConnectedClient(this, client)); |
|
326 | 59 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), |
315 | 60 |
this, SLOT(ClientDisconnect(HWConnectedClient*))); |
61 |
} |
|
62 |
||
63 |
void HWNetServer::ClientDisconnect(HWConnectedClient* client) |
|
64 |
{ |
|
65 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
383 | 66 |
if(it==connclients.end()) return; |
350 | 67 |
for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) { |
383 | 68 |
sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1)); |
350 | 69 |
} |
315 | 70 |
connclients.erase(it); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
71 |
//teamChanged(); |
315 | 72 |
} |
73 |
||
74 |
QString HWNetServer::getRunningHostName() const |
|
75 |
{ |
|
76 |
return IPCServer->serverAddress().toString(); |
|
77 |
} |
|
78 |
||
79 |
quint16 HWNetServer::getRunningPort() const |
|
80 |
{ |
|
81 |
return ds_port; |
|
82 |
} |
|
83 |
||
334 | 84 |
HWConnectedClient* HWNetServer::getChiefClient() const |
317 | 85 |
{ |
86 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
334 | 87 |
// watch for first fully connected client (with confirmed nick) |
88 |
if((*it)->getClientNick()!="") return *it; |
|
317 | 89 |
} |
334 | 90 |
return 0; |
91 |
} |
|
92 |
||
93 |
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const |
|
94 |
{ |
|
95 |
return getChiefClient()==cl; |
|
96 |
} |
|
97 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
98 |
QMap<QString, QStringList> HWNetServer::getGameCfg() const |
334 | 99 |
{ |
100 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
335 | 101 |
if(isChiefClient(*it)) { |
102 |
return (*it)->m_gameCfg; |
|
103 |
} |
|
334 | 104 |
} |
105 |
// error happened if we are here |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
106 |
return QMap<QString, QStringList>(); |
317 | 107 |
} |
108 |
||
315 | 109 |
bool HWNetServer::haveNick(const QString& nick) const |
110 |
{ |
|
111 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
112 |
if((*it)->getClientNick()==nick) { |
|
113 |
return true; |
|
114 |
} |
|
115 |
} |
|
116 |
return false; |
|
117 |
} |
|
118 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
119 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 120 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
121 |
QList<QStringList> lst; |
315 | 122 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
123 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
124 |
lst+=(*it)->getTeamNames(); |
315 | 125 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
126 |
} |
|
127 |
} |
|
128 |
return lst; |
|
129 |
} |
|
130 |
||
131 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
132 |
{ |
|
133 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
134 |
if(it==connclients.end() || *it!=client) return false; |
|
135 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
136 |
if(!(*it)->isReady()) return false; |
|
137 |
} |
|
138 |
return true; |
|
139 |
} |
|
140 |
||
388 | 141 |
void HWNetServer::resetStart() |
142 |
{ |
|
143 |
QList<HWConnectedClient*>::iterator it; |
|
144 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
145 |
(*it)->readyToStart=false; |
|
146 |
} |
|
147 |
} |
|
148 |
||
315 | 149 |
QString HWNetServer::prepareConfig(QStringList lst) |
150 |
{ |
|
151 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
152 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
153 |
if(!(*it)->isReady()) continue; |
|
154 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
155 |
} |
|
156 |
qDebug() << msg; |
|
157 |
return msg; |
|
158 |
} |
|
159 |
||
319 | 160 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 161 |
{ |
162 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
163 |
(*it)->RawSendNet(gameCfg); |
|
164 |
} |
|
165 |
} |
|
166 |
||
319 | 167 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
168 |
{ |
|
169 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
170 |
if(*it==this_cl) continue; |
|
171 |
(*it)->RawSendNet(gameCfg); |
|
172 |
} |
|
173 |
} |
|
174 |
||
315 | 175 |
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) : |
176 |
readyToStart(false), |
|
177 |
m_hwserver(hwserver), |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
178 |
m_client(client) |
315 | 179 |
{ |
180 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
181 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
182 |
} |
|
183 |
||
184 |
HWConnectedClient::~HWConnectedClient() |
|
185 |
{ |
|
186 |
} |
|
187 |
||
188 |
void HWConnectedClient::ClientDisconnect() |
|
189 |
{ |
|
190 |
emit(HWClientDisconnected(this)); |
|
191 |
} |
|
192 |
||
193 |
void HWConnectedClient::ClientRead() |
|
194 |
{ |
|
195 |
try { |
|
196 |
while (m_client->canReadLine()) { |
|
197 |
ParseLine(m_client->readLine().trimmed()); |
|
198 |
} |
|
199 |
} catch(ShouldDisconnectException& e) { |
|
200 |
m_client->close(); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
void HWConnectedClient::ParseLine(const QByteArray & line) |
|
205 |
{ |
|
206 |
QString msg = QString::fromUtf8 (line.data(), line.size()); |
|
207 |
||
208 |
QStringList lst = msg.split(delimeter); |
|
319 | 209 |
if(!lst.size()) return; |
315 | 210 |
if (lst[0] == "NICK") { |
319 | 211 |
if(lst.size()<2) return; |
315 | 212 |
if(m_hwserver->haveNick(lst[1])) { |
213 |
RawSendNet(QString("ERRONEUSNICKNAME")); |
|
214 |
throw ShouldDisconnectException(); |
|
215 |
} |
|
216 |
||
217 |
client_nick=lst[1]; |
|
218 |
qDebug() << "send connected"; |
|
219 |
RawSendNet(QString("CONNECTED")); |
|
326 | 220 |
if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED")); |
335 | 221 |
else { |
349 | 222 |
RawSendNet(QString("SLAVE")); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
223 |
// send teams |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
224 |
QList<QStringList> team_conf=m_hwserver->getTeamsConfig(); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
225 |
for(QList<QStringList>::iterator tmit=team_conf.begin(); tmit!=team_conf.end(); ++tmit) { |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
226 |
RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter))); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
227 |
} |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
228 |
// send config |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
229 |
QMap<QString, QStringList> conf=m_hwserver->getGameCfg(); |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
230 |
for(QMap<QString, QStringList>::iterator it=conf.begin(); it!=conf.end(); ++it) { |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
231 |
RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter))); |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
232 |
} |
335 | 233 |
} |
315 | 234 |
return; |
235 |
} |
|
236 |
if(client_nick=="") return; |
|
237 |
||
238 |
if (lst[0]=="START:") { |
|
239 |
readyToStart=true; |
|
240 |
if(m_hwserver->shouldStart(this)) { |
|
241 |
// start |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
242 |
m_hwserver->sendAll("RUNGAME"); |
388 | 243 |
m_hwserver->resetStart(); |
315 | 244 |
} |
245 |
return; |
|
246 |
} |
|
247 |
||
335 | 248 |
if(lst[0]=="CONFIG_PARAM") { |
249 |
if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :) |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
250 |
else m_gameCfg[lst[1]]=lst.mid(2); |
315 | 251 |
} |
252 |
||
253 |
if(lst[0]=="ADDTEAM:") { |
|
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
254 |
if(lst.size()<11) return; |
315 | 255 |
lst.pop_front(); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
256 |
|
352 | 257 |
// add team ID |
258 |
static unsigned int netTeamID=1; |
|
259 |
lst.insert(1, QString::number(netTeamID++)); |
|
260 |
||
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
261 |
// creating color config for new team |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
262 |
QString colorCfg=QString("CONFIG_PARAM%1TEAM_COLOR%1%2%1%3%1%4").arg(delimeter).arg(lst[0])\ |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
263 |
.arg(netTeamID)\ |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
264 |
.arg(lst.takeAt(2)); |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
265 |
qDebug() << "color config:" << colorCfg; |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
266 |
|
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
267 |
m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
268 |
m_teamsCfg.push_back(lst); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
269 |
|
352 | 270 |
m_hwserver->sendOthers(this, QString("ADDTEAM:")+delimeter+lst.join(QString(delimeter))); |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
271 |
RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1])); |
315 | 272 |
return; |
273 |
} |
|
326 | 274 |
|
347 | 275 |
if(lst[0]=="REMOVETEAM:") { |
276 |
if(lst.size()<2) return; |
|
352 | 277 |
unsigned int netID=removeTeam(lst[1]); |
278 |
m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); |
|
279 |
return; |
|
347 | 280 |
} |
281 |
||
319 | 282 |
m_hwserver->sendOthers(this, msg); |
315 | 283 |
} |
284 |
||
352 | 285 |
unsigned int HWConnectedClient::removeTeam(const QString& tname) |
347 | 286 |
{ |
352 | 287 |
unsigned int netID=0; |
347 | 288 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
289 |
if((*it)[0]==tname) { |
|
352 | 290 |
netID=(*it)[1].toUInt(); |
347 | 291 |
m_teamsCfg.erase(it); |
292 |
break; |
|
293 |
} |
|
294 |
} |
|
352 | 295 |
return netID; |
347 | 296 |
} |
297 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
298 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 299 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
300 |
return m_teamsCfg; |
315 | 301 |
} |
302 |
||
303 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
304 |
{ |
|
305 |
RawSendNet(str.toUtf8()); |
|
306 |
} |
|
307 |
||
308 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
309 |
{ |
|
310 |
m_client->write(buf); |
|
311 |
m_client->write("\n", 1); |
|
312 |
} |
|
313 |
||
314 |
QString HWConnectedClient::getClientNick() const |
|
315 |
{ |
|
316 |
return client_nick; |
|
317 |
} |
|
318 |
||
319 |
bool HWConnectedClient::isReady() const |
|
320 |
{ |
|
321 |
return readyToStart; |
|
322 |
} |
|
323 |
||
324 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
325 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
326 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 327 |
} |