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