author | displacer |
Wed, 19 Dec 2007 11:10:23 +0000 | |
changeset 681 | 7a20c50988ec |
parent 672 | 08ed55ea1e2a |
child 749 | 3acf2371d73c |
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 |
||
672
08ed55ea1e2a
- Fix a bug when server fails to start, and then connecting to another server on the same host (strange, the fix won't work on my machine)
unc0rr
parents:
658
diff
changeset
|
28 |
bool HWNetServer::StartServer(quint16 port) |
315 | 29 |
{ |
658 | 30 |
ds_port = port; |
399 | 31 |
hhnum=0; |
315 | 32 |
IPCServer = new QTcpServer(this); |
326 | 33 |
|
315 | 34 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
672
08ed55ea1e2a
- Fix a bug when server fails to start, and then connecting to another server on the same host (strange, the fix won't work on my machine)
unc0rr
parents:
658
diff
changeset
|
35 |
|
08ed55ea1e2a
- Fix a bug when server fails to start, and then connecting to another server on the same host (strange, the fix won't work on my machine)
unc0rr
parents:
658
diff
changeset
|
36 |
return IPCServer->listen(QHostAddress::Any, ds_port); |
315 | 37 |
} |
38 |
||
39 |
void HWNetServer::StopServer() |
|
40 |
{ |
|
383 | 41 |
QList<HWConnectedClient*>::iterator it; |
42 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
43 |
ClientDisconnect(*it); |
|
44 |
} |
|
315 | 45 |
IPCServer->close(); |
46 |
} |
|
47 |
||
48 |
void HWNetServer::NewConnection() |
|
49 |
{ |
|
50 |
QTcpSocket* client = IPCServer->nextPendingConnection(); |
|
51 |
if(!client) return; |
|
52 |
connclients.push_back(new HWConnectedClient(this, client)); |
|
326 | 53 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), |
315 | 54 |
this, SLOT(ClientDisconnect(HWConnectedClient*))); |
55 |
} |
|
56 |
||
57 |
void HWNetServer::ClientDisconnect(HWConnectedClient* client) |
|
58 |
{ |
|
59 |
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
|
60 |
if(it==connclients.end()) |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
61 |
{ |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
62 |
qWarning("Unknown client disconnected"); |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
63 |
return; |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
64 |
} |
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 |
} |