author | displacer |
Mon, 15 Jan 2007 18:45:07 +0000 | |
changeset 339 | 7535ab6c3820 |
parent 338 | d1e75dcd285f |
child 347 | 6521e1b2cd40 |
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); |
|
36 |
if (!IPCServer->listen(QHostAddress::LocalHost, ds_port)) { |
|
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 { |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
208 |
// send config |
335 | 209 |
QMap<QString, QString> conf=m_hwserver->getGameCfg(); |
210 |
for(QMap<QString, QString>::iterator it=conf.begin(); it!=conf.end(); ++it) { |
|
211 |
RawSendNet(QString("CONFIG_PARAM")+delimeter+it.key()+delimeter+it.value()); |
|
212 |
} |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
213 |
// send teams |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
214 |
QList<QStringList> team_conf=m_hwserver->getTeamsConfig(); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
215 |
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
|
216 |
RawSendNet(QString("ADDTEAM:")+delimeter+tmit->join(QString(delimeter))); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
217 |
} |
335 | 218 |
} |
315 | 219 |
return; |
220 |
} |
|
221 |
if(client_nick=="") return; |
|
222 |
||
223 |
if (lst[0]=="START:") { |
|
224 |
readyToStart=true; |
|
225 |
if(m_hwserver->shouldStart(this)) { |
|
226 |
// start |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
338
diff
changeset
|
227 |
m_hwserver->sendAll("RUNGAME"); |
315 | 228 |
} |
229 |
return; |
|
230 |
} |
|
231 |
||
335 | 232 |
if(lst[0]=="CONFIG_PARAM") { |
233 |
if(!m_hwserver->isChiefClient(this) || lst.size()<3) return; // error or permission denied :) |
|
234 |
else m_gameCfg[lst[1]]=lst[2]; |
|
315 | 235 |
} |
236 |
||
237 |
if(lst[0]=="ADDTEAM:") { |
|
319 | 238 |
if(lst.size()<10) return; |
315 | 239 |
lst.pop_front(); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
240 |
m_teamsCfg.push_back(lst); |
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
241 |
m_hwserver->sendOthers(this, msg); |
315 | 242 |
return; |
243 |
} |
|
326 | 244 |
|
319 | 245 |
m_hwserver->sendOthers(this, msg); |
315 | 246 |
} |
247 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
248 |
QList<QStringList> HWConnectedClient::getTeamNames() const |
315 | 249 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
250 |
return m_teamsCfg; |
315 | 251 |
} |
252 |
||
253 |
void HWConnectedClient::RawSendNet(const QString & str) |
|
254 |
{ |
|
255 |
RawSendNet(str.toUtf8()); |
|
256 |
} |
|
257 |
||
258 |
void HWConnectedClient::RawSendNet(const QByteArray & buf) |
|
259 |
{ |
|
260 |
m_client->write(buf); |
|
261 |
m_client->write("\n", 1); |
|
262 |
} |
|
263 |
||
264 |
QString HWConnectedClient::getClientNick() const |
|
265 |
{ |
|
266 |
return client_nick; |
|
267 |
} |
|
268 |
||
269 |
bool HWConnectedClient::isReady() const |
|
270 |
{ |
|
271 |
return readyToStart; |
|
272 |
} |
|
273 |
||
274 |
QString HWConnectedClient::getHedgehogsDescription() const |
|
275 |
{ |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
276 |
return QString();//pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter); |
315 | 277 |
} |