author | unc0rr |
Fri, 14 Dec 2007 16:13:35 +0000 | |
changeset 660 | 7c155e849602 |
parent 658 | a7e625f5d9d0 |
child 672 | 08ed55ea1e2a |
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 |
extern char delimeter; |
|
27 |
||
658 | 28 |
void HWNetServer::StartServer(quint16 port) |
315 | 29 |
{ |
658 | 30 |
ds_port = port; |
399 | 31 |
hhnum=0; |
315 | 32 |
IPCServer = new QTcpServer(this); |
347 | 33 |
if (!IPCServer->listen(QHostAddress::Any, ds_port)) { |
315 | 34 |
QMessageBox::critical(0, tr("Error"), |
35 |
tr("Unable to start the server: %1.") |
|
36 |
.arg(IPCServer->errorString())); |
|
37 |
} |
|
326 | 38 |
|
315 | 39 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
40 |
} |
|
41 |
||
42 |
void HWNetServer::StopServer() |
|
43 |
{ |
|
383 | 44 |
QList<HWConnectedClient*>::iterator it; |
45 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
46 |
ClientDisconnect(*it); |
|
47 |
} |
|
315 | 48 |
IPCServer->close(); |
49 |
} |
|
50 |
||
51 |
void HWNetServer::NewConnection() |
|
52 |
{ |
|
53 |
QTcpSocket* client = IPCServer->nextPendingConnection(); |
|
54 |
if(!client) return; |
|
55 |
connclients.push_back(new HWConnectedClient(this, client)); |
|
326 | 56 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), |
315 | 57 |
this, SLOT(ClientDisconnect(HWConnectedClient*))); |
58 |
} |
|
59 |
||
60 |
void HWNetServer::ClientDisconnect(HWConnectedClient* client) |
|
61 |
{ |
|
62 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
569
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
63 |
if(it==connclients.end()) |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
64 |
{ |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
65 |
qWarning("Unknown client disconnected"); |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
66 |
return; |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
67 |
} |
350 | 68 |
for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) { |
383 | 69 |
sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1)); |
350 | 70 |
} |
461 | 71 |
sendOthers(*it, QString("LEFT")+delimeter+client->client_nick); |
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; |
317 | 103 |
} |
104 |
||
315 | 105 |
bool HWNetServer::haveNick(const QString& nick) const |
106 |
{ |
|
107 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
108 |
if((*it)->getClientNick()==nick) { |
|
109 |
return true; |
|
110 |
} |
|
111 |
} |
|
112 |
return false; |
|
113 |
} |
|
114 |
||
465 | 115 |
void HWNetServer::sendNicks(HWConnectedClient* cl) const |
116 |
{ |
|
117 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
118 |
cl->RawSendNet(QString("JOINED")+delimeter+(*it)->client_nick); |
|
119 |
} |
|
120 |
} |
|
121 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
122 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 123 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
124 |
QList<QStringList> lst; |
315 | 125 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
126 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
127 |
lst+=(*it)->getTeamNames(); |
315 | 128 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
129 |
} |
|
130 |
} |
|
131 |
return lst; |
|
132 |
} |
|
133 |
||
134 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
135 |
{ |
|
136 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
137 |
if(it==connclients.end() || *it!=client) return false; |
|
138 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
139 |
if(!(*it)->isReady()) return false; |
|
140 |
} |
|
141 |
return true; |
|
142 |
} |
|
143 |
||
388 | 144 |
void HWNetServer::resetStart() |
145 |
{ |
|
146 |
QList<HWConnectedClient*>::iterator it; |
|
147 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
148 |
(*it)->readyToStart=false; |
|
149 |
} |
|
150 |
} |
|
151 |
||
315 | 152 |
QString HWNetServer::prepareConfig(QStringList lst) |
153 |
{ |
|
154 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
155 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
156 |
if(!(*it)->isReady()) continue; |
|
157 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
158 |
} |
|
159 |
return msg; |
|
160 |
} |
|
161 |
||
319 | 162 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 163 |
{ |
164 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
165 |
(*it)->RawSendNet(gameCfg); |
|
166 |
} |
|
167 |
} |
|
168 |
||
319 | 169 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
170 |
{ |
|
171 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
172 |
if(*it==this_cl) continue; |
|
173 |
(*it)->RawSendNet(gameCfg); |
|
174 |
} |
|
175 |
} |