author | displacer |
Tue, 06 Feb 2007 23:18:28 +0000 | |
changeset 404 | 64a62b679b04 |
parent 401 | bcebe3921740 |
child 420 | 6cdfc07dceed |
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 |
{ |
|
399 | 35 |
hhnum=0; |
315 | 36 |
IPCServer = new QTcpServer(this); |
347 | 37 |
if (!IPCServer->listen(QHostAddress::Any, ds_port)) { |
315 | 38 |
QMessageBox::critical(0, tr("Error"), |
39 |
tr("Unable to start the server: %1.") |
|
40 |
.arg(IPCServer->errorString())); |
|
41 |
} |
|
326 | 42 |
|
315 | 43 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
44 |
} |
|
45 |
||
46 |
void HWNetServer::StopServer() |
|
47 |
{ |
|
383 | 48 |
QList<HWConnectedClient*>::iterator it; |
49 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
50 |
ClientDisconnect(*it); |
|
51 |
} |
|
315 | 52 |
IPCServer->close(); |
53 |
} |
|
54 |
||
55 |
void HWNetServer::NewConnection() |
|
56 |
{ |
|
57 |
QTcpSocket* client = IPCServer->nextPendingConnection(); |
|
58 |
if(!client) return; |
|
59 |
connclients.push_back(new HWConnectedClient(this, client)); |
|
326 | 60 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), |
315 | 61 |
this, SLOT(ClientDisconnect(HWConnectedClient*))); |
62 |
} |
|
63 |
||
64 |
void HWNetServer::ClientDisconnect(HWConnectedClient* client) |
|
65 |
{ |
|
66 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
383 | 67 |
if(it==connclients.end()) return; |
350 | 68 |
for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) { |
383 | 69 |
sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1)); |
350 | 70 |
} |
315 | 71 |
connclients.erase(it); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
72 |
//teamChanged(); |
315 | 73 |
} |
74 |
||
75 |
QString HWNetServer::getRunningHostName() const |
|
76 |
{ |
|
77 |
return IPCServer->serverAddress().toString(); |
|
78 |
} |
|
79 |
||
80 |
quint16 HWNetServer::getRunningPort() const |
|
81 |
{ |
|
82 |
return ds_port; |
|
83 |
} |
|
84 |
||
334 | 85 |
HWConnectedClient* HWNetServer::getChiefClient() const |
317 | 86 |
{ |
87 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
334 | 88 |
// watch for first fully connected client (with confirmed nick) |
89 |
if((*it)->getClientNick()!="") return *it; |
|
317 | 90 |
} |
334 | 91 |
return 0; |
92 |
} |
|
93 |
||
94 |
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const |
|
95 |
{ |
|
96 |
return getChiefClient()==cl; |
|
97 |
} |
|
98 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
99 |
QMap<QString, QStringList> HWNetServer::getGameCfg() const |
334 | 100 |
{ |
404 | 101 |
return m_gameCfg; |
102 |
/* |
|
334 | 103 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
335 | 104 |
if(isChiefClient(*it)) { |
105 |
return (*it)->m_gameCfg; |
|
106 |
} |
|
334 | 107 |
} |
108 |
// 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
|
109 |
return QMap<QString, QStringList>(); |
404 | 110 |
*/ |
317 | 111 |
} |
112 |
||
315 | 113 |
bool HWNetServer::haveNick(const QString& nick) const |
114 |
{ |
|
115 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
116 |
if((*it)->getClientNick()==nick) { |
|
117 |
return true; |
|
118 |
} |
|
119 |
} |
|
120 |
return false; |
|
121 |
} |
|
122 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
123 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 124 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
125 |
QList<QStringList> lst; |
315 | 126 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
127 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
128 |
lst+=(*it)->getTeamNames(); |
315 | 129 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
130 |
} |
|
131 |
} |
|
132 |
return lst; |
|
133 |
} |
|
134 |
||
135 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
136 |
{ |
|
137 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
138 |
if(it==connclients.end() || *it!=client) return false; |
|
139 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
140 |
if(!(*it)->isReady()) return false; |
|
141 |
} |
|
142 |
return true; |
|
143 |
} |
|
144 |
||
388 | 145 |
void HWNetServer::resetStart() |
146 |
{ |
|
147 |
QList<HWConnectedClient*>::iterator it; |
|
148 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
149 |
(*it)->readyToStart=false; |
|
150 |
} |
|
151 |
} |
|
152 |
||
315 | 153 |
QString HWNetServer::prepareConfig(QStringList lst) |
154 |
{ |
|
155 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
156 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
157 |
if(!(*it)->isReady()) continue; |
|
158 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
159 |
} |
|
160 |
qDebug() << msg; |
|
161 |
return msg; |
|
162 |
} |
|
163 |
||
319 | 164 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 165 |
{ |
166 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
167 |
(*it)->RawSendNet(gameCfg); |
|
168 |
} |
|
169 |
} |
|
170 |
||
319 | 171 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
172 |
{ |
|
173 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
174 |
if(*it==this_cl) continue; |
|
175 |
(*it)->RawSendNet(gameCfg); |
|
176 |
} |
|
177 |
} |
|
178 |
||
315 | 179 |
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) : |
180 |
readyToStart(false), |
|
181 |
m_hwserver(hwserver), |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
182 |
m_client(client) |
315 | 183 |
{ |
184 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
185 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
186 |
} |
|
187 |
||
188 |
HWConnectedClient::~HWConnectedClient() |
|
189 |
{ |
|
190 |
} |
|
191 |
||
192 |
void HWConnectedClient::ClientDisconnect() |
|
193 |
{ |
|
194 |
emit(HWClientDisconnected(this)); |
|
195 |
} |
|
196 |
||
197 |
void HWConnectedClient::ClientRead() |
|
198 |
{ |
|
199 |
try { |
|
200 |
while (m_client->canReadLine()) { |
|
201 |
ParseLine(m_client->readLine().trimmed()); |
|
202 |
} |
|
203 |
} catch(ShouldDisconnectException& e) { |
|
204 |
m_client->close(); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
void HWConnectedClient::ParseLine(const QByteArray & line) |
|
209 |
{ |
|
210 |
QString msg = QString::fromUtf8 (line.data(), line.size()); |
|
211 |
||
212 |
QStringList lst = msg.split(delimeter); |
|
319 | 213 |
if(!lst.size()) return; |
315 | 214 |
if (lst[0] == "NICK") { |
319 | 215 |
if(lst.size()<2) return; |
315 | 216 |
if(m_hwserver->haveNick(lst[1])) { |
217 |
RawSendNet(QString("ERRONEUSNICKNAME")); |
|
218 |
throw ShouldDisconnectException(); |
|
219 |
} |
|
220 |
||
221 |
client_nick=lst[1]; |
|
222 |
qDebug() << "send connected"; |
|
223 |
RawSendNet(QString("CONNECTED")); |
|
326 | 224 |
if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED")); |
335 | 225 |
else { |
349 | 226 |
RawSendNet(QString("SLAVE")); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
227 |
// send teams |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
228 |
QList<QStringList> team_conf=m_hwserver->getTeamsConfig(); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
229 |
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
|
230 |
RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter))); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
231 |
} |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
232 |
// send config |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
233 |
QMap<QString, QStringList> conf=m_hwserver->getGameCfg(); |
401 | 234 |
qDebug() << "Config:"; |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
235 |
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
|
236 |
RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().join(QString(delimeter))); |
401 | 237 |
qDebug() << QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value().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
|
238 |
} |
335 | 239 |
} |
315 | 240 |
return; |
241 |
} |
|
242 |
if(client_nick=="") return; |
|
243 |
||
244 |
if (lst[0]=="START:") { |
|
245 |
readyToStart=true; |
|
246 |
if(m_hwserver->shouldStart(this)) { |
|
247 |
// start |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
248 |
m_hwserver->sendAll("RUNGAME"); |
388 | 249 |
m_hwserver->resetStart(); |
315 | 250 |
} |
251 |
return; |
|
252 |
} |
|
253 |
||
335 | 254 |
if(lst[0]=="CONFIG_PARAM") { |
255 |
if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :) |
|
404 | 256 |
else m_hwserver->m_gameCfg[lst[1]]=lst.mid(2); |
401 | 257 |
qDebug() << msg; |
315 | 258 |
} |
259 |
||
260 |
if(lst[0]=="ADDTEAM:") { |
|
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
261 |
if(lst.size()<11) return; |
315 | 262 |
lst.pop_front(); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
263 |
|
352 | 264 |
// add team ID |
401 | 265 |
static unsigned int netTeamID=0; |
266 |
lst.insert(1, QString::number(++netTeamID)); |
|
352 | 267 |
|
399 | 268 |
// hedgehogs num count |
269 |
int maxAdd=18-m_hwserver->hhnum; |
|
270 |
if (maxAdd<=0) return; // reject command |
|
271 |
int toAdd=maxAdd<4 ? maxAdd : 4; |
|
272 |
m_hwserver->hhnum+=toAdd; |
|
273 |
// hedgehogs num config |
|
274 |
QString hhnumCfg=QString("CONFIG_PARAM%1HHNUM+%2+%3%1%4").arg(delimeter).arg(lst[0])\ |
|
275 |
.arg(netTeamID)\ |
|
276 |
.arg(toAdd); |
|
277 |
||
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
278 |
// creating color config for new team |
401 | 279 |
QString colorCfg=QString("CONFIG_PARAM%1TEAM_COLOR+%2+%3%1%4").arg(delimeter).arg(lst[0])\ |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
280 |
.arg(netTeamID)\ |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
281 |
.arg(lst.takeAt(2)); |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
282 |
qDebug() << "color config:" << colorCfg; |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
283 |
|
404 | 284 |
m_hwserver->m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2); |
285 |
m_hwserver->m_gameCfg[hhnumCfg.split(delimeter)[1]]=hhnumCfg.split(delimeter).mid(2); |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
286 |
m_teamsCfg.push_back(lst); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
287 |
|
352 | 288 |
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
|
289 |
RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1])); |
404 | 290 |
m_hwserver->sendAll(colorCfg); |
399 | 291 |
m_hwserver->sendAll(hhnumCfg); |
315 | 292 |
return; |
293 |
} |
|
326 | 294 |
|
347 | 295 |
if(lst[0]=="REMOVETEAM:") { |
296 |
if(lst.size()<2) return; |
|
399 | 297 |
|
404 | 298 |
for(QMap<QString, QStringList>::iterator it=m_hwserver->m_gameCfg.begin(); it!=m_hwserver->m_gameCfg.end(); ++it) { |
399 | 299 |
QStringList hhTmpList=it.key().split('+'); |
300 |
if(hhTmpList[0] == "HHNUM") { |
|
301 |
qDebug() << "hhnum config found"; |
|
302 |
if(hhTmpList[1]==lst[1]) { |
|
303 |
qDebug() << "hhnum config team found with: " << lst[1] << ":" << it.value()[0].toUInt(); |
|
304 |
m_hwserver->hhnum-=it.value()[0].toUInt(); |
|
305 |
break; |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
||
352 | 310 |
unsigned int netID=removeTeam(lst[1]); |
311 |
m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); |
|
401 | 312 |
qDebug() << QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID); |
352 | 313 |
return; |
347 | 314 |
} |
315 |
||
319 | 316 |
m_hwserver->sendOthers(this, msg); |
315 | 317 |
} |
318 |
||
352 | 319 |
unsigned int HWConnectedClient::removeTeam(const QString& tname) |
347 | 320 |
{ |
352 | 321 |
unsigned int netID=0; |
347 | 322 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
323 |
if((*it)[0]==tname) { |
|
352 | 324 |
netID=(*it)[1].toUInt(); |
347 | 325 |
m_teamsCfg.erase(it); |
326 |
break; |
|
327 |
} |
|
328 |
} |
|
352 | 329 |
return netID; |
347 | 330 |
} |
331 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
332 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 333 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
334 |
return m_teamsCfg; |
315 | 335 |
} |
336 |
||
337 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
338 |
{ |
|
339 |
RawSendNet(str.toUtf8()); |
|
340 |
} |
|
341 |
||
342 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
343 |
{ |
|
344 |
m_client->write(buf); |
|
345 |
m_client->write("\n", 1); |
|
346 |
} |
|
347 |
||
348 |
QString HWConnectedClient::getClientNick() const |
|
349 |
{ |
|
350 |
return client_nick; |
|
351 |
} |
|
352 |
||
353 |
bool HWConnectedClient::isReady() const |
|
354 |
{ |
|
355 |
return readyToStart; |
|
356 |
} |
|
357 |
||
358 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
359 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
360 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 361 |
} |