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