author | unc0rr |
Sun, 21 Jan 2007 19:51:02 +0000 | |
changeset 351 | 29bc9c36ad5f |
parent 350 | c3ccec3834e8 |
child 352 | 4665bfe25470 |
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(); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
244 |
m_teamsCfg.push_back(lst); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
245 |
m_hwserver->sendOthers(this, msg); |
315 | 246 |
return; |
247 |
} |
|
326 | 248 |
|
347 | 249 |
if(lst[0]=="REMOVETEAM:") { |
250 |
if(lst.size()<2) return; |
|
251 |
removeTeam(lst[1]); |
|
252 |
} |
|
253 |
||
319 | 254 |
m_hwserver->sendOthers(this, msg); |
315 | 255 |
} |
256 |
||
347 | 257 |
void HWConnectedClient::removeTeam(const QString& tname) |
258 |
{ |
|
259 |
for(QList<QStringList>::iterator it=m_teamsCfg.begin(); it!=m_teamsCfg.end(); ++it) { |
|
260 |
if((*it)[0]==tname) { |
|
261 |
m_teamsCfg.erase(it); |
|
262 |
break; |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
267 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 268 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
269 |
return m_teamsCfg; |
315 | 270 |
} |
271 |
||
272 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
273 |
{ |
|
274 |
RawSendNet(str.toUtf8()); |
|
275 |
} |
|
276 |
||
277 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
278 |
{ |
|
279 |
m_client->write(buf); |
|
280 |
m_client->write("\n", 1); |
|
281 |
} |
|
282 |
||
283 |
QString HWConnectedClient::getClientNick() const |
|
284 |
{ |
|
285 |
return client_nick; |
|
286 |
} |
|
287 |
||
288 |
bool HWConnectedClient::isReady() const |
|
289 |
{ |
|
290 |
return readyToStart; |
|
291 |
} |
|
292 |
||
293 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
294 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
295 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 296 |
} |