author | displacer |
Tue, 06 Feb 2007 19:34:21 +0000 | |
changeset 399 | c7da1bd32b4e |
parent 391 | e7565bb852a2 |
child 401 | bcebe3921740 |
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(); |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
231 |
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
|
232 |
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
|
233 |
} |
335 | 234 |
} |
315 | 235 |
return; |
236 |
} |
|
237 |
if(client_nick=="") return; |
|
238 |
||
239 |
if (lst[0]=="START:") { |
|
240 |
readyToStart=true; |
|
241 |
if(m_hwserver->shouldStart(this)) { |
|
242 |
// start |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
243 |
m_hwserver->sendAll("RUNGAME"); |
388 | 244 |
m_hwserver->resetStart(); |
315 | 245 |
} |
246 |
return; |
|
247 |
} |
|
248 |
||
335 | 249 |
if(lst[0]=="CONFIG_PARAM") { |
250 |
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
|
251 |
else m_gameCfg[lst[1]]=lst.mid(2); |
315 | 252 |
} |
253 |
||
254 |
if(lst[0]=="ADDTEAM:") { |
|
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
255 |
if(lst.size()<11) return; |
315 | 256 |
lst.pop_front(); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
257 |
|
352 | 258 |
// add team ID |
259 |
static unsigned int netTeamID=1; |
|
260 |
lst.insert(1, QString::number(netTeamID++)); |
|
261 |
||
399 | 262 |
// hedgehogs num count |
263 |
int maxAdd=18-m_hwserver->hhnum; |
|
264 |
if (maxAdd<=0) return; // reject command |
|
265 |
int toAdd=maxAdd<4 ? maxAdd : 4; |
|
266 |
m_hwserver->hhnum+=toAdd; |
|
267 |
// hedgehogs num config |
|
268 |
QString hhnumCfg=QString("CONFIG_PARAM%1HHNUM+%2+%3%1%4").arg(delimeter).arg(lst[0])\ |
|
269 |
.arg(netTeamID)\ |
|
270 |
.arg(toAdd); |
|
271 |
||
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
272 |
// creating color config for new team |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
273 |
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
|
274 |
.arg(netTeamID)\ |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
275 |
.arg(lst.takeAt(2)); |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
276 |
qDebug() << "color config:" << colorCfg; |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
277 |
|
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
278 |
m_gameCfg[colorCfg.split(delimeter)[1]]=colorCfg.split(delimeter).mid(2); |
399 | 279 |
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
|
280 |
m_teamsCfg.push_back(lst); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
281 |
|
352 | 282 |
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
|
283 |
RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1])); |
399 | 284 |
m_hwserver->sendAll(hhnumCfg); |
315 | 285 |
return; |
286 |
} |
|
326 | 287 |
|
347 | 288 |
if(lst[0]=="REMOVETEAM:") { |
289 |
if(lst.size()<2) return; |
|
399 | 290 |
|
291 |
for(QMap<QString, QStringList>::iterator it=m_gameCfg.begin(); it!=m_gameCfg.end(); ++it) { |
|
292 |
QStringList hhTmpList=it.key().split('+'); |
|
293 |
if(hhTmpList[0] == "HHNUM") { |
|
294 |
qDebug() << "hhnum config found"; |
|
295 |
if(hhTmpList[1]==lst[1]) { |
|
296 |
qDebug() << "hhnum config team found with: " << lst[1] << ":" << it.value()[0].toUInt(); |
|
297 |
m_hwserver->hhnum-=it.value()[0].toUInt(); |
|
298 |
break; |
|
299 |
} |
|
300 |
} |
|
301 |
} |
|
302 |
||
352 | 303 |
unsigned int netID=removeTeam(lst[1]); |
304 |
m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); |
|
305 |
return; |
|
347 | 306 |
} |
307 |
||
319 | 308 |
m_hwserver->sendOthers(this, msg); |
315 | 309 |
} |
310 |
||
352 | 311 |
unsigned int HWConnectedClient::removeTeam(const QString& tname) |
347 | 312 |
{ |
352 | 313 |
unsigned int netID=0; |
347 | 314 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
315 |
if((*it)[0]==tname) { |
|
352 | 316 |
netID=(*it)[1].toUInt(); |
347 | 317 |
m_teamsCfg.erase(it); |
318 |
break; |
|
319 |
} |
|
320 |
} |
|
352 | 321 |
return netID; |
347 | 322 |
} |
323 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
324 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 325 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
326 |
return m_teamsCfg; |
315 | 327 |
} |
328 |
||
329 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
330 |
{ |
|
331 |
RawSendNet(str.toUtf8()); |
|
332 |
} |
|
333 |
||
334 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
335 |
{ |
|
336 |
m_client->write(buf); |
|
337 |
m_client->write("\n", 1); |
|
338 |
} |
|
339 |
||
340 |
QString HWConnectedClient::getClientNick() const |
|
341 |
{ |
|
342 |
return client_nick; |
|
343 |
} |
|
344 |
||
345 |
bool HWConnectedClient::isReady() const |
|
346 |
{ |
|
347 |
return readyToStart; |
|
348 |
} |
|
349 |
||
350 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
351 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
352 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 353 |
} |