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