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