author | displacer |
Sun, 18 Feb 2007 12:20:36 +0000 | |
changeset 448 | e968e19a464c |
parent 443 | eec37eb7f5db |
child 461 | 105af110b0e6 |
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; |
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 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
115 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 116 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
117 |
QList<QStringList> lst; |
315 | 118 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
119 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
120 |
lst+=(*it)->getTeamNames(); |
315 | 121 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
122 |
} |
|
123 |
} |
|
124 |
return lst; |
|
125 |
} |
|
126 |
||
127 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
128 |
{ |
|
129 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
130 |
if(it==connclients.end() || *it!=client) return false; |
|
131 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
132 |
if(!(*it)->isReady()) return false; |
|
133 |
} |
|
134 |
return true; |
|
135 |
} |
|
136 |
||
388 | 137 |
void HWNetServer::resetStart() |
138 |
{ |
|
139 |
QList<HWConnectedClient*>::iterator it; |
|
140 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
141 |
(*it)->readyToStart=false; |
|
142 |
} |
|
143 |
} |
|
144 |
||
315 | 145 |
QString HWNetServer::prepareConfig(QStringList lst) |
146 |
{ |
|
147 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
148 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
149 |
if(!(*it)->isReady()) continue; |
|
150 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
151 |
} |
|
152 |
qDebug() << msg; |
|
153 |
return msg; |
|
154 |
} |
|
155 |
||
319 | 156 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 157 |
{ |
158 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
159 |
(*it)->RawSendNet(gameCfg); |
|
160 |
} |
|
161 |
} |
|
162 |
||
319 | 163 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
164 |
{ |
|
165 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
166 |
if(*it==this_cl) continue; |
|
167 |
(*it)->RawSendNet(gameCfg); |
|
168 |
} |
|
169 |
} |