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