author | unc0rr |
Wed, 14 Feb 2007 20:05:20 +0000 | |
changeset 442 | 57ed1444606e |
parent 420 | 6cdfc07dceed |
child 443 | eec37eb7f5db |
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" |
|
420
6cdfc07dceed
netserver and netonnectedclient splited to different files
displacer
parents:
404
diff
changeset
|
20 |
#include "netconnectedclient.h" |
315 | 21 |
|
22 |
#include <QTcpServer> |
|
23 |
#include <QTcpSocket> |
|
24 |
#include <QMessageBox> |
|
25 |
||
26 |
#include <algorithm> |
|
27 |
||
28 |
#include <QDebug> |
|
29 |
||
30 |
const quint16 HWNetServer::ds_port=46631; |
|
31 |
||
32 |
extern char delimeter; |
|
33 |
||
34 |
void HWNetServer::StartServer() |
|
35 |
{ |
|
399 | 36 |
hhnum=0; |
315 | 37 |
IPCServer = new QTcpServer(this); |
347 | 38 |
if (!IPCServer->listen(QHostAddress::Any, ds_port)) { |
315 | 39 |
QMessageBox::critical(0, tr("Error"), |
40 |
tr("Unable to start the server: %1.") |
|
41 |
.arg(IPCServer->errorString())); |
|
42 |
} |
|
326 | 43 |
|
315 | 44 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
45 |
} |
|
46 |
||
47 |
void HWNetServer::StopServer() |
|
48 |
{ |
|
383 | 49 |
QList<HWConnectedClient*>::iterator it; |
50 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
51 |
ClientDisconnect(*it); |
|
52 |
} |
|
315 | 53 |
IPCServer->close(); |
54 |
} |
|
55 |
||
56 |
void HWNetServer::NewConnection() |
|
57 |
{ |
|
58 |
QTcpSocket* client = IPCServer->nextPendingConnection(); |
|
59 |
if(!client) return; |
|
60 |
connclients.push_back(new HWConnectedClient(this, client)); |
|
326 | 61 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), |
315 | 62 |
this, SLOT(ClientDisconnect(HWConnectedClient*))); |
63 |
} |
|
64 |
||
65 |
void HWNetServer::ClientDisconnect(HWConnectedClient* client) |
|
66 |
{ |
|
67 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
383 | 68 |
if(it==connclients.end()) return; |
350 | 69 |
for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) { |
383 | 70 |
sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1)); |
350 | 71 |
} |
315 | 72 |
connclients.erase(it); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
73 |
//teamChanged(); |
315 | 74 |
} |
75 |
||
76 |
QString HWNetServer::getRunningHostName() const |
|
77 |
{ |
|
78 |
return IPCServer->serverAddress().toString(); |
|
79 |
} |
|
80 |
||
81 |
quint16 HWNetServer::getRunningPort() const |
|
82 |
{ |
|
83 |
return ds_port; |
|
84 |
} |
|
85 |
||
334 | 86 |
HWConnectedClient* HWNetServer::getChiefClient() const |
317 | 87 |
{ |
88 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
334 | 89 |
// watch for first fully connected client (with confirmed nick) |
90 |
if((*it)->getClientNick()!="") return *it; |
|
317 | 91 |
} |
334 | 92 |
return 0; |
93 |
} |
|
94 |
||
95 |
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const |
|
96 |
{ |
|
97 |
return getChiefClient()==cl; |
|
98 |
} |
|
99 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
100 |
QMap<QString, QStringList> HWNetServer::getGameCfg() const |
334 | 101 |
{ |
404 | 102 |
return m_gameCfg; |
103 |
/* |
|
334 | 104 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
335 | 105 |
if(isChiefClient(*it)) { |
106 |
return (*it)->m_gameCfg; |
|
107 |
} |
|
334 | 108 |
} |
109 |
// error happened if we are here |
|
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
110 |
return QMap<QString, QStringList>(); |
404 | 111 |
*/ |
317 | 112 |
} |
113 |
||
315 | 114 |
bool HWNetServer::haveNick(const QString& nick) const |
115 |
{ |
|
116 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
117 |
if((*it)->getClientNick()==nick) { |
|
118 |
return true; |
|
119 |
} |
|
120 |
} |
|
121 |
return false; |
|
122 |
} |
|
123 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
124 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 125 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
126 |
QList<QStringList> lst; |
315 | 127 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
128 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
129 |
lst+=(*it)->getTeamNames(); |
315 | 130 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
131 |
} |
|
132 |
} |
|
133 |
return lst; |
|
134 |
} |
|
135 |
||
136 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
137 |
{ |
|
138 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
139 |
if(it==connclients.end() || *it!=client) return false; |
|
140 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
141 |
if(!(*it)->isReady()) return false; |
|
142 |
} |
|
143 |
return true; |
|
144 |
} |
|
145 |
||
388 | 146 |
void HWNetServer::resetStart() |
147 |
{ |
|
148 |
QList<HWConnectedClient*>::iterator it; |
|
149 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
150 |
(*it)->readyToStart=false; |
|
151 |
} |
|
152 |
} |
|
153 |
||
315 | 154 |
QString HWNetServer::prepareConfig(QStringList lst) |
155 |
{ |
|
156 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
157 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
158 |
if(!(*it)->isReady()) continue; |
|
159 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
160 |
} |
|
161 |
qDebug() << msg; |
|
162 |
return msg; |
|
163 |
} |
|
164 |
||
319 | 165 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 166 |
{ |
167 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
168 |
(*it)->RawSendNet(gameCfg); |
|
169 |
} |
|
170 |
} |
|
171 |
||
319 | 172 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
173 |
{ |
|
174 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
175 |
if(*it==this_cl) continue; |
|
176 |
(*it)->RawSendNet(gameCfg); |
|
177 |
} |
|
178 |
} |