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