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);
|
|
63 |
teamChanged();
|
|
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 |
|
|
90 |
QStringList HWNetServer::getGameCfg() const
|
|
91 |
{
|
|
92 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
93 |
if(isChiefClient(*it)) return (*it)->gameCfg;
|
|
94 |
}
|
|
95 |
// error happened if we are here
|
|
96 |
return QStringList();
|
317
|
97 |
}
|
|
98 |
|
315
|
99 |
bool HWNetServer::haveNick(const QString& nick) const
|
|
100 |
{
|
|
101 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
102 |
if((*it)->getClientNick()==nick) {
|
|
103 |
return true;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
return false;
|
|
107 |
}
|
|
108 |
|
|
109 |
QStringList HWNetServer::getTeams() const
|
|
110 |
{
|
|
111 |
QStringList lst;
|
|
112 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
113 |
try {
|
|
114 |
lst.push_back((*it)->getTeamName());
|
|
115 |
} catch(HWConnectedClient::NoTeamNameException& e) {
|
|
116 |
}
|
|
117 |
}
|
|
118 |
return lst;
|
|
119 |
}
|
|
120 |
|
|
121 |
void HWNetServer::teamChanged()
|
|
122 |
{
|
|
123 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
124 |
(*it)->teamChangedNotify();
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
bool HWNetServer::shouldStart(HWConnectedClient* client)
|
|
129 |
{
|
|
130 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client);
|
|
131 |
if(it==connclients.end() || *it!=client) return false;
|
|
132 |
for(it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
133 |
if(!(*it)->isReady()) return false;
|
|
134 |
}
|
|
135 |
return true;
|
|
136 |
}
|
|
137 |
|
|
138 |
QString HWNetServer::prepareConfig(QStringList lst)
|
|
139 |
{
|
|
140 |
QString msg=lst.join((QString)delimeter)+delimeter;
|
|
141 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
142 |
if(!(*it)->isReady()) continue;
|
|
143 |
msg+=(*it)->getHedgehogsDescription()+delimeter;
|
|
144 |
}
|
|
145 |
qDebug() << msg;
|
|
146 |
return msg;
|
|
147 |
}
|
|
148 |
|
319
|
149 |
void HWNetServer::sendAll(QString gameCfg)
|
317
|
150 |
{
|
|
151 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
152 |
(*it)->RawSendNet(gameCfg);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
319
|
156 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg)
|
|
157 |
{
|
|
158 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
159 |
if(*it==this_cl) continue;
|
|
160 |
(*it)->RawSendNet(gameCfg);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
315
|
164 |
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
|
|
165 |
readyToStart(false),
|
|
166 |
m_hwserver(hwserver),
|
|
167 |
m_client(client),
|
|
168 |
pclent_team(0)
|
|
169 |
{
|
|
170 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
171 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
172 |
}
|
|
173 |
|
|
174 |
HWConnectedClient::~HWConnectedClient()
|
|
175 |
{
|
|
176 |
if(pclent_team) delete pclent_team;
|
|
177 |
}
|
|
178 |
|
|
179 |
void HWConnectedClient::ClientDisconnect()
|
|
180 |
{
|
|
181 |
emit(HWClientDisconnected(this));
|
|
182 |
}
|
|
183 |
|
|
184 |
void HWConnectedClient::ClientRead()
|
|
185 |
{
|
|
186 |
try {
|
|
187 |
while (m_client->canReadLine()) {
|
|
188 |
ParseLine(m_client->readLine().trimmed());
|
|
189 |
}
|
|
190 |
} catch(ShouldDisconnectException& e) {
|
|
191 |
m_client->close();
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
void HWConnectedClient::ParseLine(const QByteArray & line)
|
|
196 |
{
|
|
197 |
QString msg = QString::fromUtf8 (line.data(), line.size());
|
|
198 |
|
|
199 |
qDebug() << "line " << msg << " received";
|
|
200 |
|
|
201 |
QStringList lst = msg.split(delimeter);
|
319
|
202 |
if(!lst.size()) return;
|
315
|
203 |
if (lst[0] == "NICK") {
|
319
|
204 |
if(lst.size()<2) return;
|
315
|
205 |
if(m_hwserver->haveNick(lst[1])) {
|
|
206 |
RawSendNet(QString("ERRONEUSNICKNAME"));
|
|
207 |
throw ShouldDisconnectException();
|
|
208 |
}
|
|
209 |
|
|
210 |
client_nick=lst[1];
|
|
211 |
qDebug() << "send connected";
|
|
212 |
RawSendNet(QString("CONNECTED"));
|
|
213 |
m_hwserver->teamChanged();
|
326
|
214 |
if(m_hwserver->isChiefClient(this)) RawSendNet(QString("CONFIGASKED"));
|
334
|
215 |
else RawSendNet(QString("CONFIGURED")+delimeter+m_hwserver->getGameCfg().join(QString(delimeter)));
|
315
|
216 |
return;
|
|
217 |
}
|
|
218 |
if(client_nick=="") return;
|
|
219 |
|
|
220 |
if (lst[0]=="START:") {
|
|
221 |
readyToStart=true;
|
|
222 |
if(m_hwserver->shouldStart(this)) {
|
|
223 |
// start
|
|
224 |
}
|
|
225 |
return;
|
|
226 |
}
|
|
227 |
|
|
228 |
if(lst[0]=="CONFIGANSWER") {
|
|
229 |
lst.pop_front();
|
317
|
230 |
gameCfg=lst;
|
315
|
231 |
return;
|
|
232 |
}
|
|
233 |
|
|
234 |
if(lst[0]=="ADDTEAM:") {
|
319
|
235 |
if(lst.size()<10) return;
|
315
|
236 |
lst.pop_front();
|
|
237 |
if(pclent_team) delete pclent_team;
|
|
238 |
pclent_team=new HWTeam(lst);
|
|
239 |
m_hwserver->teamChanged();
|
|
240 |
return;
|
|
241 |
}
|
326
|
242 |
|
319
|
243 |
m_hwserver->sendOthers(this, msg);
|
315
|
244 |
}
|
|
245 |
|
|
246 |
void HWConnectedClient::teamChangedNotify()
|
|
247 |
{
|
|
248 |
QString teams;
|
|
249 |
QStringList lst=m_hwserver->getTeams();
|
|
250 |
for(int i=0; i<lst.size(); i++) {
|
|
251 |
teams+=delimeter+lst[i];
|
|
252 |
}
|
|
253 |
RawSendNet(QString("TEAMCHANGED")+teams);
|
|
254 |
}
|
|
255 |
|
|
256 |
void HWConnectedClient::RawSendNet(const QString & str)
|
|
257 |
{
|
|
258 |
RawSendNet(str.toUtf8());
|
|
259 |
}
|
|
260 |
|
|
261 |
void HWConnectedClient::RawSendNet(const QByteArray & buf)
|
|
262 |
{
|
|
263 |
m_client->write(buf);
|
|
264 |
m_client->write("\n", 1);
|
|
265 |
}
|
|
266 |
|
|
267 |
QString HWConnectedClient::getClientNick() const
|
|
268 |
{
|
|
269 |
return client_nick;
|
|
270 |
}
|
|
271 |
|
|
272 |
QString HWConnectedClient::getTeamName() const
|
|
273 |
{
|
|
274 |
if(!pclent_team) throw NoTeamNameException();
|
|
275 |
return pclent_team->TeamName;
|
|
276 |
}
|
|
277 |
|
|
278 |
bool HWConnectedClient::isReady() const
|
|
279 |
{
|
|
280 |
return readyToStart;
|
|
281 |
}
|
|
282 |
|
|
283 |
QString HWConnectedClient::getHedgehogsDescription() const
|
|
284 |
{
|
319
|
285 |
return pclent_team->TeamGameConfig(65535, 4, 100, true).join((QString)delimeter);
|
315
|
286 |
}
|