Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 416 lines (373 sloc) 16.091 kb
62570b0 @vos added multiplayer core implementation
authored
1 #include "multiplayerserver.h"
2
3 #include <QTcpSocket>
303669c @vos simple network protocoll
authored
4 #include "game.h"
f968e85 @vos MP: resourcesTransferInitiated event
authored
5 #include "ship.h"
62570b0 @vos added multiplayer core implementation
authored
6
303669c @vos simple network protocoll
authored
7 MultiplayerServer::MultiplayerServer(Game *game, QObject *parent) :
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
8 QObject(parent),
1785d63 @vos MP player handling and "dedicated server"
authored
9 m_game(game),
8f0824c @vos MP added planet (still buggy)
authored
10 m_nextPlayerId(1),
11 m_nextPlanetId(1)
62570b0 @vos added multiplayer core implementation
authored
12 {
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
13 connect(&m_tcpServer, SIGNAL(newConnection()), SLOT(client_newConnection()));
14 connect(&m_udpSocket, SIGNAL(readyRead()), SLOT(client_udpReadyRead()));
62570b0 @vos added multiplayer core implementation
authored
15 }
16
303669c @vos simple network protocoll
authored
17 MultiplayerServer::~MultiplayerServer()
18 {
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
19 foreach (QTcpSocket *socket, m_tcpClientMap.keys()) {
303669c @vos simple network protocoll
authored
20 socket->disconnectFromHost();
21 }
68fd991 @vos UDP connection from server to clients should work now
authored
22 qDeleteAll(m_tcpClientMap.values());
303669c @vos simple network protocoll
authored
23 }
24
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
25 bool MultiplayerServer::listen(const QHostAddress &address, quint16 port)
62570b0 @vos added multiplayer core implementation
authored
26 {
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
27 return m_tcpServer.listen(address, port) && m_udpSocket.bind(address, port);
28 }
29
30 void MultiplayerServer::close()
31 {
32 m_tcpServer.close();
33 m_udpSocket.close();
34 }
35
36 QAbstractSocket::SocketError MultiplayerServer::serverError() const
37 {
38 return m_tcpServer.serverError() != QAbstractSocket::UnknownSocketError ? m_tcpServer.serverError() : m_udpSocket.error();
39 }
40
41 QString MultiplayerServer::errorString() const
42 {
43 return m_tcpServer.serverError() != QAbstractSocket::UnknownSocketError ? m_tcpServer.errorString() : m_udpSocket.errorString();
44 }
45
46 void MultiplayerServer::client_newConnection()
47 {
48 QTcpSocket *socket = m_tcpServer.nextPendingConnection();
49 Q_ASSERT(socket);
62570b0 @vos added multiplayer core implementation
authored
50
51 #ifdef MULTIPLAYERSERVER_DEBUG
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
52 qDebug("MultiplayerServer::client_newConnection(): %s:%i", qPrintable(socket->peerAddress().toString()), socket->peerPort());
62570b0 @vos added multiplayer core implementation
authored
53 #endif
54
55 connect(socket, SIGNAL(disconnected()), SLOT(client_disconnected()));
56 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(client_error(QAbstractSocket::SocketError)));
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
57 connect(socket, SIGNAL(readyRead()), SLOT(client_tcpReadyRead()));
62570b0 @vos added multiplayer core implementation
authored
58
59 Client *client = new Client;
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
60 m_tcpClientMap.insert(socket, client);
62570b0 @vos added multiplayer core implementation
authored
61
62 MultiplayerPacket(MultiplayerPacket::ConnectionAccepted).send(socket); // Multiplayer::ConnectionRefused
63
64 #ifdef MULTIPLAYERSERVER_DEBUG
65 qDebug("ConnectionAccepted");
66 #endif
67 }
68
69 void MultiplayerServer::client_disconnected()
70 {
71 QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
72 Q_ASSERT(socket);
73
68fd991 @vos UDP connection from server to clients should work now
authored
74 Client *client = m_tcpClientMap.take(socket);
8f0824c @vos MP added planet (still buggy)
authored
75 Q_ASSERT(client);
303669c @vos simple network protocoll
authored
76 Player *player = client->player;
77
62570b0 @vos added multiplayer core implementation
authored
78 #ifdef MULTIPLAYERSERVER_DEBUG
214c4b5 @vos debug output fixed
authored
79 qDebug("MultiplayerServer::client_disconnected(): %s (%s:%i)",
80 qPrintable(player ? QString("'%1' (%2)").arg(player->name()).arg(client->id) : "[no player object]"),
62570b0 @vos added multiplayer core implementation
authored
81 qPrintable(socket->peerAddress().toString()), socket->peerPort());
82 #endif
83
1785d63 @vos MP player handling and "dedicated server"
authored
84 if (client->isConnected()) {
303669c @vos simple network protocoll
authored
85 m_game->removePlayer(player);
68fd991 @vos UDP connection from server to clients should work now
authored
86 m_udpClientMap.removeKey(client->id);
0e9b8fd @vos BiMap used and some multiplayer optimizations
authored
87 m_idPlayerMap.removeKey(client->id);
1785d63 @vos MP player handling and "dedicated server"
authored
88
89 MultiplayerPacket playerDisconnectedPacket(MultiplayerPacket::PlayerDisconnected);
a430d2a @vos added MultiplayerPacket << stream operator
authored
90 playerDisconnectedPacket << client->id;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
91 playerDisconnectedPacket.pack();
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
92 sendTcpPacketToOtherClients(playerDisconnectedPacket, socket);
303669c @vos simple network protocoll
authored
93 }
62570b0 @vos added multiplayer core implementation
authored
94 delete client;
95 }
96
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
97 void MultiplayerServer::client_error(QAbstractSocket::SocketError error)
98 {
99 QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
100 Q_ASSERT(socket);
101
102 #ifdef MULTIPLAYERSERVER_DEBUG
103 qDebug("MultiplayerServer::client_error(%i) => %s", error, qPrintable(socket->errorString()));
104 #endif
105 }
106
107 void MultiplayerServer::client_tcpReadyRead()
62570b0 @vos added multiplayer core implementation
authored
108 {
109 QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
110 Q_ASSERT(socket);
111
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
112 Client *client = m_tcpClientMap.value(socket);
8f0824c @vos MP added planet (still buggy)
authored
113 Q_ASSERT(client);
114
62570b0 @vos added multiplayer core implementation
authored
115 #ifdef MULTIPLAYERSERVER_DEBUG
f968e85 @vos MP: resourcesTransferInitiated event
authored
116 qDebug("MultiplayerServer::client_tcpReadyRead(): %li bytes available", (long)socket->bytesAvailable());
62570b0 @vos added multiplayer core implementation
authored
117 #endif
118
119 QDataStream in(socket);
d3a588b @vos chat messages, some bug fixes and optimizations
authored
120 in.setVersion(MultiplayerPacket::StreamVersion);
121 while (socket->bytesAvailable() > 0) {
303669c @vos simple network protocoll
authored
122 if (client->packetSize == 0) {
d3a588b @vos chat messages, some bug fixes and optimizations
authored
123 if (socket->bytesAvailable() < (int)sizeof(PacketSize))
303669c @vos simple network protocoll
authored
124 return;
125 in >> client->packetSize;
126 }
d3a588b @vos chat messages, some bug fixes and optimizations
authored
127 if (socket->bytesAvailable() < client->packetSize)
62570b0 @vos added multiplayer core implementation
authored
128 return;
303669c @vos simple network protocoll
authored
129 client->packetSize = 0; // reset packet size
62570b0 @vos added multiplayer core implementation
authored
130
303669c @vos simple network protocoll
authored
131 // read packet type
8ee40f5 @vos MP game mode changed and planet changed packets
authored
132 EnumType packetType; // MultiplayerPacket::PacketType
303669c @vos simple network protocoll
authored
133 in >> packetType;
62570b0 @vos added multiplayer core implementation
authored
134
135 #ifdef MULTIPLAYERSERVER_DEBUG
214c4b5 @vos debug output fixed
authored
136 qDebug("PacketType %i (%s) from %s (%s:%i)", packetType,
303669c @vos simple network protocoll
authored
137 qPrintable(MultiplayerPacket::typeString((MultiplayerPacket::PacketType)packetType)),
214c4b5 @vos debug output fixed
authored
138 qPrintable(client->player ? QString("'%1' (%2)").arg(client->player->name()).arg(client->id) : "[no player object]"),
303669c @vos simple network protocoll
authored
139 qPrintable(socket->peerAddress().toString()), socket->peerPort());
62570b0 @vos added multiplayer core implementation
authored
140 #endif
141
303669c @vos simple network protocoll
authored
142 // read and handle packet data
143 switch ((MultiplayerPacket::PacketType)packetType) {
1785d63 @vos MP player handling and "dedicated server"
authored
144 case MultiplayerPacket::PlayerConnect: {
303669c @vos simple network protocoll
authored
145 Player *player = new Player;
146 in >> *player;
147 if (m_game->addPlayer(player)) {
148 client->player = player;
1785d63 @vos MP player handling and "dedicated server"
authored
149 client->id = m_nextPlayerId++;
be0848f @vos display player id in player list
authored
150 m_idPlayerMap.insert(client->id, client->player);
1785d63 @vos MP player handling and "dedicated server"
authored
151
152 // accept the player
153 MultiplayerPacket connectAcceptedPacket(MultiplayerPacket::PlayerConnectAccepted);
a430d2a @vos added MultiplayerPacket << stream operator
authored
154 connectAcceptedPacket << client->id;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
155 connectAcceptedPacket.packAndSend(socket);
1785d63 @vos MP player handling and "dedicated server"
authored
156
157 // send the client the other players data
68fd991 @vos UDP connection from server to clients should work now
authored
158 foreach (Client *otherClient, m_tcpClientMap.values()) {
1785d63 @vos MP player handling and "dedicated server"
authored
159 if (otherClient != client && otherClient->isConnected()) {
160 MultiplayerPacket otherPlayerPacket(MultiplayerPacket::PlayerConnected); // send PlayerList in one packet?
a430d2a @vos added MultiplayerPacket << stream operator
authored
161 otherPlayerPacket << otherClient->id << *otherClient->player;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
162 otherPlayerPacket.packAndSend(socket);
1785d63 @vos MP player handling and "dedicated server"
authored
163 }
164 }
165
166 // inform the other clients about the new player
167 MultiplayerPacket playerConnectedPacket(MultiplayerPacket::PlayerConnected);
a430d2a @vos added MultiplayerPacket << stream operator
authored
168 playerConnectedPacket << client->id << *player;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
169 playerConnectedPacket.pack();
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
170 sendTcpPacketToOtherClients(playerConnectedPacket, socket);
1785d63 @vos MP player handling and "dedicated server"
authored
171 } else {
172 MultiplayerPacket(MultiplayerPacket::PlayerConnectDenied).send(socket);
303669c @vos simple network protocoll
authored
173 }
174 break;
175 }
176 case MultiplayerPacket::PlayerDisconnect:
177 socket->disconnectFromHost();
178 break;
62f001a @vos MP: player and planet change events
authored
179 case MultiplayerPacket::PlayerChanged: {
180 EnumType changeType; // Player::ChangeType
181 in >> changeType;
182 Player *player = client->player;
183 MultiplayerPacket playerChangedPacket(MultiplayerPacket::PlayerChanged);
a430d2a @vos added MultiplayerPacket << stream operator
authored
184 playerChangedPacket << client->id << changeType;
62f001a @vos MP: player and planet change events
authored
185 switch ((Player::ChangeType)changeType) {
186 case Player::NameChange: {
187 QString name;
188 in >> name;
189 player->setName(name);
a430d2a @vos added MultiplayerPacket << stream operator
authored
190 playerChangedPacket << name;
62f001a @vos MP: player and planet change events
authored
191 break;
192 }
193 case Player::ColorChange: {
194 QColor color;
195 in >> color;
196 player->setColor(color);
a430d2a @vos added MultiplayerPacket << stream operator
authored
197 playerChangedPacket << color;
62f001a @vos MP: player and planet change events
authored
198 break;
199 }
200 case Player::ResourceFactorChange: {
201 qreal resourceFactor;
202 in >> resourceFactor;
203 player->setResourceFactor(resourceFactor);
a430d2a @vos added MultiplayerPacket << stream operator
authored
204 playerChangedPacket << resourceFactor;
62f001a @vos MP: player and planet change events
authored
205 break;
206 }
207 default:
208 qWarning("invalid Player::ChangeType: %d", changeType);
209 }
210 playerChangedPacket.pack();
211 sendTcpPacketToOtherClients(playerChangedPacket, socket);
212 break;
213 }
d3a588b @vos chat messages, some bug fixes and optimizations
authored
214 case MultiplayerPacket::Chat: {
215 QString msg;
216 in >> msg;
9315590 @vos log view and multiplayer chat
authored
217 emit chatMessageReceived(msg, client->player);
d3a588b @vos chat messages, some bug fixes and optimizations
authored
218 MultiplayerPacket chatPacket(MultiplayerPacket::Chat);
a430d2a @vos added MultiplayerPacket << stream operator
authored
219 chatPacket << msg << client->id;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
220 chatPacket.pack();
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
221 sendTcpPacketToOtherClients(chatPacket, socket);
d3a588b @vos chat messages, some bug fixes and optimizations
authored
222 break;
223 }
8ee40f5 @vos MP game mode changed and planet changed packets
authored
224 case MultiplayerPacket::ModeChanged: {
225 EnumType mode; // Game::Mode
226 in >> mode;
227 qDebug("mode = %s", qPrintable(Game::modeString((Game::Mode)mode)));
228 m_game->setMode((Game::Mode)mode);
229 MultiplayerPacket modeChangedPacket(MultiplayerPacket::ModeChanged);
a430d2a @vos added MultiplayerPacket << stream operator
authored
230 modeChangedPacket << mode;
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
231 sendTcpPacketToOtherClients(modeChangedPacket, socket);
8ee40f5 @vos MP game mode changed and planet changed packets
authored
232 break;
233 }
0eae0d6 @vos Multiplayer planetAdded event
authored
234 case MultiplayerPacket::PlanetAdded: {
8f0824c @vos MP added planet (still buggy)
authored
235 PlanetID tempPlanetId;
0eae0d6 @vos Multiplayer planetAdded event
authored
236 Planet *planet = new Planet;
8f0824c @vos MP added planet (still buggy)
authored
237 in >> tempPlanetId >> *planet;
1785d63 @vos MP player handling and "dedicated server"
authored
238 if (m_game->addPlanet(planet)) {
f968e85 @vos MP: resourcesTransferInitiated event
authored
239 client->player->addPlanet(planet);
8f0824c @vos MP added planet (still buggy)
authored
240 PlanetID planetId = m_nextPlanetId++;
c7ed731 @vos remove planet (multiplayer)
authored
241 m_idPlanetMap.insert(planetId, planet);
8f0824c @vos MP added planet (still buggy)
authored
242 // send real id to the creator
243 MultiplayerPacket planetIdPacket(MultiplayerPacket::PlanetId);
a430d2a @vos added MultiplayerPacket << stream operator
authored
244 planetIdPacket << tempPlanetId << planetId;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
245 planetIdPacket.packAndSend(socket);
8f0824c @vos MP added planet (still buggy)
authored
246 // add id and resend packet to all other clients
247 MultiplayerPacket planetAddedPacket(MultiplayerPacket::PlanetAdded);
a430d2a @vos added MultiplayerPacket << stream operator
authored
248 planetAddedPacket << planetId << *planet << client->id;
d3a588b @vos chat messages, some bug fixes and optimizations
authored
249 planetAddedPacket.pack();
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
250 sendTcpPacketToOtherClients(planetAddedPacket, socket);
c7ed731 @vos remove planet (multiplayer)
authored
251 }
252 break;
253 }
254 case MultiplayerPacket::PlanetRemoved: {
255 PlanetID planetId;
256 in >> planetId;
0e9b8fd @vos BiMap used and some multiplayer optimizations
authored
257 Planet *planet = m_idPlanetMap.takeValue(planetId);
c7ed731 @vos remove planet (multiplayer)
authored
258 Q_ASSERT(planet);
259 if (m_game->removePlanet(planet)) {
260 MultiplayerPacket planetRemovedPacket(MultiplayerPacket::PlanetRemoved);
a430d2a @vos added MultiplayerPacket << stream operator
authored
261 planetRemovedPacket << planetId;
c7ed731 @vos remove planet (multiplayer)
authored
262 planetRemovedPacket.pack();
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
263 sendTcpPacketToOtherClients(planetRemovedPacket, socket);
1785d63 @vos MP player handling and "dedicated server"
authored
264 }
0eae0d6 @vos Multiplayer planetAdded event
authored
265 break;
266 }
f968e85 @vos MP: resourcesTransferInitiated event
authored
267 case MultiplayerPacket::ResourcesTransferInitiated: {
268 PlanetID originId, targetId;
269 qreal resourceFactor;
270 in >> originId >> targetId >> resourceFactor;
271 Planet *origin = m_idPlanetMap.value(originId);
272 Q_ASSERT(origin);
273 Planet *target = m_idPlanetMap.value(targetId);
274 Q_ASSERT(target);
275 if (origin->transferResourcesTo(target, resourceFactor) != NULL) {
276 MultiplayerPacket resourcesTransferInitiatedPacket(MultiplayerPacket::ResourcesTransferInitiated);
277 resourcesTransferInitiatedPacket << originId << targetId << resourceFactor;
278 sendTcpPacketToOtherClients(resourcesTransferInitiatedPacket, socket);
279 }
280 break;
281 }
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
282 default:
283 qWarning("MultiplayerServer::client_readyRead(): Illegal PacketType %i", packetType);
284 return;
285 }
286 }
287 }
288
289 void MultiplayerServer::client_udpReadyRead()
290 {
291 #ifdef MULTIPLAYERCLIENT_DEBUG
292 qDebug("MultiplayerServer::client_udpReadyRead(): %li bytes available", (long)m_udpSocket.bytesAvailable());
293 #endif
294
295 while (m_udpSocket.hasPendingDatagrams()) {
296 QByteArray datagram;
297 datagram.resize(m_udpSocket.pendingDatagramSize());
298 QHostAddress senderAddress;
299 quint16 senderPort;
300 m_udpSocket.readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort);
301
68fd991 @vos UDP connection from server to clients should work now
authored
302 PlayerID senderId = m_udpClientMap.key(qMakePair(senderAddress, senderPort));
303
304 #ifdef MULTIPLAYERSERVER_DEBUG
305 qDebug("sender: %s:%d (%d -> '%s'')", qPrintable(senderAddress.toString()), senderPort, senderId,
306 qPrintable(senderId > 0 ? m_idPlayerMap.value(senderId)->name() : "[no player id]"));
307 #endif
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
308
309 QDataStream in(datagram);
310 in.setVersion(MultiplayerPacket::StreamVersion);
311
312 // skip packet size (ignored with datagrams over UDP)
313 in.skipRawData(sizeof(PacketSize));
314
315 // read packet type
316 EnumType packetType; // MultiplayerPacket::PacketType
317 in >> packetType;
318
319 #ifdef MULTIPLAYERCLIENT_DEBUG
320 qDebug("PacketType %i (%s)", packetType, qPrintable(MultiplayerPacket::typeString((MultiplayerPacket::PacketType)packetType)));
321 #endif
322
323 // read and handle packet data
324 switch ((MultiplayerPacket::PacketType)packetType) {
68fd991 @vos UDP connection from server to clients should work now
authored
325 case MultiplayerPacket::UdpRegister: {
326 PlayerID playerId;
327 in >> playerId;
328 m_udpClientMap.insert(playerId, qMakePair(senderAddress, senderPort));
329 break;
330 }
8ee40f5 @vos MP game mode changed and planet changed packets
authored
331 case MultiplayerPacket::PlanetChanged: {
332 PlanetID planetId;
333 EnumType changeType; // Planet::ChangeType
334 in >> planetId >> changeType;
335 Planet *planet = m_idPlanetMap.value(planetId);
336 Q_ASSERT(planet);
337 MultiplayerPacket planetChangedPacket(MultiplayerPacket::PlanetChanged);
a430d2a @vos added MultiplayerPacket << stream operator
authored
338 planetChangedPacket << planetId << changeType;
8ee40f5 @vos MP game mode changed and planet changed packets
authored
339 switch ((Planet::ChangeType)changeType) {
340 case Planet::PositionChange: {
341 QVector2D position;
342 in >> position;
343 planet->setPosition(position);
a430d2a @vos added MultiplayerPacket << stream operator
authored
344 planetChangedPacket << position;
8ee40f5 @vos MP game mode changed and planet changed packets
authored
345 break;
346 }
347 case Planet::RadiusChange: {
348 qreal radius;
349 in >> radius;
350 planet->setRadius(radius);
a430d2a @vos added MultiplayerPacket << stream operator
authored
351 planetChangedPacket << radius;
8ee40f5 @vos MP game mode changed and planet changed packets
authored
352 break;
353 }
62f001a @vos MP: player and planet change events
authored
354 case Planet::ResourcesChange: {
355 qreal resources;
356 in >> resources;
357 planet->setResources(resources);
a430d2a @vos added MultiplayerPacket << stream operator
authored
358 planetChangedPacket << resources;
62f001a @vos MP: player and planet change events
authored
359 break;
360 }
361 case Planet::ProductionFactorChange: {
362 qreal productionFactor;
363 in >> productionFactor;
364 planet->setProductionFactor(productionFactor);
a430d2a @vos added MultiplayerPacket << stream operator
authored
365 planetChangedPacket << productionFactor;
62f001a @vos MP: player and planet change events
authored
366 break;
367 }
8ee40f5 @vos MP game mode changed and planet changed packets
authored
368 default:
369 qWarning("invalid Planet::ChangeType: %d", changeType);
370 }
371 planetChangedPacket.pack();
68fd991 @vos UDP connection from server to clients should work now
authored
372 sendUdpPacketToOtherClients(planetChangedPacket, senderId);
8ee40f5 @vos MP game mode changed and planet changed packets
authored
373 break;
374 }
303669c @vos simple network protocoll
authored
375 default:
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
376 qWarning("MultiplayerServer::client_udpReadyRead(): Illegal PacketType %i", packetType);
303669c @vos simple network protocoll
authored
377 return;
378 }
62570b0 @vos added multiplayer core implementation
authored
379 }
380 }
381
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
382 void MultiplayerServer::sendChatMessage(const QString &msg)
383 {
384 MultiplayerPacket packet(MultiplayerPacket::Chat);
a430d2a @vos added MultiplayerPacket << stream operator
authored
385 packet << msg << PlayerID(0); // server has PlayerID 0
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
386 packet.pack();
387 sendTcpPacketToAllClients(packet);
388 }
389
390 void MultiplayerServer::sendTcpPacketToOtherClients(const MultiplayerPacket &packet, const QTcpSocket *sender)
0eae0d6 @vos Multiplayer planetAdded event
authored
391 {
d3a588b @vos chat messages, some bug fixes and optimizations
authored
392 QHash<QTcpSocket*, Client*>::const_iterator it;
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
393 for (it = m_tcpClientMap.constBegin(); it != m_tcpClientMap.constEnd(); ++it) {
d3a588b @vos chat messages, some bug fixes and optimizations
authored
394 QTcpSocket *clientSocket = it.key();
395 const Client *client = it.value();
396 if (clientSocket != sender && client->isConnected()) {
397 packet.send(clientSocket);
0eae0d6 @vos Multiplayer planetAdded event
authored
398 }
399 }
400 }
401
68fd991 @vos UDP connection from server to clients should work now
authored
402 void MultiplayerServer::sendUdpPacketToOtherClients(const MultiplayerPacket &packet, const PlayerID senderId)
62570b0 @vos added multiplayer core implementation
authored
403 {
68fd991 @vos UDP connection from server to clients should work now
authored
404 QHash<PlayerID, QPair<QHostAddress, quint16> >::const_iterator it;
405 for (it = m_udpClientMap.hash().constBegin(); it != m_udpClientMap.hash().constEnd(); ++it) {
406 const PlayerID playerId = it.key();
407 if (playerId != senderId) {
62f001a @vos MP: player and planet change events
authored
408 const QPair<QHostAddress, quint16> &clientAddress = it.value();
68fd991 @vos UDP connection from server to clients should work now
authored
409 #ifdef MULTIPLAYERSERVER_DEBUG
62f001a @vos MP: player and planet change events
authored
410 qDebug("m_udpSocket.writeDatagram(size = %d, receiver = %s:%d)", packet.size(), qPrintable(clientAddress.first.toString()), clientAddress.second);
68fd991 @vos UDP connection from server to clients should work now
authored
411 #endif
a6e45e6 @vos multiplayer TCP and UDP connections (UDP to client is still buggy!)
authored
412 m_udpSocket.writeDatagram(packet.data(), clientAddress.first, clientAddress.second);
413 }
414 }
62570b0 @vos added multiplayer core implementation
authored
415 }
Something went wrong with that request. Please try again.