author | displacer |
Sun, 04 Feb 2007 14:35:28 +0000 | |
changeset 383 | 09a8795105a4 |
parent 382 | e7220e48ead1 |
child 388 | dcf5335940bd |
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 |
||
141 |
QString HWNetServer::prepareConfig(QStringList lst) |
|
142 |
{ |
|
143 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
144 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
145 |
if(!(*it)->isReady()) continue; |
|
146 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
147 |
} |
|
148 |
qDebug() << msg; |
|
149 |
return msg; |
|
150 |
} |
|
151 |
||
319 | 152 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 153 |
{ |
154 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
155 |
(*it)->RawSendNet(gameCfg); |
|
156 |
} |
|
157 |
} |
|
158 |
||
319 | 159 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
160 |
{ |
|
161 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
162 |
if(*it==this_cl) continue; |
|
163 |
(*it)->RawSendNet(gameCfg); |
|
164 |
} |
|
165 |
} |
|
166 |
||
315 | 167 |
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) : |
168 |
readyToStart(false), |
|
169 |
m_hwserver(hwserver), |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
170 |
m_client(client) |
315 | 171 |
{ |
172 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect())); |
|
173 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead())); |
|
174 |
} |
|
175 |
||
176 |
HWConnectedClient::~HWConnectedClient() |
|
177 |
{ |
|
178 |
} |
|
179 |
||
180 |
void HWConnectedClient::ClientDisconnect() |
|
181 |
{ |
|
182 |
emit(HWClientDisconnected(this)); |
|
183 |
} |
|
184 |
||
185 |
void HWConnectedClient::ClientRead() |
|
186 |
{ |
|
187 |
try { |
|
188 |
while (m_client->canReadLine()) { |
|
189 |
ParseLine(m_client->readLine().trimmed()); |
|
190 |
} |
|
191 |
} catch(ShouldDisconnectException& e) { |
|
192 |
m_client->close(); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
void HWConnectedClient::ParseLine(const QByteArray & line) |
|
197 |
{ |
|
198 |
QString msg = QString::fromUtf8 (line.data(), line.size()); |
|
199 |
||
200 |
qDebug() << "line " << msg << " received"; |
|
201 |
||
202 |
QStringList lst = msg.split(delimeter); |
|
319 | 203 |
if(!lst.size()) return; |
315 | 204 |
if (lst[0] == "NICK") { |
319 | 205 |
if(lst.size()<2) return; |
315 | 206 |
if(m_hwserver->haveNick(lst[1])) { |
207 |
RawSendNet(QString("ERRONEUSNICKNAME")); |
|
208 |
throw ShouldDisconnectException(); |
|
209 |
} |
|
210 |
||
211 |
client_nick=lst[1]; |
|
212 |
qDebug() << "send connected"; |
|
213 |
RawSendNet(QString("CONNECTED")); |
|
326 | 214 |
if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED")); |
335 | 215 |
else { |
349 | 216 |
RawSendNet(QString("SLAVE")); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
217 |
// send teams |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
218 |
QList<QStringList> team_conf=m_hwserver->getTeamsConfig(); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
219 |
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
|
220 |
RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter))); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
221 |
} |
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
222 |
// send config |
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
223 |
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
|
224 |
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
|
225 |
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
|
226 |
} |
335 | 227 |
} |
315 | 228 |
return; |
229 |
} |
|
230 |
if(client_nick=="") return; |
|
231 |
||
232 |
if (lst[0]=="START:") { |
|
233 |
readyToStart=true; |
|
234 |
if(m_hwserver->shouldStart(this)) { |
|
235 |
// start |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
236 |
m_hwserver->sendAll("RUNGAME"); |
315 | 237 |
} |
238 |
return; |
|
239 |
} |
|
240 |
||
335 | 241 |
if(lst[0]=="CONFIG_PARAM") { |
242 |
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
|
243 |
else m_gameCfg[lst[1]]=lst.mid(2); |
315 | 244 |
} |
245 |
||
246 |
if(lst[0]=="ADDTEAM:") { |
|
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
247 |
if(lst.size()<11) return; |
315 | 248 |
lst.pop_front(); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
249 |
|
352 | 250 |
// add team ID |
251 |
static unsigned int netTeamID=1; |
|
252 |
lst.insert(1, QString::number(netTeamID++)); |
|
253 |
||
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
254 |
// creating color config for new team |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
255 |
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
|
256 |
.arg(netTeamID)\ |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
257 |
.arg(lst.takeAt(2)); |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
258 |
qDebug() << "color config:" << colorCfg; |
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
259 |
|
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
260 |
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
|
261 |
m_teamsCfg.push_back(lst); |
382
e7220e48ead1
colors changing config fully working (still need disabling in slaves)
displacer
parents:
354
diff
changeset
|
262 |
|
352 | 263 |
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
|
264 |
RawSendNet(QString("TEAM_ACCEPTED%1%2%1%3").arg(delimeter).arg(lst[0]).arg(lst[1])); |
315 | 265 |
return; |
266 |
} |
|
326 | 267 |
|
347 | 268 |
if(lst[0]=="REMOVETEAM:") { |
269 |
if(lst.size()<2) return; |
|
352 | 270 |
unsigned int netID=removeTeam(lst[1]); |
271 |
m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); |
|
272 |
return; |
|
347 | 273 |
} |
274 |
||
319 | 275 |
m_hwserver->sendOthers(this, msg); |
315 | 276 |
} |
277 |
||
352 | 278 |
unsigned int HWConnectedClient::removeTeam(const QString& tname) |
347 | 279 |
{ |
352 | 280 |
unsigned int netID=0; |
347 | 281 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
282 |
if((*it)[0]==tname) { |
|
352 | 283 |
netID=(*it)[1].toUInt(); |
347 | 284 |
m_teamsCfg.erase(it); |
285 |
break; |
|
286 |
} |
|
287 |
} |
|
352 | 288 |
return netID; |
347 | 289 |
} |
290 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
291 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 292 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
293 |
return m_teamsCfg; |
315 | 294 |
} |
295 |
||
296 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
297 |
{ |
|
298 |
RawSendNet(str.toUtf8()); |
|
299 |
} |
|
300 |
||
301 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
302 |
{ |
|
303 |
m_client->write(buf); |
|
304 |
m_client->write("\n", 1); |
|
305 |
} |
|
306 |
||
307 |
QString HWConnectedClient::getClientNick() const |
|
308 |
{ |
|
309 |
return client_nick; |
|
310 |
} |
|
311 |
||
312 |
bool HWConnectedClient::isReady() const |
|
313 |
{ |
|
314 |
return readyToStart; |
|
315 |
} |
|
316 |
||
317 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
318 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
319 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 320 |
} |