author | displacer |
Sun, 04 Feb 2007 21:30:23 +0000 | |
changeset 390 | dca6bd77d71d |
parent 388 | dcf5335940bd |
child 391 | e7565bb852a2 |
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 |
qDebug() << "line " << msg << " received"; |
|
209 |
||
210 |
QStringList lst = msg.split(delimeter); |
|
319 | 211 |
if(!lst.size()) return; |
315 | 212 |
if (lst[0] == "NICK") { |
319 | 213 |
if(lst.size()<2) return; |
315 | 214 |
if(m_hwserver->haveNick(lst[1])) { |
215 |
RawSendNet(QString("ERRONEUSNICKNAME")); |
|
216 |
throw ShouldDisconnectException(); |
|
217 |
} |
|
218 |
||
219 |
client_nick=lst[1]; |
|
220 |
qDebug() << "send connected"; |
|
221 |
RawSendNet(QString("CONNECTED")); |
|
326 | 222 |
if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED")); |
335 | 223 |
else { |
349 | 224 |
RawSendNet(QString("SLAVE")); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
225 |
// send teams |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
226 |
QList<QStringList> team_conf=m_hwserver->getTeamsConfig(); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
227 |
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
|
228 |
RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter))); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
229 |
} |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
230 |
// send config |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
231 |
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
|
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))); |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
234 |
} |
335 | 235 |
} |
315 | 236 |
return; |
237 |
} |
|
238 |
if(client_nick=="") return; |
|
239 |
||
240 |
if (lst[0]=="START:") { |
|
241 |
readyToStart=true; |
|
242 |
if(m_hwserver->shouldStart(this)) { |
|
243 |
// start |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
244 |
m_hwserver->sendAll("RUNGAME"); |
388 | 245 |
m_hwserver->resetStart(); |
315 | 246 |
} |
247 |
return; |
|
248 |
} |
|
249 |
||
335 | 250 |
if(lst[0]=="CONFIG_PARAM") { |
251 |
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
|
252 |
else m_gameCfg[lst[1]]=lst.mid(2); |
315 | 253 |
} |
254 |
||
255 |
if(lst[0]=="ADDTEAM:") { |
|
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
256 |
if(lst.size()<11) return; |
315 | 257 |
lst.pop_front(); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
258 |
|
352 | 259 |
// add team ID |
260 |
static unsigned int netTeamID=1; |
|
261 |
lst.insert(1, QString::number(netTeamID++)); |
|
262 |
||
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
263 |
// creating color config for new team |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
264 |
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
|
265 |
.arg(netTeamID)\ |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
266 |
.arg(lst.takeAt(2)); |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
267 |
qDebug() << "color config:" << colorCfg; |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
268 |
|
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
269 |
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
|
270 |
m_teamsCfg.push_back(lst); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
271 |
|
352 | 272 |
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
|
273 |
RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1])); |
315 | 274 |
return; |
275 |
} |
|
326 | 276 |
|
347 | 277 |
if(lst[0]=="REMOVETEAM:") { |
278 |
if(lst.size()<2) return; |
|
352 | 279 |
unsigned int netID=removeTeam(lst[1]); |
280 |
m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); |
|
281 |
return; |
|
347 | 282 |
} |
283 |
||
319 | 284 |
m_hwserver->sendOthers(this, msg); |
315 | 285 |
} |
286 |
||
352 | 287 |
unsigned int HWConnectedClient::removeTeam(const QString& tname) |
347 | 288 |
{ |
352 | 289 |
unsigned int netID=0; |
347 | 290 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
291 |
if((*it)[0]==tname) { |
|
352 | 292 |
netID=(*it)[1].toUInt(); |
347 | 293 |
m_teamsCfg.erase(it); |
294 |
break; |
|
295 |
} |
|
296 |
} |
|
352 | 297 |
return netID; |
347 | 298 |
} |
299 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
300 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 301 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
302 |
return m_teamsCfg; |
315 | 303 |
} |
304 |
||
305 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
306 |
{ |
|
307 |
RawSendNet(str.toUtf8()); |
|
308 |
} |
|
309 |
||
310 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
311 |
{ |
|
312 |
m_client->write(buf); |
|
313 |
m_client->write("\n", 1); |
|
314 |
} |
|
315 |
||
316 |
QString HWConnectedClient::getClientNick() const |
|
317 |
{ |
|
318 |
return client_nick; |
|
319 |
} |
|
320 |
||
321 |
bool HWConnectedClient::isReady() const |
|
322 |
{ |
|
323 |
return readyToStart; |
|
324 |
} |
|
325 |
||
326 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
327 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
328 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 329 |
} |