author | unc0rr |
Thu, 13 Dec 2007 14:15:56 +0000 | |
changeset 649 | 26166c87dc75 |
parent 569 | 4e41c9e9e4d1 |
child 658 | a7e625f5d9d0 |
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); |
|
569
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
64 |
if(it==connclients.end()) |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
65 |
{ |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
66 |
qWarning("Unknown client disconnected"); |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
67 |
return; |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
68 |
} |
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 |
} |
461 | 72 |
sendOthers(*it, QString("LEFT")+delimeter+client->client_nick); |
315 | 73 |
connclients.erase(it); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
74 |
//teamChanged(); |
315 | 75 |
} |
76 |
||
77 |
QString HWNetServer::getRunningHostName() const |
|
78 |
{ |
|
79 |
return IPCServer->serverAddress().toString(); |
|
80 |
} |
|
81 |
||
82 |
quint16 HWNetServer::getRunningPort() const |
|
83 |
{ |
|
84 |
return ds_port; |
|
85 |
} |
|
86 |
||
334 | 87 |
HWConnectedClient* HWNetServer::getChiefClient() const |
317 | 88 |
{ |
89 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
334 | 90 |
// watch for first fully connected client (with confirmed nick) |
91 |
if((*it)->getClientNick()!="") return *it; |
|
317 | 92 |
} |
334 | 93 |
return 0; |
94 |
} |
|
95 |
||
96 |
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const |
|
97 |
{ |
|
98 |
return getChiefClient()==cl; |
|
99 |
} |
|
100 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
101 |
QMap<QString, QStringList> HWNetServer::getGameCfg() const |
334 | 102 |
{ |
404 | 103 |
return m_gameCfg; |
317 | 104 |
} |
105 |
||
315 | 106 |
bool HWNetServer::haveNick(const QString& nick) const |
107 |
{ |
|
108 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
109 |
if((*it)->getClientNick()==nick) { |
|
110 |
return true; |
|
111 |
} |
|
112 |
} |
|
113 |
return false; |
|
114 |
} |
|
115 |
||
465 | 116 |
void HWNetServer::sendNicks(HWConnectedClient* cl) const |
117 |
{ |
|
118 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
119 |
cl->RawSendNet(QString("JOINED")+delimeter+(*it)->client_nick); |
|
120 |
} |
|
121 |
} |
|
122 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
123 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 124 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
125 |
QList<QStringList> lst; |
315 | 126 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
127 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
128 |
lst+=(*it)->getTeamNames(); |
315 | 129 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
130 |
} |
|
131 |
} |
|
132 |
return lst; |
|
133 |
} |
|
134 |
||
135 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
136 |
{ |
|
137 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
138 |
if(it==connclients.end() || *it!=client) return false; |
|
139 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
140 |
if(!(*it)->isReady()) return false; |
|
141 |
} |
|
142 |
return true; |
|
143 |
} |
|
144 |
||
388 | 145 |
void HWNetServer::resetStart() |
146 |
{ |
|
147 |
QList<HWConnectedClient*>::iterator it; |
|
148 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
149 |
(*it)->readyToStart=false; |
|
150 |
} |
|
151 |
} |
|
152 |
||
315 | 153 |
QString HWNetServer::prepareConfig(QStringList lst) |
154 |
{ |
|
155 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
156 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
157 |
if(!(*it)->isReady()) continue; |
|
158 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
159 |
} |
|
160 |
return msg; |
|
161 |
} |
|
162 |
||
319 | 163 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 164 |
{ |
165 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
166 |
(*it)->RawSendNet(gameCfg); |
|
167 |
} |
|
168 |
} |
|
169 |
||
319 | 170 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
171 |
{ |
|
172 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
173 |
if(*it==this_cl) continue; |
|
174 |
(*it)->RawSendNet(gameCfg); |
|
175 |
} |
|
176 |
} |