author | displacer |
Sun, 21 Jan 2007 19:53:25 +0000 | |
changeset 352 | 4665bfe25470 |
parent 350 | c3ccec3834e8 |
child 354 | 60e4af0a4375 |
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 |
||
335 | 93 |
QMap<QString, QString> 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 |
|
335 | 101 |
return QMap<QString, QString>(); |
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 config |
335 | 213 |
QMap<QString, QString> conf=m_hwserver->getGameCfg(); |
214 |
for(QMap<QString, QString>::iterator it=conf.begin(); it!=conf.end(); ++it) { |
|
215 |
RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value()); |
|
216 |
} |
|
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 |
} |
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 :) |
|
238 |
else m_gameCfg[lst[1]]=lst[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))); |
315 | 250 |
return; |
251 |
} |
|
326 | 252 |
|
347 | 253 |
if(lst[0]=="REMOVETEAM:") { |
254 |
if(lst.size()<2) return; |
|
352 | 255 |
unsigned int netID=removeTeam(lst[1]); |
256 |
m_hwserver->sendOthers(this, QString("REMOVETEAM:")+delimeter+lst[1]+delimeter+QString::number(netID)); |
|
257 |
return; |
|
347 | 258 |
} |
259 |
||
319 | 260 |
m_hwserver->sendOthers(this, msg); |
315 | 261 |
} |
262 |
||
352 | 263 |
unsigned int HWConnectedClient::removeTeam(const QString& tname) |
347 | 264 |
{ |
352 | 265 |
unsigned int netID=0; |
347 | 266 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
267 |
if((*it)[0]==tname) { |
|
352 | 268 |
netID=(*it)[1].toUInt(); |
347 | 269 |
m_teamsCfg.erase(it); |
270 |
break; |
|
271 |
} |
|
272 |
} |
|
352 | 273 |
return netID; |
347 | 274 |
} |
275 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
276 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 277 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
278 |
return m_teamsCfg; |
315 | 279 |
} |
280 |
||
281 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
282 |
{ |
|
283 |
RawSendNet(str.toUtf8()); |
|
284 |
} |
|
285 |
||
286 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
287 |
{ |
|
288 |
m_client->write(buf); |
|
289 |
m_client->write("\n", 1); |
|
290 |
} |
|
291 |
||
292 |
QString HWConnectedClient::getClientNick() const |
|
293 |
{ |
|
294 |
return client_nick; |
|
295 |
} |
|
296 |
||
297 |
bool HWConnectedClient::isReady() const |
|
298 |
{ |
|
299 |
return readyToStart; |
|
300 |
} |
|
301 |
||
302 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
303 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
304 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 305 |
} |