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 |
}
|
|
41 |
|
|
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));
|
|
55 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)),
|
|
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 |
|
317
|
76 |
bool HWNetServer::isCheefClient(HWConnectedClient* cl) const
|
|
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 |
|
317
|
134 |
void HWNetServer::startAll(QString gameCfg)
|
|
135 |
{
|
|
136 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) {
|
|
137 |
(*it)->RawSendNet(gameCfg);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
315
|
141 |
HWConnectedClient::HWConnectedClient(HWNetServer* hwserver, QTcpSocket* client) :
|
|
142 |
readyToStart(false),
|
|
143 |
m_hwserver(hwserver),
|
|
144 |
m_client(client),
|
|
145 |
pclent_team(0)
|
|
146 |
{
|
|
147 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
148 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
149 |
}
|
|
150 |
|
|
151 |
HWConnectedClient::~HWConnectedClient()
|
|
152 |
{
|
|
153 |
if(pclent_team) delete pclent_team;
|
|
154 |
}
|
|
155 |
|
|
156 |
void HWConnectedClient::ClientDisconnect()
|
|
157 |
{
|
|
158 |
emit(HWClientDisconnected(this));
|
|
159 |
}
|
|
160 |
|
|
161 |
void HWConnectedClient::ClientRead()
|
|
162 |
{
|
|
163 |
try {
|
|
164 |
while (m_client->canReadLine()) {
|
|
165 |
ParseLine(m_client->readLine().trimmed());
|
|
166 |
}
|
|
167 |
} catch(ShouldDisconnectException& e) {
|
|
168 |
m_client->close();
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
void HWConnectedClient::ParseLine(const QByteArray & line)
|
|
173 |
{
|
|
174 |
QString msg = QString::fromUtf8 (line.data(), line.size());
|
|
175 |
|
|
176 |
qDebug() << "line " << msg << " received";
|
|
177 |
|
|
178 |
QStringList lst = msg.split(delimeter);
|
|
179 |
if(lst.size()<2) return;
|
|
180 |
if (lst[0] == "NICK") {
|
|
181 |
if(m_hwserver->haveNick(lst[1])) {
|
|
182 |
RawSendNet(QString("ERRONEUSNICKNAME"));
|
|
183 |
throw ShouldDisconnectException();
|
|
184 |
}
|
|
185 |
|
|
186 |
client_nick=lst[1];
|
|
187 |
qDebug() << "send connected";
|
|
188 |
RawSendNet(QString("CONNECTED"));
|
|
189 |
m_hwserver->teamChanged();
|
317
|
190 |
if(m_hwserver->isCheefClient(this)) RawSendNet(QString("CONFIGASKED"));
|
315
|
191 |
return;
|
|
192 |
}
|
|
193 |
if(client_nick=="") return;
|
|
194 |
|
|
195 |
if (lst[0]=="START:") {
|
|
196 |
readyToStart=true;
|
|
197 |
if(m_hwserver->shouldStart(this)) {
|
|
198 |
// start
|
317
|
199 |
m_hwserver->startAll(QString("CONFIGURED")+delimeter+m_hwserver->prepareConfig(gameCfg)+delimeter+"!"+delimeter);
|
315
|
200 |
}
|
|
201 |
return;
|
|
202 |
}
|
|
203 |
|
|
204 |
if(lst[0]=="CONFIGANSWER") {
|
|
205 |
lst.pop_front();
|
317
|
206 |
gameCfg=lst;
|
315
|
207 |
return;
|
|
208 |
}
|
|
209 |
|
|
210 |
if(lst.size()<10) return;
|
|
211 |
if(lst[0]=="ADDTEAM:") {
|
|
212 |
lst.pop_front();
|
|
213 |
if(pclent_team) delete pclent_team;
|
|
214 |
pclent_team=new HWTeam(lst);
|
|
215 |
m_hwserver->teamChanged();
|
|
216 |
return;
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
void HWConnectedClient::teamChangedNotify()
|
|
221 |
{
|
|
222 |
QString teams;
|
|
223 |
QStringList lst=m_hwserver->getTeams();
|
|
224 |
for(int i=0; i<lst.size(); i++) {
|
|
225 |
teams+=delimeter+lst[i];
|
|
226 |
}
|
|
227 |
RawSendNet(QString("TEAMCHANGED")+teams);
|
|
228 |
}
|
|
229 |
|
|
230 |
void HWConnectedClient::RawSendNet(const QString & str)
|
|
231 |
{
|
|
232 |
RawSendNet(str.toUtf8());
|
|
233 |
}
|
|
234 |
|
|
235 |
void HWConnectedClient::RawSendNet(const QByteArray & buf)
|
|
236 |
{
|
|
237 |
m_client->write(buf);
|
|
238 |
m_client->write("\n", 1);
|
|
239 |
}
|
|
240 |
|
|
241 |
QString HWConnectedClient::getClientNick() const
|
|
242 |
{
|
|
243 |
return client_nick;
|
|
244 |
}
|
|
245 |
|
|
246 |
QString HWConnectedClient::getTeamName() const
|
|
247 |
{
|
|
248 |
if(!pclent_team) throw NoTeamNameException();
|
|
249 |
return pclent_team->TeamName;
|
|
250 |
}
|
|
251 |
|
|
252 |
bool HWConnectedClient::isReady() const
|
|
253 |
{
|
|
254 |
return readyToStart;
|
|
255 |
}
|
|
256 |
|
|
257 |
QString HWConnectedClient::getHedgehogsDescription() const
|
|
258 |
{
|
|
259 |
return pclent_team->TeamGameConfig(65535, 4, 100).join((QString)delimeter);
|
|
260 |
}
|