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