| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name network.cpp - The network. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2000-2008 by Lutz Sammer, Andreas Arens, and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
//---------------------------------------------------------------------------- |
| 32 |
// Documentation |
| 33 |
//---------------------------------------------------------------------------- |
| 34 |
|
| 35 |
/** |
| 36 |
** @page NetworkModule Module - Network |
| 37 |
** |
| 38 |
** @section Basics How does it work. |
| 39 |
** |
| 40 |
** Stratagus uses an UDP peer to peer protocol (p2p). The default port |
| 41 |
** is 6660. |
| 42 |
** |
| 43 |
** @subsection udp_vs_tcp UDP vs. TCP |
| 44 |
** |
| 45 |
** UDP is a connectionless protocol. This means it does not perform |
| 46 |
** retransmission of data and therefore provides very few error recovery |
| 47 |
** services. UDP instead offers a direct way to send and receive |
| 48 |
** datagrams (packets) over the network; it is used primarily for |
| 49 |
** broadcasting messages. |
| 50 |
** |
| 51 |
** TCP, on the other hand, provides a connection-based, reliable data |
| 52 |
** stream. TCP guarantees delivery of data and also guarantees that |
| 53 |
** packets will be delivered in the same order in which they were sent. |
| 54 |
** |
| 55 |
** TCP is a simple and effective way of transmitting data. For making sure |
| 56 |
** that a client and server can talk to each other it is very good. |
| 57 |
** However, it carries with it a lot of overhead and extra network lag. |
| 58 |
** |
| 59 |
** UDP needs less overhead and has a smaller lag. Which is very important |
| 60 |
** for real time games. The disadvantages includes: |
| 61 |
** |
| 62 |
** @li You won't have an individual socket for each client. |
| 63 |
** @li Given that clients don't need to open a unique socket in order to |
| 64 |
** transmit data there is the very real possibility that a client |
| 65 |
** who is not logged into the game will start sending all kinds of |
| 66 |
** garbage to your server in some kind of attack. It becomes much |
| 67 |
** more difficult to stop them at this point. |
| 68 |
** @li Likewise, you won't have a clear disconnect/leave game message |
| 69 |
** unless you write one yourself. |
| 70 |
** @li Some data may not reach the other machine, so you may have to send |
| 71 |
** important stuff many times. |
| 72 |
** @li Some data may arrive in the wrong order. Imagine that you get |
| 73 |
** package 3 before package 1. Even a package can come duplicate. |
| 74 |
** @li UDP is connectionless and therefore has problems with firewalls. |
| 75 |
** |
| 76 |
** I have choosen UDP. Additional support for the TCP protocol is welcome. |
| 77 |
** |
| 78 |
** @subsection sc_vs_p2p server/client vs. peer to peer |
| 79 |
** |
| 80 |
** @li server to client |
| 81 |
** |
| 82 |
** The player input is send to the server. The server collects the input |
| 83 |
** of all players and than send the commands to all clients. |
| 84 |
** |
| 85 |
** @li peer to peer (p2p) |
| 86 |
** |
| 87 |
** The player input is direct send to all others clients in game. |
| 88 |
** |
| 89 |
** p2p has the advantage of a smaller lag, but needs a higher bandwidth |
| 90 |
** by the clients. |
| 91 |
** |
| 92 |
** I have choosen p2p. Additional support for a server to client protocol |
| 93 |
** is welcome. |
| 94 |
** |
| 95 |
** @subsection bandwidth bandwidth |
| 96 |
** |
| 97 |
** I wanted to support up to 8 players with 28.8kbit modems. |
| 98 |
** |
| 99 |
** Most modems have a bandwidth of 28.8K bits/second (both directions) to |
| 100 |
** 56K bits/second (33.6K uplink) It takes actually 10 bits to send 1 byte. |
| 101 |
** This makes calculating how many bytes you are sending easy however, as |
| 102 |
** you just need to divide 28800 bits/second by 10 and end up with 2880 |
| 103 |
** bytes per second. |
| 104 |
** |
| 105 |
** We want to send many packets, more updated pro second and big packets, |
| 106 |
** less protocol overhead. |
| 107 |
** |
| 108 |
** If we do an update 6 times per second, leaving approximately 480 bytes |
| 109 |
** per update in an ideal environment. |
| 110 |
** |
| 111 |
** For the TCP/IP protocol we need following: |
| 112 |
** IP Header 20 bytes |
| 113 |
** UDP Header 8 bytes |
| 114 |
** |
| 115 |
** With 10 bytes per command and 4 commands this are 68 (20+8+4*10) bytes |
| 116 |
** pro packet. Sending it to 7 other players, gives 476 bytes pro update. |
| 117 |
** This means we could do 6 updates (each 166ms) pro second. |
| 118 |
** |
| 119 |
** @subsection a_packet Network packet |
| 120 |
** |
| 121 |
** @li [IP Header - 20 bytes] |
| 122 |
** @li [UDP Header - 8 bytes] |
| 123 |
** @li [Type 1 byte][Cycle 1 byte][Data 8 bytes] - Slot 0 |
| 124 |
** @li [Type 1 byte][Cycle 1 byte][Data 8 bytes] - Slot 1 |
| 125 |
** @li [Type 1 byte][Cycle 1 byte][Data 8 bytes] - Slot 2 |
| 126 |
** @li [Type 1 byte][Cycle 1 byte][Data 8 bytes] - Slot 3 |
| 127 |
** |
| 128 |
** @subsection internals Putting it together |
| 129 |
** |
| 130 |
** All computers in play must run absolute syncron. Only user commands |
| 131 |
** are send over the network to the other computers. The command needs |
| 132 |
** some time to reach the other clients (lag), so the command is not |
| 133 |
** executed immediatly on the local computer, it is stored in a delay |
| 134 |
** queue and send to all other clients. After a delay of ::NetworkLag |
| 135 |
** game cycles the commands of the other players are received and executed |
| 136 |
** together with the local command. Each ::NetworkUpdates game cycles there |
| 137 |
** must a package send, to keep the clients in sync, if there is no user |
| 138 |
** command, a dummy sync package is send. |
| 139 |
** If there are missing packages, the game is paused and old commands |
| 140 |
** are resend to all clients. |
| 141 |
** |
| 142 |
** @section missing What features are missing |
| 143 |
** |
| 144 |
** @li The recover from lost packets can be improved, if the server knows |
| 145 |
** which packets the clients have received. |
| 146 |
** |
| 147 |
** @li The UDP protocol isn't good for firewalls, we need also support |
| 148 |
** for the TCP protocol. |
| 149 |
** |
| 150 |
** @li Add a server / client protocol, which allows more players pro |
| 151 |
** game. |
| 152 |
** |
| 153 |
** @li Lag (latency) and bandwidth are set over the commandline. This |
| 154 |
** should be automatic detected during game setup and later during |
| 155 |
** game automatic adapted. |
| 156 |
** |
| 157 |
** @li Also it would be nice, if we support viewing clients. This means |
| 158 |
** other people can view the game in progress. |
| 159 |
** |
| 160 |
** @li The current protocol only uses single cast, for local LAN we |
| 161 |
** should also support broadcast and multicast. |
| 162 |
** |
| 163 |
** @li Proxy and relays should be supported, to improve the playable |
| 164 |
** over the internet. |
| 165 |
** |
| 166 |
** @li The game cycles is transfered for each slot, this is not needed. We |
| 167 |
** can save some bytes if we compress this. |
| 168 |
** |
| 169 |
** @li We can sort the command by importants, currently all commands are |
| 170 |
** send in order, only chat messages are send if there are free slots. |
| 171 |
** |
| 172 |
** @li password protection the login process (optional), to prevent that |
| 173 |
** the wrong player join an open network game. |
| 174 |
** |
| 175 |
** @li add meta server support, i have planned to use bnetd and its |
| 176 |
** protocol. |
| 177 |
** |
| 178 |
** @section api API How should it be used. |
| 179 |
** |
| 180 |
** ::InitNetwork1() |
| 181 |
** |
| 182 |
** ::InitNetwork2() |
| 183 |
** |
| 184 |
** ::ExitNetwork1() |
| 185 |
** |
| 186 |
** ::NetworkSendCommand() |
| 187 |
** |
| 188 |
** ::NetworkSendExtendedCommand() |
| 189 |
** |
| 190 |
** ::NetworkEvent() |
| 191 |
** |
| 192 |
** ::NetworkQuit() |
| 193 |
** |
| 194 |
** ::NetworkChatMessage() |
| 195 |
** |
| 196 |
** ::NetworkEvent() |
| 197 |
** |
| 198 |
** ::NetworkRecover() |
| 199 |
** |
| 200 |
** ::NetworkCommands() |
| 201 |
** |
| 202 |
** ::NetworkFildes |
| 203 |
** |
| 204 |
** ::NetworkInSync |
| 205 |
** |
| 206 |
** @todo FIXME: continue docu |
| 207 |
*/ |
| 208 |
|
| 209 |
//---------------------------------------------------------------------------- |
| 210 |
// Includes |
| 211 |
//---------------------------------------------------------------------------- |
| 212 |
|
| 213 |
#include "stratagus.h" |
| 214 |
|
| 215 |
#include <stdio.h> |
| 216 |
#include <stdlib.h> |
| 217 |
#include <stddef.h> |
| 218 |
#include <string.h> |
| 219 |
#include <list> |
| 220 |
|
| 221 |
#include "net_lowlevel.h" |
| 222 |
#include "unit.h" |
| 223 |
#include "unit_manager.h" |
| 224 |
#include "unittype.h" |
| 225 |
#include "map.h" |
| 226 |
#include "actions.h" |
| 227 |
#include "player.h" |
| 228 |
#include "network.h" |
| 229 |
#include "netconnect.h" |
| 230 |
#include "commands.h" |
| 231 |
#include "replay.h" |
| 232 |
#include "interface.h" |
| 233 |
#include "results.h" |
| 234 |
#include "sound.h" |
| 235 |
|
| 236 |
|
| 237 |
//---------------------------------------------------------------------------- |
| 238 |
// Declaration |
| 239 |
//---------------------------------------------------------------------------- |
| 240 |
|
| 241 |
/** |
| 242 |
** Network command input/output queue. |
| 243 |
*/ |
| 244 |
class CNetworkCommandQueue { |
| 245 |
public: |
| 246 |
CNetworkCommandQueue() : Time(0), Type(0) {} |
| 247 |
void Clear() { this->Time = this->Type = 0; Data.Clear(); } |
| 248 |
|
| 249 |
unsigned long Time; /// time to execute |
| 250 |
unsigned char Type; /// Command Type |
| 251 |
CNetworkCommand Data; /// command content |
| 252 |
}; |
| 253 |
|
| 254 |
//---------------------------------------------------------------------------- |
| 255 |
// Variables |
| 256 |
//---------------------------------------------------------------------------- |
| 257 |
|
| 258 |
int NetworkNumInterfaces; /// Network number of interfaces |
| 259 |
Socket NetworkFildes = (Socket)-1; /// Network file descriptor |
| 260 |
int NetworkInSync = 1; /// Network is in sync |
| 261 |
int NetworkUpdates = 5; /// Network update each # game cycles |
| 262 |
int NetworkLag = 10; /// Network lag in # game cycles |
| 263 |
unsigned long NetworkStatus[PlayerMax]; /// Network status |
| 264 |
unsigned long NetworkLastFrame[PlayerMax]; /// Last frame received packet |
| 265 |
int NetworkTimeout = 45; /// Number of seconds until player times out |
| 266 |
|
| 267 |
static char NetMsgBuf[PlayerMax][128]; /// Chat message buffers |
| 268 |
static int NetMsgBufLen[PlayerMax]; /// Stored chat message length |
| 269 |
#ifdef DEBUG |
| 270 |
unsigned long MyHost; /// My host number. |
| 271 |
int MyPort; /// My port number. |
| 272 |
#endif |
| 273 |
static unsigned long NetworkDelay; /// Delay counter for recover. |
| 274 |
static int NetworkSyncSeeds[256]; /// Network sync seeds. |
| 275 |
static int NetworkSyncHashs[256]; /// Network sync hashs. |
| 276 |
static CNetworkCommandQueue NetworkIn[256][PlayerMax][MaxNetworkCommands]; /// Per-player network packet input queue |
| 277 |
std::list<CNetworkCommandQueue *> CommandsIn; /// Network command input queue |
| 278 |
std::list<CNetworkCommandQueue *> MsgCommandsIn;/// Network message input queue |
| 279 |
|
| 280 |
#ifdef DEBUG |
| 281 |
static int NetworkReceivedPackets; /// Packets received packets |
| 282 |
static int NetworkReceivedEarly; /// Packets received too early |
| 283 |
static int NetworkReceivedLate; /// Packets received too late |
| 284 |
static int NetworkReceivedDups; /// Packets received as duplicates |
| 285 |
static int NetworkReceivedLost; /// Packets received packet lost |
| 286 |
|
| 287 |
static int NetworkSendPackets; /// Packets send packets |
| 288 |
static int NetworkSendResend; /// Packets send to resend |
| 289 |
#endif |
| 290 |
|
| 291 |
static int PlayerQuit[PlayerMax]; /// Player quit |
| 292 |
|
| 293 |
#define MAX_NCQS 100 |
| 294 |
static CNetworkCommandQueue NCQs[MAX_NCQS]; /// CNetworkCommandQueues |
| 295 |
static int NumNCQs; /// Number of NCQs in use |
| 296 |
|
| 297 |
|
| 298 |
//---------------------------------------------------------------------------- |
| 299 |
// Serialize/Deserialize |
| 300 |
//---------------------------------------------------------------------------- |
| 301 |
|
| 302 |
void CNetworkCommand::Serialize(unsigned char *p) const |
| 303 |
{ |
| 304 |
*(Uint16 *)p = this->Unit; |
| 305 |
p += 2; |
| 306 |
*(Uint16 *)p = this->X; |
| 307 |
p += 2; |
| 308 |
*(Uint16 *)p = this->Y; |
| 309 |
p += 2; |
| 310 |
*(Uint16 *)p = this->Dest; |
| 311 |
p += 2; |
| 312 |
} |
| 313 |
|
| 314 |
void CNetworkCommand::Deserialize(unsigned char *p) |
| 315 |
{ |
| 316 |
this->Unit = *(Uint16 *)p; |
| 317 |
p += 2; |
| 318 |
this->X = *(Uint16 *)p; |
| 319 |
p += 2; |
| 320 |
this->Y = *(Uint16 *)p; |
| 321 |
p += 2; |
| 322 |
this->Dest = *(Uint16 *)p; |
| 323 |
p += 2; |
| 324 |
} |
| 325 |
|
| 326 |
void CNetworkExtendedCommand::Serialize(unsigned char *p) |
| 327 |
{ |
| 328 |
*p++ = this->ExtendedType; |
| 329 |
*p++ = this->Arg1; |
| 330 |
*(Uint16 *)p = this->Arg2; |
| 331 |
p += 2; |
| 332 |
*(Uint16 *)p = this->Arg3; |
| 333 |
p += 2; |
| 334 |
*(Uint16 *)p = this->Arg4; |
| 335 |
p += 2; |
| 336 |
} |
| 337 |
|
| 338 |
void CNetworkExtendedCommand::Deserialize(unsigned char *p) |
| 339 |
{ |
| 340 |
this->ExtendedType = *p++; |
| 341 |
this->Arg1 = *p++; |
| 342 |
this->Arg2 = *(Uint16 *)p; |
| 343 |
p += 2; |
| 344 |
this->Arg3 = *(Uint16 *)p; |
| 345 |
p += 2; |
| 346 |
this->Arg4 = *(Uint16 *)p; |
| 347 |
p += 2; |
| 348 |
} |
| 349 |
|
| 350 |
void CNetworkChat::Serialize(unsigned char *p) |
| 351 |
{ |
| 352 |
*p++ = this->Player; |
| 353 |
memcpy(p, this->Text, 7); |
| 354 |
p += 7; |
| 355 |
} |
| 356 |
|
| 357 |
void CNetworkChat::Deserialize(unsigned char *p) |
| 358 |
{ |
| 359 |
this->Player = *p++; |
| 360 |
memcpy(this->Text, p, 7); |
| 361 |
p += 7; |
| 362 |
} |
| 363 |
|
| 364 |
void CNetworkPacketHeader::Serialize(unsigned char *p) const |
| 365 |
{ |
| 366 |
*p++ = this->Cycle; |
| 367 |
for (int i = 0; i < MaxNetworkCommands; ++i) { |
| 368 |
*p++ = this->Type[i]; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
void CNetworkPacketHeader::Deserialize(unsigned char *p) |
| 373 |
{ |
| 374 |
this->Cycle = *p++; |
| 375 |
for (int i = 0; i < MaxNetworkCommands; ++i) { |
| 376 |
this->Type[i] = *p++; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
unsigned char *CNetworkPacket::Serialize(int numcommands) const |
| 381 |
{ |
| 382 |
unsigned char *buf = new unsigned char[CNetworkPacket::Size(numcommands)]; |
| 383 |
unsigned char *p = buf; |
| 384 |
|
| 385 |
this->Header.Serialize(p); |
| 386 |
p += CNetworkPacketHeader::Size(); |
| 387 |
|
| 388 |
for (int i = 0; i < numcommands; ++i) { |
| 389 |
if (this->Header.Type[i] == MessageExtendedCommand) { |
| 390 |
CNetworkExtendedCommand *nec; |
| 391 |
nec = (CNetworkExtendedCommand *)&Command[i]; |
| 392 |
nec->Serialize(p); |
| 393 |
} else if (this->Header.Type[i] == MessageChat) { |
| 394 |
((CNetworkChat *)&this->Command[i])->Serialize(p); |
| 395 |
} else { |
| 396 |
this->Command[i].Serialize(p); |
| 397 |
} |
| 398 |
p += CNetworkCommand::Size(); |
| 399 |
} |
| 400 |
|
| 401 |
return buf; |
| 402 |
} |
| 403 |
|
| 404 |
int CNetworkPacket::Deserialize(unsigned char *p, unsigned int len) |
| 405 |
{ |
| 406 |
// check min and max size |
| 407 |
if (len < CNetworkPacket::Size(1) || |
| 408 |
len > CNetworkPacket::Size(MaxNetworkCommands)) { |
| 409 |
return -1; |
| 410 |
} |
| 411 |
|
| 412 |
// can't have partial commands |
| 413 |
len -= CNetworkPacketHeader::Size(); |
| 414 |
if ((len / CNetworkCommand::Size()) * CNetworkCommand::Size() != len) { |
| 415 |
return -1; |
| 416 |
} |
| 417 |
|
| 418 |
this->Header.Deserialize(p); |
| 419 |
p += CNetworkPacketHeader::Size(); |
| 420 |
|
| 421 |
int commands = len / CNetworkCommand::Size(); |
| 422 |
|
| 423 |
for (int i = 0; i < commands; ++i) { |
| 424 |
if (this->Header.Type[i] == MessageExtendedCommand) { |
| 425 |
CNetworkExtendedCommand *nec; |
| 426 |
nec = (CNetworkExtendedCommand *)&Command[i]; |
| 427 |
nec->Deserialize(p); |
| 428 |
} else if (this->Header.Type[i] == MessageChat) { |
| 429 |
((CNetworkChat *)&this->Command[i])->Deserialize(p); |
| 430 |
} else { |
| 431 |
this->Command[i].Deserialize(p); |
| 432 |
} |
| 433 |
p += CNetworkCommand::Size(); |
| 434 |
} |
| 435 |
|
| 436 |
return commands; |
| 437 |
} |
| 438 |
|
| 439 |
//---------------------------------------------------------------------------- |
| 440 |
// Mid-Level api functions |
| 441 |
//---------------------------------------------------------------------------- |
| 442 |
|
| 443 |
/** |
| 444 |
** Send message to all clients. |
| 445 |
** |
| 446 |
** @param packet Packet to send. |
| 447 |
** @param numcommands Number of commands. |
| 448 |
*/ |
| 449 |
static void NetworkBroadcast(const CNetworkPacket *packet, int numcommands) |
| 450 |
{ |
| 451 |
unsigned char *buf = packet->Serialize(numcommands); |
| 452 |
|
| 453 |
// Send to all clients. |
| 454 |
for (int i = 0; i < HostsCount; ++i) { |
| 455 |
NetSendUDP(NetworkFildes, Hosts[i].Host, Hosts[i].Port, buf, |
| 456 |
CNetworkPacket::Size(numcommands)); |
| 457 |
} |
| 458 |
|
| 459 |
delete[] buf; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
** Network send packet. Build it from queue and broadcast. |
| 464 |
** |
| 465 |
** @param ncq Outgoing network queue start. |
| 466 |
*/ |
| 467 |
static void NetworkSendPacket(const CNetworkCommandQueue *ncq) |
| 468 |
{ |
| 469 |
CNetworkPacket packet; |
| 470 |
int i; |
| 471 |
int numcommands; |
| 472 |
|
| 473 |
#ifdef DEBUG |
| 474 |
++NetworkSendPackets; |
| 475 |
#endif |
| 476 |
|
| 477 |
// |
| 478 |
// Build packet of up to MaxNetworkCommands messages. |
| 479 |
// |
| 480 |
numcommands = 0; |
| 481 |
packet.Header.Cycle = ncq[0].Time & 0xFF; |
| 482 |
for (i = 0; i < MaxNetworkCommands && ncq[i].Type != MessageNone; ++i) { |
| 483 |
packet.Header.Type[i] = ncq[i].Type; |
| 484 |
packet.Command[i] = ncq[i].Data; |
| 485 |
++numcommands; |
| 486 |
} |
| 487 |
|
| 488 |
for (; i < MaxNetworkCommands; ++i) { |
| 489 |
packet.Header.Type[i] = MessageNone; |
| 490 |
} |
| 491 |
|
| 492 |
NetworkBroadcast(&packet, numcommands); |
| 493 |
} |
| 494 |
|
| 495 |
//---------------------------------------------------------------------------- |
| 496 |
// API init.. |
| 497 |
//---------------------------------------------------------------------------- |
| 498 |
|
| 499 |
/** |
| 500 |
** Initialize network part 1. |
| 501 |
*/ |
| 502 |
void InitNetwork1(void) |
| 503 |
{ |
| 504 |
int i; |
| 505 |
int port; |
| 506 |
|
| 507 |
NetworkFildes = (Socket)-1; |
| 508 |
NetworkInSync = 1; |
| 509 |
NetworkNumInterfaces = 0; |
| 510 |
|
| 511 |
NetInit(); // machine dependent setup |
| 512 |
|
| 513 |
for (i = 0; i < PlayerMax; ++i) { |
| 514 |
NetMsgBufLen[i] = 0; |
| 515 |
} |
| 516 |
|
| 517 |
if (NetworkUpdates <= 0) { |
| 518 |
NetworkUpdates = 1; |
| 519 |
} |
| 520 |
// Lag must be multiple of updates |
| 521 |
NetworkLag = (NetworkLag / NetworkUpdates) * NetworkUpdates; |
| 522 |
|
| 523 |
// Our communication port |
| 524 |
port = NetworkPort; |
| 525 |
for (i = 0; i < 10; ++i) { |
| 526 |
NetworkFildes = NetOpenUDP(port + i); |
| 527 |
if (NetworkFildes != (Socket)-1) { |
| 528 |
break; |
| 529 |
} |
| 530 |
} |
| 531 |
if (i == 10) { |
| 532 |
fprintf(stderr, "NETWORK: No free ports %d-%d available, aborting\n", |
| 533 |
port, port + i); |
| 534 |
NetExit(); // machine dependent network exit |
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
#if 1 |
| 539 |
// FIXME: need a working interface check |
| 540 |
NetworkNumInterfaces = 1; |
| 541 |
#else |
| 542 |
NetworkNumInterfaces = NetSocketAddr(NetworkFildes); |
| 543 |
if (NetworkNumInterfaces) { |
| 544 |
DebugPrint("Num IP: %d\n" _C_ NetworkNumInterfaces); |
| 545 |
for (i = 0; i < NetworkNumInterfaces; ++i) { |
| 546 |
DebugPrint("IP: %d.%d.%d.%d\n" _C_ NIPQUAD(ntohl(NetLocalAddrs[i]))); |
| 547 |
} |
| 548 |
} else { |
| 549 |
fprintf(stderr, "NETWORK: Not connected to any external IPV4-network, aborting\n"); |
| 550 |
ExitNetwork1(); |
| 551 |
return; |
| 552 |
} |
| 553 |
#endif |
| 554 |
|
| 555 |
#ifdef DEBUG |
| 556 |
{ |
| 557 |
char buf[128]; |
| 558 |
|
| 559 |
gethostname(buf, sizeof(buf)); |
| 560 |
DebugPrint("%s\n" _C_ buf); |
| 561 |
MyHost = NetResolveHost(buf); |
| 562 |
MyPort = NetLastPort; |
| 563 |
DebugPrint("My host:port %d.%d.%d.%d:%d\n" _C_ |
| 564 |
NIPQUAD(ntohl(MyHost)) _C_ ntohs(MyPort)); |
| 565 |
} |
| 566 |
#endif |
| 567 |
|
| 568 |
CommandsIn.clear(); |
| 569 |
MsgCommandsIn.clear(); |
| 570 |
|
| 571 |
NumNCQs = 0; |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
** Cleanup network part 1. (to be called _AFTER_ part 2 :) |
| 576 |
*/ |
| 577 |
void ExitNetwork1(void) |
| 578 |
{ |
| 579 |
if (!IsNetworkGame()) { // No network running |
| 580 |
return; |
| 581 |
} |
| 582 |
|
| 583 |
#ifdef DEBUG |
| 584 |
DebugPrint("Received: %d packets, %d early, %d late, %d dups, %d lost.\n" _C_ |
| 585 |
NetworkReceivedPackets _C_ NetworkReceivedEarly _C_ NetworkReceivedLate _C_ |
| 586 |
NetworkReceivedDups _C_ NetworkReceivedLost); |
| 587 |
DebugPrint("Send: %d packets, %d resend\n" _C_ |
| 588 |
NetworkSendPackets _C_ NetworkSendResend); |
| 589 |
#endif |
| 590 |
|
| 591 |
NetCloseUDP(NetworkFildes); |
| 592 |
NetExit(); // machine dependent setup |
| 593 |
|
| 594 |
NetworkFildes = (Socket)-1; |
| 595 |
NetworkInSync = 1; |
| 596 |
NetPlayers = 0; |
| 597 |
HostsCount = 0; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
** Initialize network part 2. |
| 602 |
*/ |
| 603 |
void InitNetwork2(void) |
| 604 |
{ |
| 605 |
NetworkConnectSetupGame(); |
| 606 |
|
| 607 |
DebugPrint("Lag %d, Updates %d, Hosts %d\n" _C_ |
| 608 |
NetworkLag _C_ NetworkUpdates _C_ HostsCount); |
| 609 |
|
| 610 |
// |
| 611 |
// Prepare first time without syncs. |
| 612 |
// |
| 613 |
memset(NetworkIn, 0, sizeof(NetworkIn)); |
| 614 |
for (int i = 0; i <= NetworkLag; i += NetworkUpdates) { |
| 615 |
for (int n = 0; n < HostsCount; ++n) { |
| 616 |
for (int c = 0; c < MaxNetworkCommands; ++c) { |
| 617 |
NetworkIn[i][Hosts[n].PlyNr][c].Time = i; |
| 618 |
NetworkIn[i][Hosts[n].PlyNr][c].Type = MessageSync; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
memset(NetworkSyncSeeds, 0, sizeof(NetworkSyncSeeds)); |
| 624 |
memset(NetworkSyncHashs, 0, sizeof(NetworkSyncHashs)); |
| 625 |
memset(PlayerQuit, 0, sizeof(PlayerQuit)); |
| 626 |
memset(NetworkStatus, 0, sizeof(NetworkStatus)); |
| 627 |
memset(NetworkLastFrame, 0, sizeof(NetworkLastFrame)); |
| 628 |
} |
| 629 |
|
| 630 |
//---------------------------------------------------------------------------- |
| 631 |
// Memory management for CNetworkCommandQueues |
| 632 |
//---------------------------------------------------------------------------- |
| 633 |
|
| 634 |
/** |
| 635 |
** Allocate a CNetworkCommandQueue |
| 636 |
** |
| 637 |
** @return CNetworkCommandQueue |
| 638 |
*/ |
| 639 |
static CNetworkCommandQueue *AllocNCQ(void) |
| 640 |
{ |
| 641 |
Assert(NumNCQs != MAX_NCQS); |
| 642 |
CNetworkCommandQueue *ncq = &NCQs[NumNCQs++]; |
| 643 |
ncq->Clear(); |
| 644 |
return ncq; |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
** Free a CNetworkCommandQueue |
| 649 |
** |
| 650 |
** @param ncq CNetworkCommandQueue to free |
| 651 |
*/ |
| 652 |
static void FreeNCQ(CNetworkCommandQueue *ncq) |
| 653 |
{ |
| 654 |
NCQs[ncq - NCQs] = NCQs[--NumNCQs]; |
| 655 |
} |
| 656 |
|
| 657 |
//---------------------------------------------------------------------------- |
| 658 |
// Commands input |
| 659 |
//---------------------------------------------------------------------------- |
| 660 |
|
| 661 |
/** |
| 662 |
** Prepare send of command message. |
| 663 |
** |
| 664 |
** Convert arguments into network format and place it into output queue. |
| 665 |
** |
| 666 |
** @param command Command (Move,Attack,...). |
| 667 |
** @param unit Unit that receive the command. |
| 668 |
** @param x optional X map position. |
| 669 |
** @param y optional y map position. |
| 670 |
** @param dest optional destination unit. |
| 671 |
** @param type optional unit-type argument. |
| 672 |
** @param status Append command or flush old commands. |
| 673 |
** |
| 674 |
** @warning Destination and unit-type shares the same network slot. |
| 675 |
*/ |
| 676 |
void NetworkSendCommand(int command, const CUnit *unit, int x, int y, |
| 677 |
const CUnit *dest, const CUnitType *type, int status) |
| 678 |
{ |
| 679 |
CNetworkCommandQueue *ncq; |
| 680 |
std::list<CNetworkCommandQueue *>::iterator it; |
| 681 |
|
| 682 |
// Check for duplicate command in queue |
| 683 |
for (it = CommandsIn.begin(); it != CommandsIn.end(); ++it) { |
| 684 |
ncq = *it; |
| 685 |
if ((ncq->Type & 0x7F) == command && |
| 686 |
ncq->Data.Unit == htons(unit->Slot) && |
| 687 |
ncq->Data.X == htons(x) && |
| 688 |
ncq->Data.Y == htons(y)) { |
| 689 |
if (dest && ncq->Data.Dest == htons(dest->Slot)) { |
| 690 |
return; |
| 691 |
} else if (type && ncq->Data.Dest == htons(type->Slot)) { |
| 692 |
return; |
| 693 |
} else if (ncq->Data.Dest == 0xFFFF) { |
| 694 |
return; |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
ncq = AllocNCQ(); |
| 700 |
CommandsIn.push_back(ncq); |
| 701 |
|
| 702 |
ncq->Time = GameCycle; |
| 703 |
ncq->Type = command; |
| 704 |
if (status) { |
| 705 |
ncq->Type |= 0x80; |
| 706 |
} |
| 707 |
ncq->Data.Unit = htons(unit->Slot); |
| 708 |
ncq->Data.X = htons(x); |
| 709 |
ncq->Data.Y = htons(y); |
| 710 |
Assert (!dest || !type); // Both together isn't allowed |
| 711 |
if (dest) { |
| 712 |
ncq->Data.Dest = htons(dest->Slot); |
| 713 |
} else if (type) { |
| 714 |
ncq->Data.Dest = htons(type->Slot); |
| 715 |
} else { |
| 716 |
ncq->Data.Dest = htons(0xFFFF); // -1 |
| 717 |
} |
| 718 |
|
| 719 |
} |
| 720 |
|
| 721 |
/** |
| 722 |
** Prepare send of extended command message. |
| 723 |
** |
| 724 |
** Convert arguments into network format and place it into output queue. |
| 725 |
** |
| 726 |
** @param command Command (Move,Attack,...). |
| 727 |
** @param arg1 optional argument #1 |
| 728 |
** @param arg2 optional argument #2 |
| 729 |
** @param arg3 optional argument #3 |
| 730 |
** @param arg4 optional argument #4 |
| 731 |
** @param status Append command or flush old commands. |
| 732 |
*/ |
| 733 |
void NetworkSendExtendedCommand(int command, int arg1, int arg2, int arg3, |
| 734 |
int arg4, int status) |
| 735 |
{ |
| 736 |
CNetworkCommandQueue *ncq; |
| 737 |
CNetworkExtendedCommand *nec; |
| 738 |
|
| 739 |
ncq = AllocNCQ(); |
| 740 |
CommandsIn.push_back(ncq); |
| 741 |
|
| 742 |
ncq->Time = GameCycle; |
| 743 |
nec = (CNetworkExtendedCommand *)&ncq->Data; |
| 744 |
|
| 745 |
ncq->Type = MessageExtendedCommand; |
| 746 |
if (status) { |
| 747 |
ncq->Type |= 0x80; |
| 748 |
} |
| 749 |
nec->ExtendedType = command; |
| 750 |
nec->Arg1 = arg1; |
| 751 |
nec->Arg2 = htons(arg2); |
| 752 |
nec->Arg3 = htons(arg3); |
| 753 |
nec->Arg4 = htons(arg4); |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
** Sends my selections to teammates |
| 758 |
** |
| 759 |
** @param units Units to send |
| 760 |
** @param count Number of units to send |
| 761 |
*/ |
| 762 |
void NetworkSendSelection(CUnit **units, int count) |
| 763 |
{ |
| 764 |
CNetworkPacket packet; |
| 765 |
NetworkSelectionHeader *header; |
| 766 |
CNetworkSelection *selection; |
| 767 |
int unitcount; |
| 768 |
int ref; |
| 769 |
int i; |
| 770 |
int teammates[PlayerMax]; |
| 771 |
int numteammates; |
| 772 |
int nosent; |
| 773 |
|
| 774 |
// Check if we have any teammates to send to |
| 775 |
numteammates = 0; |
| 776 |
for (i = 0; i < HostsCount; ++i) { |
| 777 |
if (Players[Hosts[i].PlyNr].Team == ThisPlayer->Team) { |
| 778 |
teammates[numteammates++] = i; |
| 779 |
} |
| 780 |
} |
| 781 |
if (!numteammates) { |
| 782 |
return; |
| 783 |
} |
| 784 |
|
| 785 |
// |
| 786 |
// Build and send packets to cover all units. |
| 787 |
// |
| 788 |
unitcount = 0; |
| 789 |
while (unitcount < count) { |
| 790 |
header = (NetworkSelectionHeader *)&(packet.Header); |
| 791 |
if (unitcount == 0) { |
| 792 |
header->Add = 0; |
| 793 |
} else { |
| 794 |
header->Add = 1; |
| 795 |
} |
| 796 |
header->Remove = 0; |
| 797 |
|
| 798 |
nosent = 0; |
| 799 |
for (i = 0; i < MaxNetworkCommands && unitcount < count; ++i) { |
| 800 |
header->Type[i] = MessageSelection; |
| 801 |
selection = (CNetworkSelection *)&packet.Command[i]; |
| 802 |
for (ref = 0; ref < 4 && unitcount < count; ++ref, ++unitcount) { |
| 803 |
selection->Unit[ref] = htons(UnitNumber(units[unitcount])); |
| 804 |
++nosent; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
if (unitcount >= count) { |
| 809 |
// This is the last command |
| 810 |
header->NumberSent = nosent; |
| 811 |
} else { |
| 812 |
header->NumberSent = MaxNetworkCommands * 4; |
| 813 |
} |
| 814 |
|
| 815 |
for (; i < MaxNetworkCommands; ++i) { |
| 816 |
packet.Header.Type[i] = MessageNone; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
// |
| 821 |
// Send the Constructed packet to team members |
| 822 |
// |
| 823 |
int numcommands = (nosent + 3) / 4; |
| 824 |
unsigned char *buf = packet.Serialize(numcommands); |
| 825 |
|
| 826 |
for (i = 0; i < numteammates; ++i) { |
| 827 |
ref = NetSendUDP(NetworkFildes, Hosts[teammates[i]].Host, Hosts[teammates[i]].Port, |
| 828 |
buf, CNetworkPacketHeader::Size() + CNetworkSelection::Size() * numcommands); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
} |
| 833 |
/** |
| 834 |
** Process Received Unit Selection |
| 835 |
** |
| 836 |
** @param packet Network Packet to Process |
| 837 |
** @param player Player number |
| 838 |
*/ |
| 839 |
static void NetworkProcessSelection(CNetworkPacket *packet, int player) |
| 840 |
{ |
| 841 |
CUnit *units[UnitMax]; |
| 842 |
NetworkSelectionHeader *header; |
| 843 |
CNetworkSelection *selection; |
| 844 |
int adjust; |
| 845 |
int count; |
| 846 |
int unitcount; |
| 847 |
|
| 848 |
header = (NetworkSelectionHeader *)&(packet->Header); |
| 849 |
// |
| 850 |
// Create Unit Array |
| 851 |
// |
| 852 |
count = header->NumberSent; |
| 853 |
adjust = (header->Add << 1) | header->Remove; |
| 854 |
unitcount = 0; |
| 855 |
|
| 856 |
for (int i = 0; header->Type[i] == MessageSelection; ++i) { |
| 857 |
selection = (CNetworkSelection *)&(packet->Command[i]); |
| 858 |
for (int j = 0; j < 4 && unitcount < count; ++j) { |
| 859 |
units[unitcount++] = Units[ntohs(selection->Unit[j])]; |
| 860 |
} |
| 861 |
} |
| 862 |
Assert(count == unitcount); |
| 863 |
|
| 864 |
ChangeTeamSelectedUnits(&Players[player], units, adjust, count); |
| 865 |
} |
| 866 |
|
| 867 |
/** |
| 868 |
** Remove a player from the game. |
| 869 |
** |
| 870 |
** @param player Player number |
| 871 |
*/ |
| 872 |
static void NetworkRemovePlayer(int player) |
| 873 |
{ |
| 874 |
int i; |
| 875 |
|
| 876 |
// Remove player from Hosts and clear NetworkIn |
| 877 |
for (i = 0; i < HostsCount; ++i) { |
| 878 |
if (Hosts[i].PlyNr == player) { |
| 879 |
Hosts[i] = Hosts[HostsCount - 1]; |
| 880 |
--HostsCount; |
| 881 |
break; |
| 882 |
} |
| 883 |
} |
| 884 |
for (i = 0; i < 256; ++i) { |
| 885 |
for (int c = 0; c < MaxNetworkCommands; ++c) { |
| 886 |
NetworkIn[i][player][c].Time = 0; |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
** Called if message for the network is ready. |
| 893 |
** (by WaitEventsOneFrame) |
| 894 |
** |
| 895 |
** @todo |
| 896 |
** NetworkReceivedEarly NetworkReceivedLate NetworkReceivedDups |
| 897 |
** Must be calculated. |
| 898 |
*/ |
| 899 |
void NetworkEvent(void) |
| 900 |
{ |
| 901 |
unsigned char buf[1024]; |
| 902 |
CNetworkPacket packet; |
| 903 |
int player; |
| 904 |
int i; |
| 905 |
int commands; |
| 906 |
unsigned long n; |
| 907 |
|
| 908 |
if (!IsNetworkGame()) { |
| 909 |
NetworkInSync = 1; |
| 910 |
return; |
| 911 |
} |
| 912 |
// |
| 913 |
// Read the packet. |
| 914 |
// |
| 915 |
if ((i = NetRecvUDP(NetworkFildes, &buf, sizeof(buf))) < 0) { |
| 916 |
// |
| 917 |
// Server or client gone? |
| 918 |
// |
| 919 |
DebugPrint("Server/Client gone?\n"); |
| 920 |
// just hope for an automatic recover right now.. |
| 921 |
NetworkInSync = 0; |
| 922 |
return; |
| 923 |
} |
| 924 |
|
| 925 |
#ifdef DEBUG |
| 926 |
++NetworkReceivedPackets; |
| 927 |
#endif |
| 928 |
|
| 929 |
// |
| 930 |
// Setup messages |
| 931 |
// |
| 932 |
if (NetConnectRunning) { |
| 933 |
if (NetworkParseSetupEvent(buf, i)) { |
| 934 |
return; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
commands = packet.Deserialize(buf, i); |
| 939 |
if (commands < 0) { |
| 940 |
DebugPrint("Bad packet read\n"); |
| 941 |
return; |
| 942 |
} |
| 943 |
|
| 944 |
for (i = 0; i < HostsCount; ++i) { |
| 945 |
if (Hosts[i].Host == NetLastHost && Hosts[i].Port == NetLastPort && |
| 946 |
!PlayerQuit[Hosts[i].PlyNr]) { |
| 947 |
break; |
| 948 |
} |
| 949 |
} |
| 950 |
if (i == HostsCount) { |
| 951 |
DebugPrint("Not a host in play: %d.%d.%d.%d:%d\n" _C_ |
| 952 |
NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 953 |
return; |
| 954 |
} |
| 955 |
player = Hosts[i].PlyNr; |
| 956 |
|
| 957 |
// In a normal packet there is a least sync, selection may not have that |
| 958 |
if (packet.Header.Type[0] == MessageSelection || commands == 0) { |
| 959 |
NetworkProcessSelection(&packet, player); |
| 960 |
return; |
| 961 |
} |
| 962 |
|
| 963 |
// |
| 964 |
// Parse the packet commands. |
| 965 |
// |
| 966 |
for (i = 0; i < commands; ++i) { |
| 967 |
const CNetworkCommand *nc = &packet.Command[i]; |
| 968 |
bool validCommand = false; |
| 969 |
|
| 970 |
// |
| 971 |
// Handle some messages. |
| 972 |
// |
| 973 |
if (packet.Header.Type[i] == MessageQuit) { |
| 974 |
int playerNum = ntohs(nc->X); |
| 975 |
|
| 976 |
if (playerNum >= 0 && playerNum < NumPlayers) { |
| 977 |
PlayerQuit[playerNum] = 1; |
| 978 |
validCommand = true; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
if (packet.Header.Type[i] == MessageResend) { |
| 983 |
// Destination cycle (time to execute). |
| 984 |
n = ((GameCycle + 128) & ~0xFF) | packet.Header.Cycle; |
| 985 |
if (n > GameCycle + 128) { |
| 986 |
n -= 0x100; |
| 987 |
} |
| 988 |
|
| 989 |
// FIXME: not necessary to send this packet multiple times!!!! |
| 990 |
// other side sends re-send until it gets an answer. |
| 991 |
|
| 992 |
if (n != NetworkIn[n & 0xFF][ThisPlayer->Index][0].Time) { |
| 993 |
// Asking for a cycle we haven't gotten to yet, ignore for now |
| 994 |
return; |
| 995 |
} |
| 996 |
|
| 997 |
NetworkSendPacket(NetworkIn[n & 0xFF][ThisPlayer->Index]); |
| 998 |
|
| 999 |
// Check if a player quit this cycle |
| 1000 |
for (int j = 0; j < HostsCount; ++j) { |
| 1001 |
for (int c = 0; c < MaxNetworkCommands; ++c) { |
| 1002 |
CNetworkCommandQueue *ncq; |
| 1003 |
ncq = &NetworkIn[n & 0xFF][Hosts[j].PlyNr][c]; |
| 1004 |
if (ncq->Time && ncq->Type == MessageQuit) { |
| 1005 |
CNetworkPacket np; |
| 1006 |
np.Header.Cycle = ncq->Time & 0xFF; |
| 1007 |
np.Header.Type[0] = ncq->Type; |
| 1008 |
np.Command[0] = ncq->Data; |
| 1009 |
for (int k = 1; k < MaxNetworkCommands; ++k) { |
| 1010 |
np.Header.Type[k] = MessageNone; |
| 1011 |
} |
| 1012 |
|
| 1013 |
NetworkBroadcast(&np, 1); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
return; |
| 1019 |
} |
| 1020 |
|
| 1021 |
// Destination cycle (time to execute). |
| 1022 |
n = ((GameCycle + 128) & ~0xFF) | packet.Header.Cycle; |
| 1023 |
if (n > GameCycle + 128) { |
| 1024 |
n -= 0x100; |
| 1025 |
} |
| 1026 |
|
| 1027 |
// Receive statistic |
| 1028 |
if (n > NetworkStatus[player]) { |
| 1029 |
NetworkStatus[player] = n; |
| 1030 |
} |
| 1031 |
NetworkLastFrame[player] = FrameCounter; |
| 1032 |
|
| 1033 |
// Place in network in |
| 1034 |
switch (packet.Header.Type[i] & 0x7F) { |
| 1035 |
case MessageExtendedCommand: |
| 1036 |
// FIXME: ensure the sender is part of the command |
| 1037 |
validCommand = true; |
| 1038 |
break; |
| 1039 |
case MessageSync: |
| 1040 |
// Sync does not matter |
| 1041 |
validCommand = true; |
| 1042 |
break; |
| 1043 |
case MessageQuit: |
| 1044 |
case MessageQuitAck: |
| 1045 |
case MessageResend: |
| 1046 |
case MessageChat: |
| 1047 |
case MessageChatTerm: |
| 1048 |
// FIXME: ensure it's from the right player |
| 1049 |
validCommand = true; |
| 1050 |
break; |
| 1051 |
case MessageCommandDismiss: |
| 1052 |
// Fall through! |
| 1053 |
default: |
| 1054 |
{ |
| 1055 |
unsigned int slot = ntohs(nc->Unit); |
| 1056 |
|
| 1057 |
if (slot < UnitSlotFree && |
| 1058 |
(UnitSlots[slot]->Player->Index == player || |
| 1059 |
Players[player].IsTeamed(UnitSlots[slot]))) { |
| 1060 |
validCommand = true; |
| 1061 |
} else { |
| 1062 |
validCommand = false; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
// FIXME: not all values in nc have been validated |
| 1068 |
if (validCommand) { |
| 1069 |
NetworkIn[packet.Header.Cycle][player][i].Time = n; |
| 1070 |
NetworkIn[packet.Header.Cycle][player][i].Type = packet.Header.Type[i]; |
| 1071 |
NetworkIn[packet.Header.Cycle][player][i].Data = *nc; |
| 1072 |
} else { |
| 1073 |
SetMessage(_("%s sent bad command"), Players[player].Name.c_str()); |
| 1074 |
DebugPrint("%s sent bad command: 0x%x\n" _C_ |
| 1075 |
Players[player].Name.c_str() _C_ |
| 1076 |
packet.Header.Type[i] & 0x7F); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
for ( ; i < MaxNetworkCommands; ++i) { |
| 1081 |
NetworkIn[packet.Header.Cycle][player][i].Time = 0; |
| 1082 |
} |
| 1083 |
|
| 1084 |
// |
| 1085 |
// Waiting for this time slot |
| 1086 |
// |
| 1087 |
if (!NetworkInSync) { |
| 1088 |
NetworkInSync = 1; |
| 1089 |
n = (GameCycle / NetworkUpdates) * NetworkUpdates + NetworkUpdates; |
| 1090 |
for (player = 0; player < HostsCount; ++player) { |
| 1091 |
if (NetworkIn[n & 0xFF][Hosts[player].PlyNr][0].Time != n) { |
| 1092 |
NetworkInSync = 0; |
| 1093 |
break; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
/** |
| 1100 |
** Quit the game. |
| 1101 |
*/ |
| 1102 |
void NetworkQuit(void) |
| 1103 |
{ |
| 1104 |
if (!ThisPlayer) { |
| 1105 |
return; |
| 1106 |
} |
| 1107 |
|
| 1108 |
int n = (GameCycle + NetworkUpdates) / NetworkUpdates * NetworkUpdates + NetworkLag; |
| 1109 |
NetworkIn[n & 0xFF][ThisPlayer->Index][0].Type = MessageQuit; |
| 1110 |
NetworkIn[n & 0xFF][ThisPlayer->Index][0].Time = n; |
| 1111 |
NetworkIn[n & 0xFF][ThisPlayer->Index][0].Data.X = ThisPlayer->Index; |
| 1112 |
|
| 1113 |
for (int i = 1; i < MaxNetworkCommands; ++i) { |
| 1114 |
NetworkIn[n & 0xFF][ThisPlayer->Index][i].Type = MessageNone; |
| 1115 |
} |
| 1116 |
|
| 1117 |
NetworkSendPacket(NetworkIn[n & 0xFF][ThisPlayer->Index]); |
| 1118 |
} |
| 1119 |
|
| 1120 |
/** |
| 1121 |
** Send chat message. (Message is sent with low priority) |
| 1122 |
** |
| 1123 |
** @param msg Text message to send. |
| 1124 |
*/ |
| 1125 |
void NetworkChatMessage(const std::string &msg) |
| 1126 |
{ |
| 1127 |
CNetworkCommandQueue *ncq; |
| 1128 |
CNetworkChat *ncm; |
| 1129 |
const char *cp; |
| 1130 |
int n; |
| 1131 |
|
| 1132 |
if (IsNetworkGame()) { |
| 1133 |
cp = msg.c_str(); |
| 1134 |
n = msg.size(); |
| 1135 |
while (n >= (int)sizeof(ncm->Text)) { |
| 1136 |
ncq = AllocNCQ(); |
| 1137 |
MsgCommandsIn.push_back(ncq); |
| 1138 |
ncq->Type = MessageChat; |
| 1139 |
ncm = (CNetworkChat *)(&ncq->Data); |
| 1140 |
ncm->Player = ThisPlayer->Index; |
| 1141 |
memcpy(ncm->Text, cp, sizeof(ncm->Text)); |
| 1142 |
cp += sizeof(ncm->Text); |
| 1143 |
n -= sizeof(ncm->Text); |
| 1144 |
} |
| 1145 |
ncq = AllocNCQ(); |
| 1146 |
MsgCommandsIn.push_back(ncq); |
| 1147 |
ncq->Type = MessageChatTerm; |
| 1148 |
ncm = (CNetworkChat *)(&ncq->Data); |
| 1149 |
ncm->Player = ThisPlayer->Index; |
| 1150 |
memcpy(ncm->Text, cp, n + 1); // see >= above :) |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
/** |
| 1155 |
** Parse a network command. |
| 1156 |
** |
| 1157 |
** @param ncq Network command from queue |
| 1158 |
*/ |
| 1159 |
static void ParseNetworkCommand(const CNetworkCommandQueue *ncq) |
| 1160 |
{ |
| 1161 |
int ply; |
| 1162 |
|
| 1163 |
switch (ncq->Type & 0x7F) { |
| 1164 |
case MessageSync: |
| 1165 |
ply = ntohs(ncq->Data.X) << 16; |
| 1166 |
ply |= ntohs(ncq->Data.Y); |
| 1167 |
if (ply != NetworkSyncSeeds[GameCycle & 0xFF] || |
| 1168 |
ntohs(ncq->Data.Unit) != NetworkSyncHashs[GameCycle & 0xFF]) { |
| 1169 |
|
| 1170 |
SetMessage("%s", _("Network out of sync")); |
| 1171 |
DebugPrint("\nNetwork out of sync %x!=%x! %d!=%d! Cycle %lu\n\n" _C_ |
| 1172 |
ply _C_ NetworkSyncSeeds[GameCycle & 0xFF] _C_ |
| 1173 |
ntohs(ncq->Data.Unit) _C_ NetworkSyncHashs[GameCycle & 0xFF] _C_ GameCycle); |
| 1174 |
} |
| 1175 |
return; |
| 1176 |
case MessageChat: |
| 1177 |
case MessageChatTerm: { |
| 1178 |
const CNetworkChat *ncm; |
| 1179 |
|
| 1180 |
ncm = (CNetworkChat *)(&ncq->Data); |
| 1181 |
ply = ncm->Player; |
| 1182 |
if (NetMsgBufLen[ply] + sizeof(ncm->Text) < 128) { |
| 1183 |
memcpy(((char *)NetMsgBuf[ply]) + NetMsgBufLen[ply], ncm->Text, |
| 1184 |
sizeof(ncm->Text)); |
| 1185 |
} |
| 1186 |
NetMsgBufLen[ply] += sizeof(ncm->Text); |
| 1187 |
if (ncq->Type == MessageChatTerm) { |
| 1188 |
NetMsgBuf[ply][127] = '\0'; |
| 1189 |
SetMessage("%s", NetMsgBuf[ply]); |
| 1190 |
PlayGameSound(GameSounds.ChatMessage.Sound, MaxSampleVolume); |
| 1191 |
NetMsgBufLen[ply] = 0; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
break; |
| 1195 |
case MessageQuit: |
| 1196 |
NetworkRemovePlayer(ncq->Data.X); |
| 1197 |
CommandLog("quit", NoUnitP, FlushCommands, ncq->Data.X, -1, NoUnitP, NULL, -1); |
| 1198 |
CommandQuit(ncq->Data.X); |
| 1199 |
break; |
| 1200 |
case MessageExtendedCommand: { |
| 1201 |
const CNetworkExtendedCommand *nec; |
| 1202 |
|
| 1203 |
nec = (CNetworkExtendedCommand *)(&ncq->Data); |
| 1204 |
ParseExtendedCommand(nec->ExtendedType, (ncq->Type & 0x80) >> 7, |
| 1205 |
nec->Arg1, ntohs(nec->Arg2), ntohs(nec->Arg3), ntohs(nec->Arg4)); |
| 1206 |
} |
| 1207 |
break; |
| 1208 |
case MessageNone: |
| 1209 |
// Nothing to Do, This Message Should Never be Executed |
| 1210 |
Assert(0); |
| 1211 |
break; |
| 1212 |
default: |
| 1213 |
ParseCommand(ncq->Type, ntohs(ncq->Data.Unit), |
| 1214 |
ntohs(ncq->Data.X), ntohs(ncq->Data.Y), ntohs(ncq->Data.Dest)); |
| 1215 |
break; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
/** |
| 1220 |
** Network resend commands, we have a missing packet send to all clients |
| 1221 |
** what packet we are missing. |
| 1222 |
** |
| 1223 |
** @todo |
| 1224 |
** We need only send to the clients, that have not delivered the packet. |
| 1225 |
*/ |
| 1226 |
static void NetworkResendCommands(void) |
| 1227 |
{ |
| 1228 |
CNetworkPacket packet; |
| 1229 |
|
| 1230 |
#ifdef DEBUG |
| 1231 |
++NetworkSendResend; |
| 1232 |
#endif |
| 1233 |
|
| 1234 |
// |
| 1235 |
// Build packet |
| 1236 |
// |
| 1237 |
packet.Header.Type[0] = MessageResend; |
| 1238 |
packet.Header.Type[1] = MessageNone; |
| 1239 |
packet.Header.Cycle = |
| 1240 |
(Uint8)((GameCycle / NetworkUpdates) * NetworkUpdates + NetworkUpdates); |
| 1241 |
|
| 1242 |
NetworkBroadcast(&packet, 1); |
| 1243 |
} |
| 1244 |
|
| 1245 |
/** |
| 1246 |
** Network send commands. |
| 1247 |
*/ |
| 1248 |
static void NetworkSendCommands(void) |
| 1249 |
{ |
| 1250 |
CNetworkCommandQueue *incommand; |
| 1251 |
CNetworkCommandQueue *ncq; |
| 1252 |
int numcommands; |
| 1253 |
|
| 1254 |
// |
| 1255 |
// No command available, send sync. |
| 1256 |
// |
| 1257 |
numcommands = 0; |
| 1258 |
incommand = NULL; |
| 1259 |
ncq = NetworkIn[(GameCycle + NetworkLag) & 0xFF][ThisPlayer->Index]; |
| 1260 |
ncq->Clear(); |
| 1261 |
if (CommandsIn.empty() && MsgCommandsIn.empty()) { |
| 1262 |
ncq[0].Type = MessageSync; |
| 1263 |
ncq[0].Data.Unit = htons(SyncHash&0xFFFF); |
| 1264 |
ncq[0].Data.X = htons(SyncRandSeed>>16); |
| 1265 |
ncq[0].Data.Y = htons(SyncRandSeed&0xFFFF); |
| 1266 |
ncq[0].Time = GameCycle + NetworkLag; |
| 1267 |
numcommands = 1; |
| 1268 |
} else { |
| 1269 |
while ((!CommandsIn.empty() || !MsgCommandsIn.empty()) && |
| 1270 |
numcommands < MaxNetworkCommands) { |
| 1271 |
if (!CommandsIn.empty()) { |
| 1272 |
incommand = CommandsIn.front(); |
| 1273 |
#ifdef DEBUG |
| 1274 |
if (incommand->Type != MessageExtendedCommand) { |
| 1275 |
// FIXME: we can send destoyed units over network :( |
| 1276 |
if (UnitSlots[ntohs(ncq->Data.Unit)]->Destroyed) { |
| 1277 |
DebugPrint("Sending destroyed unit %d over network!!!!!!\n" _C_ |
| 1278 |
ntohs(incommand->Data.Unit)); |
| 1279 |
} |
| 1280 |
} |
| 1281 |
#endif |
| 1282 |
CommandsIn.pop_front(); |
| 1283 |
} else { |
| 1284 |
incommand = MsgCommandsIn.front(); |
| 1285 |
MsgCommandsIn.pop_front(); |
| 1286 |
} |
| 1287 |
ncq[numcommands] = *incommand; |
| 1288 |
ncq[numcommands].Time = GameCycle + NetworkLag; |
| 1289 |
++numcommands; |
| 1290 |
FreeNCQ(incommand); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (numcommands != MaxNetworkCommands) { |
| 1295 |
ncq[numcommands].Type = MessageNone; |
| 1296 |
} |
| 1297 |
|
| 1298 |
NetworkSendPacket(ncq); |
| 1299 |
|
| 1300 |
NetworkSyncSeeds[(GameCycle + NetworkLag) & 0xFF] = SyncRandSeed; |
| 1301 |
NetworkSyncHashs[(GameCycle + NetworkLag) & 0xFF] = SyncHash & 0xFFFF; // FIXME: 32bit later |
| 1302 |
} |
| 1303 |
|
| 1304 |
/** |
| 1305 |
** Network excecute commands. |
| 1306 |
*/ |
| 1307 |
static void NetworkExecCommands(void) |
| 1308 |
{ |
| 1309 |
CNetworkCommandQueue *ncq; |
| 1310 |
|
| 1311 |
// |
| 1312 |
// Must execute commands on all computers in the same order. |
| 1313 |
// |
| 1314 |
for (int i = 0; i < NumPlayers; ++i) { |
| 1315 |
// |
| 1316 |
// Remove commands. |
| 1317 |
// |
| 1318 |
for (int c = 0; c < MaxNetworkCommands; ++c) { |
| 1319 |
ncq = &NetworkIn[GameCycle & 0xFF][i][c]; |
| 1320 |
if (ncq->Type == MessageNone) { |
| 1321 |
break; |
| 1322 |
} |
| 1323 |
if (ncq->Time) { |
| 1324 |
#ifdef DEBUG |
| 1325 |
if (ncq->Time != GameCycle) { |
| 1326 |
DebugPrint("cycle %lu idx %lu time %lu\n" _C_ |
| 1327 |
GameCycle _C_ GameCycle & 0xFF _C_ ncq->Time); |
| 1328 |
Assert(ncq->Time == GameCycle); |
| 1329 |
} |
| 1330 |
#endif |
| 1331 |
ParseNetworkCommand(ncq); |
| 1332 |
} |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
/** |
| 1338 |
** Network synchronize commands. |
| 1339 |
*/ |
| 1340 |
static void NetworkSyncCommands(void) |
| 1341 |
{ |
| 1342 |
const CNetworkCommandQueue *ncq; |
| 1343 |
unsigned long n; |
| 1344 |
|
| 1345 |
// |
| 1346 |
// Check if all next messages are available. |
| 1347 |
// |
| 1348 |
NetworkInSync = 1; |
| 1349 |
n = GameCycle + NetworkUpdates; |
| 1350 |
for (int i = 0; i < HostsCount; ++i) { |
| 1351 |
ncq = NetworkIn[n & 0xFF][Hosts[i].PlyNr]; |
| 1352 |
if (ncq[0].Time != n) { |
| 1353 |
NetworkInSync = 0; |
| 1354 |
NetworkDelay = FrameCounter + NetworkUpdates; |
| 1355 |
// FIXME: should send a resend request. |
| 1356 |
break; |
| 1357 |
} |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
/** |
| 1362 |
** Handle network commands. |
| 1363 |
*/ |
| 1364 |
void NetworkCommands(void) |
| 1365 |
{ |
| 1366 |
if (IsNetworkGame()) { |
| 1367 |
if (!(GameCycle % NetworkUpdates)) { |
| 1368 |
// Send messages to all clients (other players) |
| 1369 |
NetworkSendCommands(); |
| 1370 |
NetworkExecCommands(); |
| 1371 |
NetworkSyncCommands(); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
|
| 1377 |
/** |
| 1378 |
** Recover network. |
| 1379 |
*/ |
| 1380 |
void NetworkRecover(void) |
| 1381 |
{ |
| 1382 |
if (HostsCount == 0) { |
| 1383 |
NetworkInSync = 1; |
| 1384 |
return; |
| 1385 |
} |
| 1386 |
|
| 1387 |
if (FrameCounter > NetworkDelay) { |
| 1388 |
NetworkDelay += NetworkUpdates; |
| 1389 |
|
| 1390 |
// Check for players that timed out |
| 1391 |
for (int i = 0; i < HostsCount; ++i) { |
| 1392 |
int secs; |
| 1393 |
|
| 1394 |
if (!NetworkLastFrame[Hosts[i].PlyNr]) { |
| 1395 |
continue; |
| 1396 |
} |
| 1397 |
|
| 1398 |
secs = (FrameCounter - NetworkLastFrame[Hosts[i].PlyNr]) / |
| 1399 |
(FRAMES_PER_SECOND * VideoSyncSpeed / 100); |
| 1400 |
// FIXME: display a menu while we wait |
| 1401 |
if (secs >= 3 && secs < NetworkTimeout) { |
| 1402 |
if (FrameCounter % FRAMES_PER_SECOND < (unsigned long)NetworkUpdates) { |
| 1403 |
SetMessage(_("Waiting for player \"%s\": %d:%02d"), Hosts[i].PlyName, |
| 1404 |
(NetworkTimeout - secs) / 60, (NetworkTimeout - secs) % 60); |
| 1405 |
} |
| 1406 |
} |
| 1407 |
if (secs >= NetworkTimeout) { |
| 1408 |
CNetworkCommand nc; |
| 1409 |
const CNetworkCommandQueue *ncq; |
| 1410 |
unsigned long n; |
| 1411 |
CNetworkPacket np; |
| 1412 |
|
| 1413 |
n = GameCycle + NetworkUpdates; |
| 1414 |
nc.X = Hosts[i].PlyNr; |
| 1415 |
NetworkIn[n & 0xFF][Hosts[i].PlyNr][0].Time = n; |
| 1416 |
NetworkIn[n & 0xFF][Hosts[i].PlyNr][0].Type = MessageQuit; |
| 1417 |
NetworkIn[n & 0xFF][Hosts[i].PlyNr][0].Data = nc; |
| 1418 |
PlayerQuit[Hosts[i].PlyNr] = 1; |
| 1419 |
SetMessage("%s", _("Timed out")); |
| 1420 |
|
| 1421 |
ncq = &NetworkIn[n & 0xFF][Hosts[i].PlyNr][0]; |
| 1422 |
np.Header.Cycle = ncq->Time & 0xFF; |
| 1423 |
np.Header.Type[0] = ncq->Type; |
| 1424 |
np.Header.Type[1] = MessageNone; |
| 1425 |
|
| 1426 |
NetworkBroadcast(&np, 1); |
| 1427 |
|
| 1428 |
NetworkSyncCommands(); |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
// Resend old commands |
| 1433 |
NetworkResendCommands(); |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
//@} |