author | unc0rr |
Fri, 07 Mar 2008 19:44:00 +0000 | |
changeset 805 | 4d75759b38bd |
parent 749 | 3acf2371d73c |
child 883 | 07a568ba44e0 |
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 |
||
749 | 26 |
#include <algorithm> |
27 |
||
315 | 28 |
extern char delimeter; |
29 |
||
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
|
30 |
bool HWNetServer::StartServer(quint16 port) |
315 | 31 |
{ |
658 | 32 |
ds_port = port; |
399 | 33 |
hhnum=0; |
315 | 34 |
IPCServer = new QTcpServer(this); |
326 | 35 |
|
315 | 36 |
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
|
37 |
|
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
|
38 |
return IPCServer->listen(QHostAddress::Any, ds_port); |
315 | 39 |
} |
40 |
||
41 |
void HWNetServer::StopServer() |
|
42 |
{ |
|
383 | 43 |
QList<HWConnectedClient*>::iterator it; |
44 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
45 |
ClientDisconnect(*it); |
|
46 |
} |
|
315 | 47 |
IPCServer->close(); |
48 |
} |
|
49 |
||
50 |
void HWNetServer::NewConnection() |
|
51 |
{ |
|
52 |
QTcpSocket* client = IPCServer->nextPendingConnection(); |
|
53 |
if(!client) return; |
|
54 |
connclients.push_back(new HWConnectedClient(this, client)); |
|
326 | 55 |
connect(connclients.back(), SIGNAL(HWClientDisconnected(HWConnectedClient*)), |
315 | 56 |
this, SLOT(ClientDisconnect(HWConnectedClient*))); |
57 |
} |
|
58 |
||
59 |
void HWNetServer::ClientDisconnect(HWConnectedClient* client) |
|
60 |
{ |
|
61 |
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
|
62 |
if(it==connclients.end()) |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
63 |
{ |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
64 |
qWarning("Unknown client disconnected"); |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
65 |
return; |
4e41c9e9e4d1
Highlight some bad behavior of network server/clients
unc0rr
parents:
541
diff
changeset
|
66 |
} |
350 | 67 |
for(QList<QStringList>::iterator tmIt=(*it)->m_teamsCfg.begin(); tmIt!=(*it)->m_teamsCfg.end(); ++tmIt) { |
383 | 68 |
sendOthers(*it, QString("REMOVETEAM:")+delimeter+*(tmIt->begin()) + delimeter + *(tmIt->begin()+1)); |
350 | 69 |
} |
461 | 70 |
sendOthers(*it, QString("LEFT")+delimeter+client->client_nick); |
315 | 71 |
connclients.erase(it); |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
72 |
//teamChanged(); |
315 | 73 |
} |
74 |
||
75 |
QString HWNetServer::getRunningHostName() const |
|
76 |
{ |
|
77 |
return IPCServer->serverAddress().toString(); |
|
78 |
} |
|
79 |
||
80 |
quint16 HWNetServer::getRunningPort() const |
|
81 |
{ |
|
82 |
return ds_port; |
|
83 |
} |
|
84 |
||
334 | 85 |
HWConnectedClient* HWNetServer::getChiefClient() const |
317 | 86 |
{ |
87 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
334 | 88 |
// watch for first fully connected client (with confirmed nick) |
89 |
if((*it)->getClientNick()!="") return *it; |
|
317 | 90 |
} |
334 | 91 |
return 0; |
92 |
} |
|
93 |
||
94 |
bool HWNetServer::isChiefClient(HWConnectedClient* cl) const |
|
95 |
{ |
|
96 |
return getChiefClient()==cl; |
|
97 |
} |
|
98 |
||
354
60e4af0a4375
network to local teams map, addteams from server before team config bug, fixed some segfaults
displacer
parents:
352
diff
changeset
|
99 |
QMap<QString, QStringList> HWNetServer::getGameCfg() const |
334 | 100 |
{ |
404 | 101 |
return m_gameCfg; |
317 | 102 |
} |
103 |
||
315 | 104 |
bool HWNetServer::haveNick(const QString& nick) const |
105 |
{ |
|
106 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
107 |
if((*it)->getClientNick()==nick) { |
|
108 |
return true; |
|
109 |
} |
|
110 |
} |
|
111 |
return false; |
|
112 |
} |
|
113 |
||
465 | 114 |
void HWNetServer::sendNicks(HWConnectedClient* cl) const |
115 |
{ |
|
116 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
117 |
cl->RawSendNet(QString("JOINED")+delimeter+(*it)->client_nick); |
|
118 |
} |
|
119 |
} |
|
120 |
||
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
121 |
QList<QStringList> HWNetServer::getTeamsConfig() const |
315 | 122 |
{ |
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
123 |
QList<QStringList> lst; |
315 | 124 |
for(QList<HWConnectedClient*>::const_iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
125 |
try { |
|
338
d1e75dcd285f
multiple teams now available per host (still alpha)
displacer
parents:
335
diff
changeset
|
126 |
lst+=(*it)->getTeamNames(); |
315 | 127 |
} catch(HWConnectedClient::NoTeamNameException& e) { |
128 |
} |
|
129 |
} |
|
130 |
return lst; |
|
131 |
} |
|
132 |
||
133 |
bool HWNetServer::shouldStart(HWConnectedClient* client) |
|
134 |
{ |
|
135 |
QList<HWConnectedClient*>::iterator it=std::find(connclients.begin(), connclients.end(), client); |
|
136 |
if(it==connclients.end() || *it!=client) return false; |
|
137 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
138 |
if(!(*it)->isReady()) return false; |
|
139 |
} |
|
140 |
return true; |
|
141 |
} |
|
142 |
||
388 | 143 |
void HWNetServer::resetStart() |
144 |
{ |
|
145 |
QList<HWConnectedClient*>::iterator it; |
|
146 |
for(it=connclients.begin(); it!=connclients.end(); ++it) { |
|
147 |
(*it)->readyToStart=false; |
|
148 |
} |
|
149 |
} |
|
150 |
||
315 | 151 |
QString HWNetServer::prepareConfig(QStringList lst) |
152 |
{ |
|
153 |
QString msg=lst.join((QString)delimeter)+delimeter; |
|
154 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
155 |
if(!(*it)->isReady()) continue; |
|
156 |
msg+=(*it)->getHedgehogsDescription()+delimeter; |
|
157 |
} |
|
158 |
return msg; |
|
159 |
} |
|
160 |
||
319 | 161 |
void HWNetServer::sendAll(QString gameCfg) |
317 | 162 |
{ |
163 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
164 |
(*it)->RawSendNet(gameCfg); |
|
165 |
} |
|
166 |
} |
|
167 |
||
319 | 168 |
void HWNetServer::sendOthers(HWConnectedClient* this_cl, QString gameCfg) |
169 |
{ |
|
170 |
for(QList<HWConnectedClient*>::iterator it=connclients.begin(); it!=connclients.end(); ++it) { |
|
171 |
if(*it==this_cl) continue; |
|
172 |
(*it)->RawSendNet(gameCfg); |
|
173 |
} |
|
174 |
} |