| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name netconnect.cpp - The network high level connection code. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2001-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 |
// Includes |
| 33 |
//---------------------------------------------------------------------------- |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <stddef.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
|
| 42 |
#include "net_lowlevel.h" |
| 43 |
#include "player.h" |
| 44 |
#include "map.h" |
| 45 |
#include "network.h" |
| 46 |
#include "netconnect.h" |
| 47 |
#include "interface.h" |
| 48 |
#include "menus.h" |
| 49 |
#include "settings.h" |
| 50 |
#include "version.h" |
| 51 |
|
| 52 |
|
| 53 |
//---------------------------------------------------------------------------- |
| 54 |
// Declaration |
| 55 |
//---------------------------------------------------------------------------- |
| 56 |
|
| 57 |
// received nothing from client for xx frames? |
| 58 |
#define CLIENT_LIVE_BEAT 60 |
| 59 |
#define CLIENT_IS_DEAD 300 |
| 60 |
|
| 61 |
//---------------------------------------------------------------------------- |
| 62 |
// Variables |
| 63 |
//---------------------------------------------------------------------------- |
| 64 |
|
| 65 |
int NetPlayers; /// How many network players |
| 66 |
int NetworkPort = NetworkDefaultPort; /// Local network port to use |
| 67 |
|
| 68 |
#ifdef DEBUG |
| 69 |
extern unsigned long MyHost; /// My host number. |
| 70 |
extern int MyPort; /// My port number. |
| 71 |
#endif |
| 72 |
|
| 73 |
int HostsCount; /// Number of hosts. |
| 74 |
CNetworkHost Hosts[PlayerMax]; /// Host and ports of all players. |
| 75 |
|
| 76 |
int NetConnectRunning; /// Network menu: Setup mode active |
| 77 |
NetworkState NetStates[PlayerMax]; /// Network menu: Server: Client Host states |
| 78 |
unsigned char NetLocalState; /// Network menu: Local Server/Client connect state; |
| 79 |
int NetLocalHostsSlot; /// Network menu: Slot # in Hosts array of local client |
| 80 |
int NetLocalPlayerNumber; /// Player number of local client |
| 81 |
|
| 82 |
static int NetStateMsgCnt; /// Number of consecutive msgs of same type sent |
| 83 |
static unsigned char LastStateMsgType; /// Subtype of last InitConfig message sent |
| 84 |
static unsigned long NetLastPacketSent; /// Tick the last network packet was sent |
| 85 |
static unsigned long NetworkServerIP; /// Network Client: IP of server to join |
| 86 |
std::string NetworkMapName; /// Name of the map recieved with ICMMap |
| 87 |
|
| 88 |
/// FIXME ARI: The following is a kludge to have some way to override the default port |
| 89 |
/// on the server to connect to. Should be selectable by advanced network menus. |
| 90 |
/// For now just specify with the -P port command line arg... |
| 91 |
static int NetworkServerPort = NetworkDefaultPort; /// Server network port to use |
| 92 |
|
| 93 |
/** |
| 94 |
** Client and server selection state for Multiplayer clients |
| 95 |
*/ |
| 96 |
CServerSetup ServerSetupState; |
| 97 |
CServerSetup LocalSetupState; |
| 98 |
|
| 99 |
|
| 100 |
unsigned char *CNetworkHost::Serialize() const |
| 101 |
{ |
| 102 |
unsigned char *buf = new unsigned char[CNetworkHost::Size()]; |
| 103 |
unsigned char *p = buf; |
| 104 |
|
| 105 |
*(Uint32 *)p = this->Host; |
| 106 |
p += 4; |
| 107 |
*(Uint16 *)p = this->Port; |
| 108 |
p += 2; |
| 109 |
*(Uint16 *)p = this->PlyNr; |
| 110 |
p += 2; |
| 111 |
memcpy(p, this->PlyName, sizeof(this->PlyName)); |
| 112 |
|
| 113 |
return buf; |
| 114 |
} |
| 115 |
|
| 116 |
void CNetworkHost::Deserialize(const unsigned char *p) |
| 117 |
{ |
| 118 |
this->Host = *(Uint32 *)p; |
| 119 |
p += 4; |
| 120 |
this->Port = *(Uint16 *)p; |
| 121 |
p += 2; |
| 122 |
this->PlyNr = *(Uint16 *)p; |
| 123 |
p += 2; |
| 124 |
memcpy(this->PlyName, p, sizeof(this->PlyName)); |
| 125 |
} |
| 126 |
|
| 127 |
unsigned char *CServerSetup::Serialize() const |
| 128 |
{ |
| 129 |
unsigned char *buf = new unsigned char[CServerSetup::Size()]; |
| 130 |
unsigned char *p = buf; |
| 131 |
int i; |
| 132 |
|
| 133 |
*p++ = this->ResourcesOption; |
| 134 |
*p++ = this->UnitsOption; |
| 135 |
*p++ = this->FogOfWar; |
| 136 |
*p++ = this->RevealMap; |
| 137 |
*p++ = this->GameTypeOption; |
| 138 |
*p++ = this->Difficulty; |
| 139 |
*p++ = this->MapRichness; |
| 140 |
for (i = 0; i < PlayerMax; ++i) { |
| 141 |
*p++ = this->CompOpt[i]; |
| 142 |
} |
| 143 |
for (i = 0; i < PlayerMax; ++i) { |
| 144 |
*p++ = this->Ready[i]; |
| 145 |
} |
| 146 |
for (i = 0; i < PlayerMax; ++i) { |
| 147 |
*(Uint32 *)p = this->LastFrame[i]; |
| 148 |
p += 4; |
| 149 |
} |
| 150 |
|
| 151 |
return buf; |
| 152 |
} |
| 153 |
|
| 154 |
void CServerSetup::Deserialize(const unsigned char *p) |
| 155 |
{ |
| 156 |
int i; |
| 157 |
|
| 158 |
this->ResourcesOption = *p++; |
| 159 |
this->UnitsOption = *p++; |
| 160 |
this->FogOfWar = *p++; |
| 161 |
this->RevealMap = *p++; |
| 162 |
this->GameTypeOption = *p++; |
| 163 |
this->Difficulty = *p++; |
| 164 |
this->MapRichness = *p++; |
| 165 |
for (i = 0; i < PlayerMax; ++i) { |
| 166 |
this->CompOpt[i] = *p++; |
| 167 |
} |
| 168 |
for (i = 0; i < PlayerMax; ++i) { |
| 169 |
this->Ready[i] = *p++; |
| 170 |
} |
| 171 |
for (i = 0; i < PlayerMax; ++i) { |
| 172 |
this->LastFrame[i] = *(Uint32 *)p; |
| 173 |
p += 4; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
unsigned char *CInitMessage::Serialize() const |
| 178 |
{ |
| 179 |
unsigned char *buf = new unsigned char[CInitMessage::Size()]; |
| 180 |
unsigned char *p = buf; |
| 181 |
unsigned char *x; |
| 182 |
|
| 183 |
*p++ = this->Type; |
| 184 |
*p++ = this->SubType; |
| 185 |
*(Sint32 *)p = this->Stratagus; |
| 186 |
p += 4; |
| 187 |
*(Sint32 *)p = this->Version; |
| 188 |
p += 4; |
| 189 |
*(Uint32 *)p = this->ConfUID; |
| 190 |
p += 4; |
| 191 |
*(Uint32 *)p = this->MapUID; |
| 192 |
p += 4; |
| 193 |
*(Sint32 *)p = this->Lag; |
| 194 |
p += 4; |
| 195 |
*(Sint32 *)p = this->Updates; |
| 196 |
p += 4; |
| 197 |
*p++ = this->HostsCount; |
| 198 |
|
| 199 |
switch (this->SubType) { |
| 200 |
case ICMHello: |
| 201 |
case ICMConfig: |
| 202 |
case ICMWelcome: |
| 203 |
case ICMResync: |
| 204 |
case ICMGo: |
| 205 |
for (int i = 0; i < PlayerMax; ++i) { |
| 206 |
x = this->u.Hosts[i].Serialize(); |
| 207 |
memcpy(p, x, CNetworkHost::Size()); |
| 208 |
p += CNetworkHost::Size(); |
| 209 |
delete[] x; |
| 210 |
} |
| 211 |
break; |
| 212 |
case ICMMap: |
| 213 |
memcpy(p, this->u.MapPath, sizeof(this->u.MapPath)); |
| 214 |
p += sizeof(this->u.MapPath); |
| 215 |
break; |
| 216 |
case ICMState: |
| 217 |
x = this->u.State.Serialize(); |
| 218 |
memcpy(p, x, CServerSetup::Size()); |
| 219 |
p += CServerSetup::Size(); |
| 220 |
delete[] x; |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
return buf; |
| 225 |
} |
| 226 |
|
| 227 |
void CInitMessage::Deserialize(const unsigned char *p) |
| 228 |
{ |
| 229 |
this->Type = *p++; |
| 230 |
this->SubType = *p++; |
| 231 |
this->Stratagus = *(Sint32 *)p; |
| 232 |
p += 4; |
| 233 |
this->Version = *(Sint32 *)p; |
| 234 |
p += 4; |
| 235 |
this->ConfUID = *(Uint32 *)p; |
| 236 |
p += 4; |
| 237 |
this->MapUID = *(Uint32 *)p; |
| 238 |
p += 4; |
| 239 |
this->Lag = *(Sint32 *)p; |
| 240 |
p += 4; |
| 241 |
this->Updates = *(Sint32 *)p; |
| 242 |
p += 4; |
| 243 |
this->HostsCount = *p++; |
| 244 |
|
| 245 |
switch (this->SubType) { |
| 246 |
case ICMHello: |
| 247 |
case ICMConfig: |
| 248 |
case ICMWelcome: |
| 249 |
case ICMResync: |
| 250 |
case ICMGo: |
| 251 |
for (int i = 0; i < PlayerMax; ++i) { |
| 252 |
this->u.Hosts[i].Deserialize(p); |
| 253 |
p += CNetworkHost::Size(); |
| 254 |
} |
| 255 |
break; |
| 256 |
case ICMMap: |
| 257 |
memcpy(this->u.MapPath, p, sizeof(this->u.MapPath)); |
| 258 |
p += sizeof(this->u.MapPath); |
| 259 |
break; |
| 260 |
case ICMState: |
| 261 |
this->u.State.Deserialize(p); |
| 262 |
p += CServerSetup::Size(); |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
//---------------------------------------------------------------------------- |
| 268 |
// Functions |
| 269 |
//---------------------------------------------------------------------------- |
| 270 |
|
| 271 |
/** |
| 272 |
** Send an InitConfig message across the Network |
| 273 |
** |
| 274 |
** @param host Host to send to (network byte order). |
| 275 |
** @param port Port of host to send to (network byte order). |
| 276 |
** @param msg The message to send |
| 277 |
** |
| 278 |
** @todo FIXME: we don't need to put the header into all messages. |
| 279 |
** (header = msg->Stratagus ... ) |
| 280 |
*/ |
| 281 |
static int NetworkSendICMessage(unsigned long host, int port, CInitMessage *msg) |
| 282 |
{ |
| 283 |
msg->Stratagus = htonl(StratagusVersion); |
| 284 |
msg->Version = htonl(NetworkProtocolVersion); |
| 285 |
msg->Lag = htonl(NetworkLag); |
| 286 |
msg->Updates = htonl(NetworkUpdates); |
| 287 |
|
| 288 |
unsigned char *buf = msg->Serialize(); |
| 289 |
int ret = NetSendUDP(NetworkFildes, host, port, buf, CInitMessage::Size()); |
| 290 |
delete[] buf; |
| 291 |
return ret; |
| 292 |
} |
| 293 |
|
| 294 |
#ifdef DEBUG |
| 295 |
static const char *ncconstatenames[] = { |
| 296 |
"ccs_unused", |
| 297 |
"ccs_connecting", // new client |
| 298 |
"ccs_connected", // has received slot info |
| 299 |
"ccs_mapinfo", // has received matching map-info |
| 300 |
"ccs_badmap", // has received non-matching map-info |
| 301 |
"ccs_synced", // client is in sync with server |
| 302 |
"ccs_async", // server user has changed selection |
| 303 |
"ccs_changed", // client user has made menu selection |
| 304 |
"ccs_detaching", // client user wants to detach |
| 305 |
"ccs_disconnected", // client has detached |
| 306 |
"ccs_unreachable", // server is unreachable |
| 307 |
"ccs_usercanceled", // user canceled game |
| 308 |
"ccs_nofreeslots", // server has no more free slots |
| 309 |
"ccs_serverquits", // server quits |
| 310 |
"ccs_goahead", // server wants to start game |
| 311 |
"ccs_started", // server has started game |
| 312 |
"ccs_incompatibleengine", // incompatible engine version |
| 313 |
"ccs_incompatiblenetwork", // incompatible network version |
| 314 |
}; |
| 315 |
|
| 316 |
static const char *icmsgsubtypenames[] = { |
| 317 |
"Hello", // Client Request |
| 318 |
"Config", // Setup message configure clients |
| 319 |
|
| 320 |
"EngineMismatch", // Stratagus engine version doesn't match |
| 321 |
"ProtocolMismatch", // Network protocol version doesn't match |
| 322 |
"EngineConfMismatch", // Engine configuration isn't identical |
| 323 |
"MapUidMismatch", // MAP UID doesn't match |
| 324 |
|
| 325 |
"GameFull", // No player slots available |
| 326 |
"Welcome", // Acknowledge for new client connections |
| 327 |
|
| 328 |
"Waiting", // Client has received Welcome and is waiting for Map/State |
| 329 |
"Map", // MapInfo (and Mapinfo Ack) |
| 330 |
"State", // StateInfo |
| 331 |
"Resync", // Ack StateInfo change |
| 332 |
|
| 333 |
"ServerQuit", // Server has quit game |
| 334 |
"GoodBye", // Client wants to leave game |
| 335 |
"SeeYou", // Client has left game |
| 336 |
|
| 337 |
"Go", // Client is ready to run |
| 338 |
"AreYouThere", // Server asks are you there |
| 339 |
"IAmHere", // Client answers I am here |
| 340 |
}; |
| 341 |
#endif |
| 342 |
|
| 343 |
/** |
| 344 |
** Send a message to the server, but only if the last packet was a while |
| 345 |
** ago |
| 346 |
** |
| 347 |
** @param msg The message to send |
| 348 |
** @param msecs microseconds to delay |
| 349 |
*/ |
| 350 |
static void NetworkSendRateLimitedClientMessage(CInitMessage *msg, unsigned long msecs) |
| 351 |
{ |
| 352 |
unsigned long now; |
| 353 |
int n; |
| 354 |
|
| 355 |
now = GetTicks(); |
| 356 |
if (now - NetLastPacketSent >= msecs) { |
| 357 |
NetLastPacketSent = now; |
| 358 |
if (msg->SubType == LastStateMsgType) { |
| 359 |
++NetStateMsgCnt; |
| 360 |
} else { |
| 361 |
NetStateMsgCnt = 0; |
| 362 |
LastStateMsgType = msg->SubType; |
| 363 |
} |
| 364 |
n = NetworkSendICMessage(NetworkServerIP, htons(NetworkServerPort), |
| 365 |
msg); |
| 366 |
if (!NetStateMsgCnt) { |
| 367 |
DebugPrint |
| 368 |
("Sending Init Message (%s:%d): %d:%d(%d) %d.%d.%d.%d:%d\n" _C_ |
| 369 |
ncconstatenames[NetLocalState] _C_ NetStateMsgCnt _C_ |
| 370 |
msg->Type _C_ msg->SubType _C_ n _C_ |
| 371 |
NIPQUAD(ntohl(NetworkServerIP)) _C_ NetworkServerPort); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
** Setup the IP-Address of the network server to connect to |
| 378 |
** |
| 379 |
** @param serveraddr the serveraddress the user has entered |
| 380 |
** |
| 381 |
** @return True, if error; otherwise false. |
| 382 |
*/ |
| 383 |
int NetworkSetupServerAddress(const std::string &serveraddr) |
| 384 |
{ |
| 385 |
unsigned long addr; |
| 386 |
|
| 387 |
addr = NetResolveHost(serveraddr); |
| 388 |
if (addr == INADDR_NONE) { |
| 389 |
return 1; |
| 390 |
} |
| 391 |
NetworkServerIP = addr; |
| 392 |
|
| 393 |
DebugPrint("SELECTED SERVER: %s (%d.%d.%d.%d)\n" _C_ serveraddr.c_str() _C_ |
| 394 |
NIPQUAD(ntohl(addr))); |
| 395 |
|
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
** Setup Network connect state machine for clients |
| 401 |
*/ |
| 402 |
void NetworkInitClientConnect(void) |
| 403 |
{ |
| 404 |
int i; |
| 405 |
|
| 406 |
NetConnectRunning = 2; |
| 407 |
NetLastPacketSent = GetTicks(); |
| 408 |
NetLocalState = ccs_connecting; |
| 409 |
NetStateMsgCnt = 0; |
| 410 |
NetworkServerPort = NetworkPort; |
| 411 |
LastStateMsgType = ICMServerQuit; |
| 412 |
for (i = 0; i < PlayerMax; ++i) { |
| 413 |
Hosts[i].Host = 0; |
| 414 |
Hosts[i].Port = 0; |
| 415 |
Hosts[i].PlyNr = 0; |
| 416 |
memset(Hosts[i].PlyName, 0, sizeof(Hosts[i].PlyName)); |
| 417 |
} |
| 418 |
ServerSetupState.Clear(); |
| 419 |
LocalSetupState.Clear(); |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
** Terminate Network connect state machine for clients |
| 424 |
*/ |
| 425 |
void NetworkExitClientConnect(void) |
| 426 |
{ |
| 427 |
NetConnectRunning = 0; |
| 428 |
NetPlayers = 0; // Make single player menus work again! |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
** Terminate and detach Network connect state machine for the client |
| 433 |
*/ |
| 434 |
void NetworkDetachFromServer(void) |
| 435 |
{ |
| 436 |
NetLocalState = ccs_detaching; |
| 437 |
NetStateMsgCnt = 0; |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
** Setup Network connect state machine for the server |
| 442 |
*/ |
| 443 |
void NetworkInitServerConnect(int openslots) |
| 444 |
{ |
| 445 |
int i; |
| 446 |
|
| 447 |
NetConnectRunning = 1; |
| 448 |
|
| 449 |
// Cannot use NetPlayers here, as map change might modify the number!! |
| 450 |
for (i = 0; i < PlayerMax; ++i) { |
| 451 |
NetStates[i].State = ccs_unused; |
| 452 |
Hosts[i].Host = 0; |
| 453 |
Hosts[i].Port = 0; |
| 454 |
Hosts[i].PlyNr = 0; // slotnr until final cfg msg |
| 455 |
memset(Hosts[i].PlyName, 0, sizeof(Hosts[i].PlyName)); |
| 456 |
} |
| 457 |
|
| 458 |
// preset the server (initially always slot 0) |
| 459 |
strncpy_s(Hosts[0].PlyName, sizeof(Hosts[0].PlyName), |
| 460 |
LocalPlayerName.c_str(), _TRUNCATE); |
| 461 |
|
| 462 |
ServerSetupState.Clear(); |
| 463 |
LocalSetupState.Clear(); |
| 464 |
for (i = openslots; i < PlayerMax - 1; ++i) { |
| 465 |
ServerSetupState.CompOpt[i] = 1; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
** Terminate Network connect state machine for the server |
| 471 |
*/ |
| 472 |
void NetworkExitServerConnect(void) |
| 473 |
{ |
| 474 |
int h; |
| 475 |
int i; |
| 476 |
int n; |
| 477 |
CInitMessage message; |
| 478 |
|
| 479 |
message.Type = MessageInitReply; |
| 480 |
message.SubType = ICMServerQuit; |
| 481 |
for (h = 1; h < PlayerMax - 1; ++h) { |
| 482 |
// Spew out 5 and trust in God that they arrive |
| 483 |
// Clients will time out otherwise anyway |
| 484 |
if (Hosts[h].PlyNr) { |
| 485 |
for (i = 0; i < 5; ++i) { |
| 486 |
n = NetworkSendICMessage(Hosts[h].Host, Hosts[h].Port, &message); |
| 487 |
DebugPrint("Sending InitReply Message ServerQuit: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 488 |
n _C_ NIPQUAD(ntohl(Hosts[h].Host)) _C_ ntohs(Hosts[h].Port)); |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
NetworkInitServerConnect(0); // Reset Hosts slots |
| 494 |
|
| 495 |
NetConnectRunning = 0; |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
** Notify state change by menu user to connected clients |
| 500 |
*/ |
| 501 |
void NetworkServerResyncClients(void) |
| 502 |
{ |
| 503 |
int i; |
| 504 |
|
| 505 |
if (NetConnectRunning) { |
| 506 |
for (i = 1; i < PlayerMax - 1; ++i) { |
| 507 |
if (Hosts[i].PlyNr && NetStates[i].State == ccs_synced) { |
| 508 |
NetStates[i].State = ccs_async; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
** Server user has finally hit the start game button |
| 516 |
*/ |
| 517 |
void NetworkServerStartGame(void) |
| 518 |
{ |
| 519 |
int h; |
| 520 |
int i; |
| 521 |
int j; |
| 522 |
int n; |
| 523 |
int num[PlayerMax]; |
| 524 |
int org[PlayerMax]; |
| 525 |
int rev[PlayerMax]; |
| 526 |
unsigned char buf[1024]; |
| 527 |
CInitMessage msg; |
| 528 |
CInitMessage message; |
| 529 |
CInitMessage statemsg; |
| 530 |
|
| 531 |
Assert(ServerSetupState.CompOpt[0] == 0); |
| 532 |
|
| 533 |
// save it first.. |
| 534 |
LocalSetupState = ServerSetupState; |
| 535 |
|
| 536 |
// Make a list of the available player slots. |
| 537 |
for (h = i = 0; i < PlayerMax; ++i) { |
| 538 |
if (Map.Info.PlayerType[i] == PlayerPerson) { |
| 539 |
rev[i] = h; |
| 540 |
num[h++] = i; |
| 541 |
DebugPrint("Slot %d is available for an interactive player (%d)\n" _C_ |
| 542 |
i _C_ rev[i]); |
| 543 |
} |
| 544 |
} |
| 545 |
// Make a list of the available computer slots. |
| 546 |
n = h; |
| 547 |
for (i = 0; i < PlayerMax; ++i) { |
| 548 |
if (Map.Info.PlayerType[i] == PlayerComputer) { |
| 549 |
rev[i] = n++; |
| 550 |
DebugPrint("Slot %d is available for an ai computer player (%d)\n" _C_ |
| 551 |
i _C_ rev[i]); |
| 552 |
} |
| 553 |
} |
| 554 |
// Make a list of the remaining slots. |
| 555 |
for (i = 0; i < PlayerMax; ++i) { |
| 556 |
if (Map.Info.PlayerType[i] != PlayerPerson && |
| 557 |
Map.Info.PlayerType[i] != PlayerComputer) { |
| 558 |
rev[i] = n++; |
| 559 |
// PlayerNobody - not available to anything.. |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
#if 0 |
| 564 |
printf("INITIAL ServerSetupState:\n"); |
| 565 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 566 |
printf("%02d: CO: %d Host: ", i, ServerSetupState.CompOpt[i]); |
| 567 |
if (ServerSetupState.CompOpt[i] == 0) { |
| 568 |
printf(" %d.%d.%d.%d:%d %s", NIPQUAD(ntohl(Hosts[i].Host)), |
| 569 |
ntohs(Hosts[i].Port), Hosts[i].PlyName); |
| 570 |
} |
| 571 |
printf("\n"); |
| 572 |
} |
| 573 |
#endif |
| 574 |
|
| 575 |
// Reverse to assign slots to menu setup state positions. |
| 576 |
for (i = 0; i < PlayerMax; ++i) { |
| 577 |
org[i] = -1; |
| 578 |
for (j = 0; j < PlayerMax; ++j) { |
| 579 |
if (rev[j] == i) { |
| 580 |
org[i] = j; |
| 581 |
break; |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
// Calculate NetPlayers |
| 587 |
NetPlayers = h; |
| 588 |
for (i = 1; i < h; ++i) { |
| 589 |
if (Hosts[i].PlyNr == 0 && ServerSetupState.CompOpt[i] != 0) { |
| 590 |
NetPlayers--; |
| 591 |
} else if (Hosts[i].PlyName[0] == 0) { |
| 592 |
// Unused slot gets a computer player |
| 593 |
ServerSetupState.CompOpt[i] = 1; |
| 594 |
LocalSetupState.CompOpt[i] = 1; |
| 595 |
NetPlayers--; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
// Compact host list.. (account for computer/closed slots in the middle..) |
| 600 |
for (i = 1; i < h; ++i) { |
| 601 |
if (Hosts[i].PlyNr == 0) { |
| 602 |
for (j = i + 1; j < PlayerMax - 1; ++j) { |
| 603 |
if (Hosts[j].PlyNr) { |
| 604 |
DebugPrint("Compact: Hosts %d -> Hosts %d\n" _C_ j _C_ i); |
| 605 |
Hosts[i] = Hosts[j]; |
| 606 |
Hosts[j].PlyNr = 0; |
| 607 |
Hosts[j].Host = Hosts[j].Port = 0; |
| 608 |
n = LocalSetupState.CompOpt[i]; |
| 609 |
LocalSetupState.CompOpt[i] = LocalSetupState.CompOpt[j]; |
| 610 |
LocalSetupState.CompOpt[j] = n; |
| 611 |
n = LocalSetupState.LastFrame[i]; |
| 612 |
LocalSetupState.LastFrame[i] = LocalSetupState.LastFrame[j]; |
| 613 |
LocalSetupState.LastFrame[j] = n; |
| 614 |
break; |
| 615 |
} |
| 616 |
} |
| 617 |
if (j == PlayerMax - 1) { |
| 618 |
break; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
// Randomize the position. |
| 624 |
// It can be disabled by writing NoRandomPlacementMultiplayer() in lua files. |
| 625 |
// Players slots are then mapped to players numbers(and colors). |
| 626 |
|
| 627 |
j = h; |
| 628 |
if (NoRandomPlacementMultiplayer == 1) { |
| 629 |
for (i = 0; i < PlayerMax; ++i) { |
| 630 |
if (Map.Info.PlayerType[i] != PlayerComputer) { |
| 631 |
org[i] = Hosts[i].PlyNr; |
| 632 |
} |
| 633 |
} |
| 634 |
} else { |
| 635 |
for (i = 0; i < NetPlayers; ++i) { |
| 636 |
if (j > 0) { |
| 637 |
int k; |
| 638 |
int o; |
| 639 |
int chosen; |
| 640 |
|
| 641 |
chosen = MyRand() % j; |
| 642 |
|
| 643 |
n = num[chosen]; |
| 644 |
Hosts[i].PlyNr = n; |
| 645 |
k = org[i]; |
| 646 |
if (k != n) { |
| 647 |
for (o = 0; o < PlayerMax; ++o) { |
| 648 |
if (org[o] == n) { |
| 649 |
org[o] = k; |
| 650 |
break; |
| 651 |
} |
| 652 |
} |
| 653 |
org[i] = n; |
| 654 |
} |
| 655 |
DebugPrint("Assigning player %d to slot %d (%d)\n" _C_ |
| 656 |
i _C_ n _C_ org[i]); |
| 657 |
|
| 658 |
num[chosen] = num[--j]; |
| 659 |
} else { |
| 660 |
Assert(0); |
| 661 |
#if 0 |
| 662 |
// ARI: is this code path really executed? (initially h >= NetPlayers..) |
| 663 |
// Above Debugcheck will catch it.. |
| 664 |
Hosts[i].PlyNr = num[0]; |
| 665 |
DebugPrint("Hosts[%d].PlyNr = %i\n" _C_ i _C_ num[0]); |
| 666 |
#endif |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
// Complete all setup states for the assigned slots. |
| 672 |
for (i = 0; i < PlayerMax; ++i) { |
| 673 |
num[i] = 1; |
| 674 |
n = org[i]; |
| 675 |
ServerSetupState.CompOpt[n] = LocalSetupState.CompOpt[i]; |
| 676 |
ServerSetupState.LastFrame[n] = LocalSetupState.LastFrame[i]; |
| 677 |
} |
| 678 |
|
| 679 |
/* NOW we have NetPlayers in Hosts array, with ServerSetupState shuffled up to match it.. */ |
| 680 |
|
| 681 |
// |
| 682 |
// Send all clients host:ports to all clients. |
| 683 |
// Slot 0 is the server! |
| 684 |
// |
| 685 |
NetLocalPlayerNumber = Hosts[0].PlyNr; |
| 686 |
HostsCount = NetPlayers - 1; |
| 687 |
|
| 688 |
// |
| 689 |
// Move ourselves (server slot 0) to the end of the list |
| 690 |
// |
| 691 |
Hosts[PlayerMax - 1] = Hosts[0]; // last slot unused (special), use as tempstore.. |
| 692 |
Hosts[0] = Hosts[HostsCount]; |
| 693 |
Hosts[HostsCount] = Hosts[PlayerMax - 1]; |
| 694 |
Hosts[PlayerMax - 1].PlyNr = 0; |
| 695 |
|
| 696 |
// Prepare the final config message: |
| 697 |
message.Type = MessageInitReply; |
| 698 |
message.SubType = ICMConfig; |
| 699 |
message.HostsCount = NetPlayers; |
| 700 |
message.MapUID = htonl(Map.Info.MapUID); |
| 701 |
for (i = 0; i < NetPlayers; ++i) { |
| 702 |
message.u.Hosts[i].Host = Hosts[i].Host; |
| 703 |
message.u.Hosts[i].Port = Hosts[i].Port; |
| 704 |
memcpy(message.u.Hosts[i].PlyName, Hosts[i].PlyName, sizeof(message.u.Hosts[i].PlyName) - 1); |
| 705 |
message.u.Hosts[i].PlyNr = htons(Hosts[i].PlyNr); |
| 706 |
} |
| 707 |
|
| 708 |
// Prepare the final state message: |
| 709 |
statemsg.Type = MessageInitReply; |
| 710 |
statemsg.SubType = ICMState; |
| 711 |
statemsg.HostsCount = NetPlayers; |
| 712 |
statemsg.u.State = ServerSetupState; |
| 713 |
statemsg.MapUID = htonl(Map.Info.MapUID); |
| 714 |
|
| 715 |
DebugPrint("Ready, sending InitConfig to %d host(s)\n" _C_ HostsCount); |
| 716 |
// |
| 717 |
// Send all clients host:ports to all clients. |
| 718 |
// |
| 719 |
for (j = HostsCount; j;) { |
| 720 |
|
| 721 |
breakout: |
| 722 |
// Send to all clients. |
| 723 |
for (i = 0; i < HostsCount; ++i) { |
| 724 |
unsigned long host; |
| 725 |
int port; |
| 726 |
|
| 727 |
if (num[Hosts[i].PlyNr] == 1) { // not acknowledged yet |
| 728 |
host = message.u.Hosts[i].Host; |
| 729 |
port = message.u.Hosts[i].Port; |
| 730 |
message.u.Hosts[i].Host = message.u.Hosts[i].Port = 0; |
| 731 |
n = NetworkSendICMessage(host, port, &message); |
| 732 |
DebugPrint("Sending InitConfig Message Config (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 733 |
n _C_ NIPQUAD(ntohl(host)) _C_ ntohs(port)); |
| 734 |
message.u.Hosts[i].Host = host; |
| 735 |
message.u.Hosts[i].Port = port; |
| 736 |
} else if (num[Hosts[i].PlyNr] == 2) { |
| 737 |
host = message.u.Hosts[i].Host; |
| 738 |
port = message.u.Hosts[i].Port; |
| 739 |
n = NetworkSendICMessage(host, port, &statemsg); |
| 740 |
DebugPrint("Sending InitReply Message State: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 741 |
n _C_ NIPQUAD(ntohl(host)) _C_ ntohs(port)); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
// Wait for acknowledge |
| 746 |
while (j && NetSocketReady(NetworkFildes, 1000)) { |
| 747 |
if ((n = NetRecvUDP(NetworkFildes, buf, sizeof(buf))) < 0) { |
| 748 |
DebugPrint("*Receive ack failed: (%d) from %d.%d.%d.%d:%d\n" _C_ |
| 749 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 750 |
continue; |
| 751 |
} |
| 752 |
|
| 753 |
if (n != (int)CInitMessage::Size()) { |
| 754 |
DebugPrint("Unexpected message size\n"); |
| 755 |
continue; |
| 756 |
} |
| 757 |
|
| 758 |
msg.Deserialize(buf); |
| 759 |
if (msg.Type == MessageInitHello) { |
| 760 |
switch (msg.SubType) { |
| 761 |
|
| 762 |
case ICMConfig: |
| 763 |
DebugPrint("Got ack for InitConfig: (%d) from %d.%d.%d.%d:%d\n" _C_ |
| 764 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 765 |
|
| 766 |
for (i = 0; i < HostsCount; ++i) { |
| 767 |
if (NetLastHost == Hosts[i].Host && NetLastPort == Hosts[i].Port) { |
| 768 |
if (num[Hosts[i].PlyNr] == 1) { |
| 769 |
num[Hosts[i].PlyNr]++; |
| 770 |
} |
| 771 |
goto breakout; |
| 772 |
} |
| 773 |
} |
| 774 |
break; |
| 775 |
|
| 776 |
case ICMGo: |
| 777 |
DebugPrint("Got ack for InitState: (%d) from %d.%d.%d.%d:%d\n" _C_ |
| 778 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 779 |
|
| 780 |
for (i = 0; i < HostsCount; ++i) { |
| 781 |
if (NetLastHost == Hosts[i].Host && NetLastPort == Hosts[i].Port) { |
| 782 |
if (num[Hosts[i].PlyNr] == 2) { |
| 783 |
num[Hosts[i].PlyNr] = 0; |
| 784 |
--j; |
| 785 |
DebugPrint("Removing host %d from waiting list\n" _C_ j); |
| 786 |
} |
| 787 |
break; |
| 788 |
} |
| 789 |
} |
| 790 |
break; |
| 791 |
|
| 792 |
default: |
| 793 |
DebugPrint("Server: Config ACK: Unhandled subtype %d\n" _C_ msg.SubType); |
| 794 |
break; |
| 795 |
} |
| 796 |
} else { |
| 797 |
DebugPrint("Unexpected Message Type %d while waiting for Config ACK\n" _C_ msg.Type); |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
DebugPrint("DONE: All configs acked - Now starting..\n"); |
| 803 |
|
| 804 |
// Give clients a quick-start kick.. |
| 805 |
message.SubType = ICMGo; |
| 806 |
for (i = 0; i < HostsCount; ++i) { |
| 807 |
unsigned long host; |
| 808 |
int port; |
| 809 |
|
| 810 |
host = message.u.Hosts[i].Host; |
| 811 |
port = message.u.Hosts[i].Port; |
| 812 |
n = NetworkSendICMessage(host, port, &message); |
| 813 |
DebugPrint("Sending InitReply Message Go: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 814 |
n _C_ NIPQUAD(ntohl(host)) _C_ ntohs(port)); |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
/** |
| 819 |
** Multiplayer network game final player type setup. |
| 820 |
*/ |
| 821 |
void NetworkGamePrepareGameSettings(void) |
| 822 |
{ |
| 823 |
int c; |
| 824 |
int h; |
| 825 |
int i; |
| 826 |
int num[PlayerMax]; |
| 827 |
int comp[PlayerMax]; |
| 828 |
|
| 829 |
DebugPrint("NetPlayers = %d\n" _C_ NetPlayers); |
| 830 |
|
| 831 |
GameSettings.NetGameType = SettingsMultiPlayerGame; |
| 832 |
|
| 833 |
#ifdef DEBUG |
| 834 |
for (i = 0; i < PlayerMax-1; i++) { |
| 835 |
printf("%02d: CO: %d Host: ", i, ServerSetupState.CompOpt[i]); |
| 836 |
if (ServerSetupState.CompOpt[i] == 0) { |
| 837 |
for (h = 0; h < NetPlayers; h++) { |
| 838 |
if (Hosts[h].PlyNr == i) { |
| 839 |
printf("%s", Hosts[h].PlyName); |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
printf("\n"); |
| 844 |
} |
| 845 |
#endif |
| 846 |
|
| 847 |
// Make a list of the available player slots. |
| 848 |
for (c = h = i = 0; i < PlayerMax; i++) { |
| 849 |
if (Map.Info.PlayerType[i] == PlayerPerson) { |
| 850 |
num[h++] = i; |
| 851 |
} |
| 852 |
if (Map.Info.PlayerType[i] == PlayerComputer) { |
| 853 |
comp[c++] = i; // available computer player slots |
| 854 |
} |
| 855 |
} |
| 856 |
for (i = 0; i < h; i++) { |
| 857 |
switch(ServerSetupState.CompOpt[num[i]]) { |
| 858 |
case 0: |
| 859 |
GameSettings.Presets[num[i]].Type = PlayerPerson; |
| 860 |
break; |
| 861 |
case 1: |
| 862 |
GameSettings.Presets[num[i]].Type = PlayerComputer; |
| 863 |
break; |
| 864 |
case 2: |
| 865 |
GameSettings.Presets[num[i]].Type = PlayerNobody; |
| 866 |
default: |
| 867 |
break; |
| 868 |
} |
| 869 |
} |
| 870 |
for (i = 0; i < c; i++) { |
| 871 |
if (ServerSetupState.CompOpt[comp[i]] == 2) { // closed.. |
| 872 |
GameSettings.Presets[comp[i]].Type = PlayerNobody; |
| 873 |
DebugPrint("Settings[%d].Type == Closed\n" _C_ comp[i]); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
#ifdef DEBUG |
| 878 |
for (i = 0; i < NetPlayers; i++) { |
| 879 |
Assert(GameSettings.Presets[Hosts[i].PlyNr].Type == PlayerPerson); |
| 880 |
; |
| 881 |
} |
| 882 |
#endif |
| 883 |
} |
| 884 |
|
| 885 |
/** |
| 886 |
** Assign player slots and names in a network game.. |
| 887 |
*/ |
| 888 |
void NetworkConnectSetupGame(void) |
| 889 |
{ |
| 890 |
ThisPlayer->SetName(LocalPlayerName); |
| 891 |
for (int i = 0; i < HostsCount; ++i) { |
| 892 |
Players[Hosts[i].PlyNr].SetName(Hosts[i].PlyName); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
** Callback from netconnect loop in Client-Sync state: |
| 898 |
** Compare local state with server's information |
| 899 |
** and force update when changes have occured. |
| 900 |
*/ |
| 901 |
void NetClientCheckLocalState(void) |
| 902 |
{ |
| 903 |
if (LocalSetupState.Ready[NetLocalHostsSlot] != ServerSetupState.Ready[NetLocalHostsSlot]) { |
| 904 |
NetLocalState = ccs_changed; |
| 905 |
return; |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
/** |
| 910 |
** Client Menu Loop: Send out client request messages |
| 911 |
*/ |
| 912 |
void NetworkProcessClientRequest(void) |
| 913 |
{ |
| 914 |
CInitMessage message; |
| 915 |
int i; |
| 916 |
|
| 917 |
memset(&message, 0, sizeof(message)); |
| 918 |
changed: |
| 919 |
switch (NetLocalState) { |
| 920 |
case ccs_disconnected: |
| 921 |
message.Type = MessageInitHello; |
| 922 |
message.SubType = ICMSeeYou; |
| 923 |
// Spew out 5 and trust in God that they arrive |
| 924 |
for (i = 0; i < 5; ++i) { |
| 925 |
NetworkSendICMessage(NetworkServerIP, htons(NetworkServerPort), &message); |
| 926 |
} |
| 927 |
NetLocalState = ccs_usercanceled; |
| 928 |
NetConnectRunning = 0; // End the menu.. |
| 929 |
break; |
| 930 |
case ccs_detaching: |
| 931 |
if (NetStateMsgCnt < 10) { // 10 retries = 1 second |
| 932 |
message.Type = MessageInitHello; |
| 933 |
message.SubType = ICMGoodBye; |
| 934 |
NetworkSendRateLimitedClientMessage(&message, 100); |
| 935 |
} else { |
| 936 |
// Server is ignoring us - break out! |
| 937 |
NetLocalState = ccs_unreachable; |
| 938 |
NetConnectRunning = 0; // End the menu.. |
| 939 |
DebugPrint("ccs_detaching: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 940 |
} |
| 941 |
break; |
| 942 |
case ccs_connecting: // connect to server |
| 943 |
if (NetStateMsgCnt < 48) { // 48 retries = 24 seconds |
| 944 |
message.Type = MessageInitHello; |
| 945 |
message.SubType = ICMHello; |
| 946 |
strncpy_s(message.u.Hosts[0].PlyName, |
| 947 |
sizeof(message.u.Hosts[0].PlyName), |
| 948 |
LocalPlayerName.c_str(), _TRUNCATE); |
| 949 |
message.MapUID = 0L; |
| 950 |
NetworkSendRateLimitedClientMessage(&message, 500); |
| 951 |
} else { |
| 952 |
NetLocalState = ccs_unreachable; |
| 953 |
NetConnectRunning = 0; // End the menu.. |
| 954 |
DebugPrint("ccs_connecting: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 955 |
} |
| 956 |
break; |
| 957 |
case ccs_connected: |
| 958 |
if (NetStateMsgCnt < 20) { // 20 retries |
| 959 |
message.Type = MessageInitHello; |
| 960 |
message.SubType = ICMWaiting; |
| 961 |
NetworkSendRateLimitedClientMessage(&message, 650); |
| 962 |
} else { |
| 963 |
NetLocalState = ccs_unreachable; |
| 964 |
NetConnectRunning = 0; // End the menu.. |
| 965 |
DebugPrint("ccs_connected: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 966 |
} |
| 967 |
break; |
| 968 |
case ccs_synced: |
| 969 |
NetClientCheckLocalState(); |
| 970 |
if (NetLocalState != ccs_synced) { |
| 971 |
NetStateMsgCnt = 0; |
| 972 |
goto changed; |
| 973 |
} |
| 974 |
message.Type = MessageInitHello; |
| 975 |
message.SubType = ICMWaiting; |
| 976 |
NetworkSendRateLimitedClientMessage(&message, 850); |
| 977 |
break; |
| 978 |
case ccs_changed: |
| 979 |
if (NetStateMsgCnt < 20) { // 20 retries |
| 980 |
message.Type = MessageInitHello; |
| 981 |
message.SubType = ICMState; |
| 982 |
message.u.State = LocalSetupState; |
| 983 |
message.MapUID = htonl(Map.Info.MapUID); |
| 984 |
NetworkSendRateLimitedClientMessage(&message, 450); |
| 985 |
} else { |
| 986 |
NetLocalState = ccs_unreachable; |
| 987 |
NetConnectRunning = 0; // End the menu.. |
| 988 |
DebugPrint("ccs_changed: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 989 |
} |
| 990 |
break; |
| 991 |
case ccs_async: |
| 992 |
if (NetStateMsgCnt < 20) { // 20 retries |
| 993 |
message.Type = MessageInitHello; |
| 994 |
message.SubType = ICMResync; |
| 995 |
NetworkSendRateLimitedClientMessage(&message, 450); |
| 996 |
} else { |
| 997 |
NetLocalState = ccs_unreachable; |
| 998 |
NetConnectRunning = 0; // End the menu.. |
| 999 |
DebugPrint("ccs_async: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 1000 |
} |
| 1001 |
break; |
| 1002 |
case ccs_mapinfo: |
| 1003 |
if (NetStateMsgCnt < 20) { // 20 retries |
| 1004 |
message.Type = MessageInitHello; |
| 1005 |
message.SubType = ICMMap; // ICMMapAck.. |
| 1006 |
message.MapUID = htonl(Map.Info.MapUID); |
| 1007 |
NetworkSendRateLimitedClientMessage(&message, 650); |
| 1008 |
} else { |
| 1009 |
NetLocalState = ccs_unreachable; |
| 1010 |
NetConnectRunning = 0; // End the menu.. |
| 1011 |
DebugPrint("ccs_mapinfo: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 1012 |
} |
| 1013 |
case ccs_badmap: |
| 1014 |
if (NetStateMsgCnt < 20) { // 20 retries |
| 1015 |
message.Type = MessageInitHello; |
| 1016 |
message.SubType = ICMMapUidMismatch; |
| 1017 |
message.MapUID = htonl(Map.Info.MapUID); // MAP Uid doesn't match |
| 1018 |
NetworkSendRateLimitedClientMessage(&message, 650); |
| 1019 |
} else { |
| 1020 |
NetLocalState = ccs_unreachable; |
| 1021 |
NetConnectRunning = 0; // End the menu.. |
| 1022 |
DebugPrint("ccs_badmap: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 1023 |
} |
| 1024 |
break; |
| 1025 |
case ccs_goahead: |
| 1026 |
if (NetStateMsgCnt < 50) { // 50 retries |
| 1027 |
message.Type = MessageInitHello; |
| 1028 |
message.SubType = ICMConfig; |
| 1029 |
NetworkSendRateLimitedClientMessage(&message, 250); |
| 1030 |
} else { |
| 1031 |
NetLocalState = ccs_unreachable; |
| 1032 |
NetConnectRunning = 0; // End the menu.. |
| 1033 |
DebugPrint("ccs_goahead: Above message limit %d\n" _C_ NetStateMsgCnt); |
| 1034 |
} |
| 1035 |
case ccs_started: |
| 1036 |
if (NetStateMsgCnt < 20) { // 20 retries |
| 1037 |
message.Type = MessageInitHello; |
| 1038 |
message.SubType = ICMGo; |
| 1039 |
NetworkSendRateLimitedClientMessage(&message, 250); |
| 1040 |
} else { |
| 1041 |
NetConnectRunning = 0; // End the menu.. |
| 1042 |
} |
| 1043 |
break; |
| 1044 |
default: |
| 1045 |
break; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
/** |
| 1050 |
** Kick a client that doesn't answer to our packets |
| 1051 |
** |
| 1052 |
** @param c The client (host slot) to kick |
| 1053 |
*/ |
| 1054 |
static void KickDeadClient(int c) |
| 1055 |
{ |
| 1056 |
int n; |
| 1057 |
|
| 1058 |
DebugPrint("kicking client %d\n" _C_ Hosts[c].PlyNr); |
| 1059 |
NetStates[c].State = ccs_unused; |
| 1060 |
Hosts[c].Host = 0; |
| 1061 |
Hosts[c].Port = 0; |
| 1062 |
Hosts[c].PlyNr = 0; |
| 1063 |
memset(Hosts[c].PlyName, 0, sizeof(Hosts[c].PlyName)); |
| 1064 |
ServerSetupState.Ready[c] = 0; |
| 1065 |
ServerSetupState.LastFrame[c] = 0L; |
| 1066 |
|
| 1067 |
// Resync other clients |
| 1068 |
for (n = 1; n < PlayerMax - 1; ++n) { |
| 1069 |
if (n != c && Hosts[n].PlyNr) { |
| 1070 |
NetStates[n].State = ccs_async; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
/** |
| 1076 |
** Server Menu Loop: Send out server request messages |
| 1077 |
*/ |
| 1078 |
void NetworkProcessServerRequest(void) |
| 1079 |
{ |
| 1080 |
int i; |
| 1081 |
int n; |
| 1082 |
unsigned long fcd; |
| 1083 |
CInitMessage message; |
| 1084 |
|
| 1085 |
if (GameRunning) { |
| 1086 |
return; |
| 1087 |
// Game already started... |
| 1088 |
} |
| 1089 |
|
| 1090 |
for (i = 1; i < PlayerMax - 1; ++i) { |
| 1091 |
if (Hosts[i].PlyNr && Hosts[i].Host && Hosts[i].Port) { |
| 1092 |
fcd = FrameCounter - ServerSetupState.LastFrame[i]; |
| 1093 |
if (fcd >= CLIENT_LIVE_BEAT) { |
| 1094 |
if (fcd > CLIENT_IS_DEAD) { |
| 1095 |
KickDeadClient(i); |
| 1096 |
} else if (fcd % 5 == 0) { |
| 1097 |
message.Type = MessageInitReply; |
| 1098 |
message.SubType = ICMAYT; // Probe for the client |
| 1099 |
message.MapUID = 0L; |
| 1100 |
n = NetworkSendICMessage(Hosts[i].Host, Hosts[i].Port, &message); |
| 1101 |
DebugPrint("Sending InitReply Message AreYouThere: (%d) to %d.%d.%d.%d:%d (%ld:%ld)\n" _C_ |
| 1102 |
n _C_ NIPQUAD(ntohl(Hosts[i].Host)) _C_ ntohs(Hosts[i].Port) _C_ |
| 1103 |
FrameCounter _C_ (unsigned long)ServerSetupState.LastFrame[i]); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
#ifdef DEBUG |
| 1111 |
/** |
| 1112 |
** Parse a network menu packet in client disconnected state. |
| 1113 |
** |
| 1114 |
** @param msg message received |
| 1115 |
*/ |
| 1116 |
static void ClientParseDisconnected(const CInitMessage *msg) |
| 1117 |
{ |
| 1118 |
DebugPrint("ccs_disconnected: Server sending GoodBye dups %d\n" _C_ |
| 1119 |
msg->SubType); |
| 1120 |
} |
| 1121 |
#endif |
| 1122 |
|
| 1123 |
/** |
| 1124 |
** Parse a network menu packet in client detaching state. |
| 1125 |
** |
| 1126 |
** @param msg message received |
| 1127 |
*/ |
| 1128 |
static void ClientParseDetaching(const CInitMessage *msg) |
| 1129 |
{ |
| 1130 |
switch(msg->SubType) { |
| 1131 |
|
| 1132 |
case ICMGoodBye: // Server has let us go |
| 1133 |
NetLocalState = ccs_disconnected; |
| 1134 |
NetStateMsgCnt = 0; |
| 1135 |
break; |
| 1136 |
|
| 1137 |
default: |
| 1138 |
DebugPrint("ccs_detaching: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1139 |
break; |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
/** |
| 1144 |
** Parse a network menu packet in client connecting state. |
| 1145 |
** |
| 1146 |
** @param msg message received |
| 1147 |
*/ |
| 1148 |
static void ClientParseConnecting(const CInitMessage *msg) |
| 1149 |
{ |
| 1150 |
int i; |
| 1151 |
|
| 1152 |
switch(msg->SubType) { |
| 1153 |
|
| 1154 |
case ICMEngineMismatch: // Stratagus engine version doesn't match |
| 1155 |
fprintf(stderr, "Incompatible Bos Wars version " |
| 1156 |
StratagusFormatString " <-> " |
| 1157 |
StratagusFormatString "\n" |
| 1158 |
"from %d.%d.%d.%d:%d\n", |
| 1159 |
StratagusFormatArgs((int)ntohl(msg->Stratagus)), |
| 1160 |
StratagusFormatArgs(StratagusVersion), |
| 1161 |
NIPQUAD(ntohl(NetLastHost)), ntohs(NetLastPort)); |
| 1162 |
NetLocalState = ccs_incompatibleengine; |
| 1163 |
NetConnectRunning = 0; // End the menu.. |
| 1164 |
return; |
| 1165 |
|
| 1166 |
case ICMProtocolMismatch: // Network protocol version doesn't match |
| 1167 |
fprintf(stderr, "Incompatible network protocol version " |
| 1168 |
NetworkProtocolFormatString " <-> " |
| 1169 |
NetworkProtocolFormatString "\n" |
| 1170 |
"from %d.%d.%d.%d:%d\n", |
| 1171 |
NetworkProtocolFormatArgs((int)ntohl(msg->Version)), |
| 1172 |
NetworkProtocolFormatArgs(NetworkProtocolVersion), |
| 1173 |
NIPQUAD(ntohl(NetLastHost)), ntohs(NetLastPort)); |
| 1174 |
NetLocalState = ccs_incompatiblenetwork; |
| 1175 |
NetConnectRunning = 0; // End the menu.. |
| 1176 |
return; |
| 1177 |
|
| 1178 |
case ICMGameFull: // Game is full - server rejected connnection |
| 1179 |
fprintf(stderr, "Server at %d.%d.%d.%d:%d is full!\n", |
| 1180 |
NIPQUAD(ntohl(NetLastHost)), ntohs(NetLastPort)); |
| 1181 |
NetLocalState = ccs_nofreeslots; |
| 1182 |
NetConnectRunning = 0; // End the menu.. |
| 1183 |
return; |
| 1184 |
|
| 1185 |
case ICMWelcome: // Server has accepted us |
| 1186 |
NetLocalState = ccs_connected; |
| 1187 |
NetStateMsgCnt = 0; |
| 1188 |
NetLocalHostsSlot = ntohs(msg->u.Hosts[0].PlyNr); |
| 1189 |
memcpy(Hosts[0].PlyName, msg->u.Hosts[0].PlyName, sizeof(Hosts[0].PlyName) - 1); // Name of server player |
| 1190 |
NetworkLag = ntohl(msg->Lag); |
| 1191 |
NetworkUpdates = ntohl(msg->Updates); |
| 1192 |
|
| 1193 |
Hosts[0].Host = NetworkServerIP; |
| 1194 |
Hosts[0].Port = htons(NetworkServerPort); |
| 1195 |
for (i = 1; i < PlayerMax; ++i) { |
| 1196 |
if (i != NetLocalHostsSlot) { |
| 1197 |
Hosts[i].Host = msg->u.Hosts[i].Host; |
| 1198 |
Hosts[i].Port = msg->u.Hosts[i].Port; |
| 1199 |
Hosts[i].PlyNr = ntohs(msg->u.Hosts[i].PlyNr); |
| 1200 |
if (Hosts[i].PlyNr) { |
| 1201 |
memcpy(Hosts[i].PlyName, msg->u.Hosts[i].PlyName, sizeof(Hosts[i].PlyName) - 1); |
| 1202 |
} |
| 1203 |
} else { |
| 1204 |
Hosts[i].PlyNr = i; |
| 1205 |
strncpy_s(Hosts[i].PlyName, |
| 1206 |
sizeof(Hosts[i].PlyName), |
| 1207 |
LocalPlayerName.c_str(), |
| 1208 |
_TRUNCATE); |
| 1209 |
} |
| 1210 |
} |
| 1211 |
break; |
| 1212 |
|
| 1213 |
default: |
| 1214 |
DebugPrint("ccs_connecting: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1215 |
break; |
| 1216 |
|
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
/** |
| 1221 |
** Check if the map name looks safe. |
| 1222 |
** |
| 1223 |
** A map name looks safe when there are no special characters |
| 1224 |
** and no .. or // sequences. This way only real valid |
| 1225 |
** maps from the map directory will be loaded. |
| 1226 |
** |
| 1227 |
** @return true if the map name looks safe. |
| 1228 |
*/ |
| 1229 |
static bool IsSafeMapName(const char *mapname) |
| 1230 |
{ |
| 1231 |
char buf[256]; |
| 1232 |
const char *ch; |
| 1233 |
|
| 1234 |
if (strncpy_s(buf, sizeof(buf), mapname, sizeof(buf)) != 0) { |
| 1235 |
return false; |
| 1236 |
} |
| 1237 |
if (strstr(buf, "..")) { |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
if (strstr(buf, "//")) { |
| 1241 |
return false; |
| 1242 |
} |
| 1243 |
if (buf[0] == '\0') { |
| 1244 |
return false; |
| 1245 |
} |
| 1246 |
ch = buf; |
| 1247 |
while (*ch != 0) { |
| 1248 |
if (!isalnum(*ch) && *ch != '/' && *ch != '.' && *ch != '-' && |
| 1249 |
*ch != '(' && *ch != ')' && *ch != '_') { |
| 1250 |
return false; |
| 1251 |
} |
| 1252 |
ch++; |
| 1253 |
} |
| 1254 |
|
| 1255 |
return true; |
| 1256 |
} |
| 1257 |
|
| 1258 |
/** |
| 1259 |
** Parse a network menu packet in client connected state. |
| 1260 |
** |
| 1261 |
** @param msg message received |
| 1262 |
*/ |
| 1263 |
static void ClientParseConnected(const CInitMessage *msg) |
| 1264 |
{ |
| 1265 |
switch (msg->SubType) { |
| 1266 |
|
| 1267 |
case ICMMap: // Server has sent us new map info |
| 1268 |
{ |
| 1269 |
if (!IsSafeMapName(msg->u.MapPath)) { |
| 1270 |
fprintf(stderr, "Unsecure map name!\n"); |
| 1271 |
NetLocalState = ccs_badmap; |
| 1272 |
break; |
| 1273 |
} |
| 1274 |
NetworkMapName = std::string(msg->u.MapPath, sizeof(msg->u.MapPath)); |
| 1275 |
std::string mappath = StratagusLibPath + "/" + NetworkMapName; |
| 1276 |
LoadStratagusMapInfo(mappath); |
| 1277 |
if (ntohl(msg->MapUID) != Map.Info.MapUID) { |
| 1278 |
NetLocalState = ccs_badmap; |
| 1279 |
fprintf(stderr, "Maps do not match (0x%08x) <-> (0x%08x)\n", |
| 1280 |
(unsigned int)Map.Info.MapUID, |
| 1281 |
(unsigned int)ntohl(msg->MapUID)); |
| 1282 |
break; |
| 1283 |
} |
| 1284 |
NetLocalState = ccs_mapinfo; |
| 1285 |
NetStateMsgCnt = 0; |
| 1286 |
break; |
| 1287 |
} |
| 1288 |
|
| 1289 |
case ICMWelcome: // Server has accepted us (dup) |
| 1290 |
break; |
| 1291 |
|
| 1292 |
default: |
| 1293 |
DebugPrint("ccs_connected: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1294 |
break; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
/** |
| 1299 |
** Parse a network menu packet in client initial mapinfo state. |
| 1300 |
** |
| 1301 |
** @param msg message received |
| 1302 |
*/ |
| 1303 |
static void ClientParseMapInfo(const CInitMessage *msg) |
| 1304 |
{ |
| 1305 |
switch(msg->SubType) { |
| 1306 |
|
| 1307 |
case ICMState: // Server has sent us first state info |
| 1308 |
ServerSetupState = msg->u.State; |
| 1309 |
NetLocalState = ccs_synced; |
| 1310 |
NetStateMsgCnt = 0; |
| 1311 |
break; |
| 1312 |
|
| 1313 |
default: |
| 1314 |
DebugPrint("ccs_mapinfo: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
/** |
| 1320 |
** Parse a network menu packet in client synced state. |
| 1321 |
** |
| 1322 |
** @param msg message received |
| 1323 |
*/ |
| 1324 |
static void ClientParseSynced(const CInitMessage *msg) |
| 1325 |
{ |
| 1326 |
int i; |
| 1327 |
|
| 1328 |
switch(msg->SubType) { |
| 1329 |
|
| 1330 |
case ICMState: // Server has sent us new state info |
| 1331 |
DebugPrint("ccs_synced: ICMState recieved\n"); |
| 1332 |
ServerSetupState = msg->u.State; |
| 1333 |
NetLocalState = ccs_async; |
| 1334 |
NetStateMsgCnt = 0; |
| 1335 |
break; |
| 1336 |
|
| 1337 |
case ICMConfig: // Server gives the go ahead.. - start game |
| 1338 |
DebugPrint("ccs_synced: Config subtype %d received - starting\n" _C_ msg->SubType); |
| 1339 |
HostsCount = 0; |
| 1340 |
for (i = 0; i < msg->HostsCount - 1; ++i) { |
| 1341 |
if (msg->u.Hosts[i].Host || msg->u.Hosts[i].Port) { |
| 1342 |
Hosts[HostsCount].Host = msg->u.Hosts[i].Host; |
| 1343 |
Hosts[HostsCount].Port = msg->u.Hosts[i].Port; |
| 1344 |
Hosts[HostsCount].PlyNr = ntohs(msg->u.Hosts[i].PlyNr); |
| 1345 |
memcpy(Hosts[HostsCount].PlyName, msg->u.Hosts[i].PlyName, sizeof(Hosts[HostsCount].PlyName) - 1); |
| 1346 |
HostsCount++; |
| 1347 |
DebugPrint("Client %d = %d.%d.%d.%d:%d [%.*s]\n" _C_ |
| 1348 |
ntohs(ntohs(msg->u.Hosts[i].PlyNr)) _C_ NIPQUAD(ntohl(msg->u.Hosts[i].Host)) _C_ |
| 1349 |
ntohs(msg->u.Hosts[i].Port) _C_ |
| 1350 |
static_cast<int>(sizeof(msg->u.Hosts[i].PlyName)) _C_ |
| 1351 |
msg->u.Hosts[i].PlyName); |
| 1352 |
} else { // Own client |
| 1353 |
NetLocalPlayerNumber = ntohs(msg->u.Hosts[i].PlyNr); |
| 1354 |
DebugPrint("SELF %d [%.*s]\n" _C_ ntohs(msg->u.Hosts[i].PlyNr) _C_ |
| 1355 |
static_cast<int>(sizeof(msg->u.Hosts[i].PlyName)) _C_ |
| 1356 |
msg->u.Hosts[i].PlyName); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
// server is last: |
| 1360 |
Hosts[HostsCount].Host = NetLastHost; |
| 1361 |
Hosts[HostsCount].Port = NetLastPort; |
| 1362 |
Hosts[HostsCount].PlyNr = ntohs(msg->u.Hosts[i].PlyNr); |
| 1363 |
memcpy(Hosts[HostsCount].PlyName, msg->u.Hosts[i].PlyName, sizeof(Hosts[HostsCount].PlyName) - 1); |
| 1364 |
++HostsCount; |
| 1365 |
NetPlayers = HostsCount + 1; |
| 1366 |
DebugPrint("Server %d = %d.%d.%d.%d:%d [%.*s]\n" _C_ |
| 1367 |
ntohs(msg->u.Hosts[i].PlyNr) _C_ NIPQUAD(ntohl(NetLastHost)) _C_ |
| 1368 |
ntohs(NetLastPort) _C_ |
| 1369 |
static_cast<int>(sizeof(msg->u.Hosts[i].PlyName)) _C_ |
| 1370 |
msg->u.Hosts[i].PlyName); |
| 1371 |
|
| 1372 |
// put ourselves to the end, like on the server.. |
| 1373 |
Hosts[HostsCount].Host = 0; |
| 1374 |
Hosts[HostsCount].Port = 0; |
| 1375 |
Hosts[HostsCount].PlyNr = NetLocalPlayerNumber; |
| 1376 |
strncpy_s(Hosts[HostsCount].PlyName, |
| 1377 |
sizeof(Hosts[HostsCount].PlyName), |
| 1378 |
LocalPlayerName.c_str(), _TRUNCATE); |
| 1379 |
|
| 1380 |
NetLocalState = ccs_goahead; |
| 1381 |
NetStateMsgCnt = 0; |
| 1382 |
break; |
| 1383 |
|
| 1384 |
default: |
| 1385 |
DebugPrint("ccs_synced: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1386 |
break; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
/** |
| 1391 |
** Parse a network menu packet in client async state. |
| 1392 |
** |
| 1393 |
** @param msg message received |
| 1394 |
*/ |
| 1395 |
static void ClientParseAsync(const CInitMessage *msg) |
| 1396 |
{ |
| 1397 |
int i; |
| 1398 |
|
| 1399 |
switch(msg->SubType) { |
| 1400 |
|
| 1401 |
case ICMResync: // Server has resynced with us and sends resync data |
| 1402 |
DebugPrint("ccs_async: ICMResync\n"); |
| 1403 |
for (i = 1; i < PlayerMax - 1; ++i) { |
| 1404 |
if (i != NetLocalHostsSlot) { |
| 1405 |
Hosts[i].Host = msg->u.Hosts[i].Host; |
| 1406 |
Hosts[i].Port = msg->u.Hosts[i].Port; |
| 1407 |
Hosts[i].PlyNr = ntohs(msg->u.Hosts[i].PlyNr); |
| 1408 |
if (Hosts[i].PlyNr) { |
| 1409 |
memcpy(Hosts[i].PlyName, msg->u.Hosts[i].PlyName, sizeof(Hosts[i].PlyName) - 1); |
| 1410 |
} |
| 1411 |
} else { |
| 1412 |
Hosts[i].PlyNr = ntohs(msg->u.Hosts[i].PlyNr); |
| 1413 |
strncpy_s(Hosts[i].PlyName, |
| 1414 |
sizeof(Hosts[i].PlyName), |
| 1415 |
LocalPlayerName.c_str(), |
| 1416 |
_TRUNCATE); |
| 1417 |
} |
| 1418 |
} |
| 1419 |
NetLocalState = ccs_synced; |
| 1420 |
NetStateMsgCnt = 0; |
| 1421 |
break; |
| 1422 |
|
| 1423 |
default: |
| 1424 |
DebugPrint("ccs_async: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1425 |
break; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
|
| 1429 |
/** |
| 1430 |
** Parse a network menu packet in client final goahead waiting state. |
| 1431 |
** |
| 1432 |
** @param msg message received |
| 1433 |
*/ |
| 1434 |
static void ClientParseGoAhead(const CInitMessage *msg) |
| 1435 |
{ |
| 1436 |
switch(msg->SubType) { |
| 1437 |
|
| 1438 |
case ICMConfig: // Server go ahead dup - ignore.. |
| 1439 |
break; |
| 1440 |
|
| 1441 |
case ICMState: // Server has sent final state info |
| 1442 |
DebugPrint("ccs_goahead: Final State subtype %d received - starting\n" _C_ msg->SubType); |
| 1443 |
ServerSetupState = msg->u.State; |
| 1444 |
NetLocalState = ccs_started; |
| 1445 |
NetStateMsgCnt = 0; |
| 1446 |
break; |
| 1447 |
|
| 1448 |
default: |
| 1449 |
DebugPrint("ccs_goahead: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1450 |
break; |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
/** |
| 1455 |
** Parse a network menu packet in client final started state |
| 1456 |
** |
| 1457 |
** @param msg message received |
| 1458 |
*/ |
| 1459 |
static void ClientParseStarted(const CInitMessage *msg) |
| 1460 |
{ |
| 1461 |
switch(msg->SubType) { |
| 1462 |
|
| 1463 |
case ICMGo: // Server's final go .. |
| 1464 |
DebugPrint("ClientParseStarted ICMGo !!!!!\n"); |
| 1465 |
NetConnectRunning = 0; // End the menu.. |
| 1466 |
break; |
| 1467 |
|
| 1468 |
default: |
| 1469 |
DebugPrint("ccs_started: Unhandled subtype %d\n" _C_ msg->SubType); |
| 1470 |
break; |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
/** |
| 1475 |
** Parse a network menu AreYouThere keepalive packet and reply IAmHere. |
| 1476 |
** |
| 1477 |
** @param msg message received |
| 1478 |
*/ |
| 1479 |
static void ClientParseAreYouThere(const CInitMessage *msg) |
| 1480 |
{ |
| 1481 |
CInitMessage message; |
| 1482 |
|
| 1483 |
message.Type = MessageInitHello; |
| 1484 |
message.SubType = ICMIAH; |
| 1485 |
NetworkSendICMessage(NetworkServerIP, htons(NetworkServerPort), &message); |
| 1486 |
} |
| 1487 |
|
| 1488 |
/** |
| 1489 |
** Parse a network menu Bad Map reply from server. |
| 1490 |
** |
| 1491 |
** @param msg message received |
| 1492 |
*/ |
| 1493 |
static void ClientParseBadMap(const CInitMessage *msg) |
| 1494 |
{ |
| 1495 |
int i; |
| 1496 |
CInitMessage message; |
| 1497 |
|
| 1498 |
message.Type = MessageInitHello; |
| 1499 |
message.SubType = ICMSeeYou; |
| 1500 |
// Spew out 5 and trust in God that they arrive |
| 1501 |
for (i = 0; i < 5; ++i) { |
| 1502 |
NetworkSendICMessage(NetworkServerIP, htons(NetworkServerPort), &message); |
| 1503 |
} |
| 1504 |
NetConnectRunning = 0; // End the menu.. |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
/** |
| 1509 |
** Parse the initial 'Hello' message of new client that wants to join the game |
| 1510 |
** |
| 1511 |
** @param h slot number of host msg originates from |
| 1512 |
** @param msg message received |
| 1513 |
*/ |
| 1514 |
static void ServerParseHello(int h, const CInitMessage *msg) |
| 1515 |
{ |
| 1516 |
int i, n; |
| 1517 |
CInitMessage message; |
| 1518 |
|
| 1519 |
if (h == -1) { // it is a new client |
| 1520 |
for (i = 1; i < PlayerMax - 1; ++i) { |
| 1521 |
// occupy first available slot |
| 1522 |
if (ServerSetupState.CompOpt[i] == 0) { |
| 1523 |
if (Hosts[i].PlyNr == 0) { |
| 1524 |
h = i; |
| 1525 |
break; |
| 1526 |
} |
| 1527 |
} |
| 1528 |
} |
| 1529 |
if (h != -1) { |
| 1530 |
Hosts[h].Host = NetLastHost; |
| 1531 |
Hosts[h].Port = NetLastPort; |
| 1532 |
Hosts[h].PlyNr = h; |
| 1533 |
memcpy(Hosts[h].PlyName, msg->u.Hosts[0].PlyName, sizeof(Hosts[h].PlyName) - 1); |
| 1534 |
DebugPrint("New client %d.%d.%d.%d:%d [%s]\n" _C_ |
| 1535 |
NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort) _C_ Hosts[h].PlyName); |
| 1536 |
NetStates[h].State = ccs_connecting; |
| 1537 |
NetStates[h].MsgCnt = 0; |
| 1538 |
} else { |
| 1539 |
message.Type = MessageInitReply; |
| 1540 |
message.SubType = ICMGameFull; // Game is full - reject connnection |
| 1541 |
message.MapUID = 0L; |
| 1542 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1543 |
DebugPrint("Sending InitReply Message GameFull: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1544 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1545 |
return; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
// this code path happens until client sends waiting (= has received this message) |
| 1549 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1550 |
message.Type = MessageInitReply; |
| 1551 |
message.SubType = ICMWelcome; // Acknowledge: Client is welcome |
| 1552 |
message.u.Hosts[0].PlyNr = htons(h); // Host array slot number |
| 1553 |
strncpy_s(message.u.Hosts[0].PlyName, sizeof(message.u.Hosts[0].PlyName), |
| 1554 |
LocalPlayerName.c_str(), _TRUNCATE); // Name of server player |
| 1555 |
message.MapUID = 0L; |
| 1556 |
for (i = 1; i < PlayerMax - 1; ++i) { // Info about other clients |
| 1557 |
if (i != h) { |
| 1558 |
if (Hosts[i].PlyNr) { |
| 1559 |
message.u.Hosts[i].Host = Hosts[i].Host; |
| 1560 |
message.u.Hosts[i].Port = Hosts[i].Port; |
| 1561 |
message.u.Hosts[i].PlyNr = htons(Hosts[i].PlyNr); |
| 1562 |
memcpy(message.u.Hosts[i].PlyName, Hosts[i].PlyName, sizeof(message.u.Hosts[i].PlyName) - 1); |
| 1563 |
} else { |
| 1564 |
message.u.Hosts[i].Host = 0; |
| 1565 |
message.u.Hosts[i].Port = 0; |
| 1566 |
message.u.Hosts[i].PlyNr = 0; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
} |
| 1570 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1571 |
DebugPrint("Sending InitReply Message Welcome: (%d) [PlyNr: %d] to %d.%d.%d.%d:%d\n" _C_ |
| 1572 |
n _C_ h _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1573 |
NetStates[h].MsgCnt++; |
| 1574 |
if (NetStates[h].MsgCnt > 48) { |
| 1575 |
// Detects UDP input firewalled or behind NAT firewall clients |
| 1576 |
// If packets are missed, clients are kicked by AYT check later.. |
| 1577 |
KickDeadClient(h); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
/** |
| 1582 |
** Parse client resync request after client user has changed menu selection |
| 1583 |
** |
| 1584 |
** @param h slot number of host msg originates from |
| 1585 |
*/ |
| 1586 |
static void ServerParseResync(const int h) |
| 1587 |
{ |
| 1588 |
int i; |
| 1589 |
int n; |
| 1590 |
CInitMessage message; |
| 1591 |
|
| 1592 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1593 |
switch (NetStates[h].State) { |
| 1594 |
case ccs_mapinfo: |
| 1595 |
// a delayed ack - fall through.. |
| 1596 |
case ccs_async: |
| 1597 |
// client has recvd welcome and is waiting for info |
| 1598 |
NetStates[h].State = ccs_synced; |
| 1599 |
NetStates[h].MsgCnt = 0; |
| 1600 |
/* Fall through */ |
| 1601 |
case ccs_synced: |
| 1602 |
// this code path happens until client falls back to ICMWaiting |
| 1603 |
// (indicating Resync has completed) |
| 1604 |
message.Type = MessageInitReply; |
| 1605 |
message.SubType = ICMResync; |
| 1606 |
for (i = 1; i < PlayerMax - 1; ++i) { // Info about other clients |
| 1607 |
if (i != h) { |
| 1608 |
if (Hosts[i].PlyNr) { |
| 1609 |
message.u.Hosts[i].Host = Hosts[i].Host; |
| 1610 |
message.u.Hosts[i].Port = Hosts[i].Port; |
| 1611 |
message.u.Hosts[i].PlyNr = htons(Hosts[i].PlyNr); |
| 1612 |
memcpy(message.u.Hosts[i].PlyName, Hosts[i].PlyName, sizeof(message.u.Hosts[i].PlyName) - 1); |
| 1613 |
} else { |
| 1614 |
message.u.Hosts[i].Host = 0; |
| 1615 |
message.u.Hosts[i].Port = 0; |
| 1616 |
message.u.Hosts[i].PlyNr = 0; |
| 1617 |
} |
| 1618 |
} |
| 1619 |
} |
| 1620 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1621 |
DebugPrint("Sending InitReply Message Resync: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1622 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1623 |
NetStates[h].MsgCnt++; |
| 1624 |
if (NetStates[h].MsgCnt > 50) { |
| 1625 |
// FIXME: Client sends resync, but doesn't receive our resync ack.... |
| 1626 |
; |
| 1627 |
} |
| 1628 |
break; |
| 1629 |
|
| 1630 |
default: |
| 1631 |
DebugPrint("Server: ICMResync: Unhandled state %d Host %d\n" _C_ |
| 1632 |
NetStates[h].State _C_ h); |
| 1633 |
break; |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
/** |
| 1639 |
** Parse client heart beat waiting message |
| 1640 |
** |
| 1641 |
** @param h slot number of host msg originates from |
| 1642 |
*/ |
| 1643 |
static void ServerParseWaiting(const int h) |
| 1644 |
{ |
| 1645 |
int i; |
| 1646 |
int n; |
| 1647 |
CInitMessage message; |
| 1648 |
|
| 1649 |
// Don't leave uninitialized data at the end of message.u.MapPath, |
| 1650 |
// in case it might contain passwords or similar. |
| 1651 |
memset(&message, 0, sizeof(message)); |
| 1652 |
|
| 1653 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1654 |
|
| 1655 |
switch (NetStates[h].State) { |
| 1656 |
// client has recvd welcome and is waiting for info |
| 1657 |
case ccs_connecting: |
| 1658 |
NetStates[h].State = ccs_connected; |
| 1659 |
NetStates[h].MsgCnt = 0; |
| 1660 |
/* Fall through */ |
| 1661 |
case ccs_connected: |
| 1662 |
// this code path happens until client acknowledges the map |
| 1663 |
message.Type = MessageInitReply; |
| 1664 |
message.SubType = ICMMap; // Send Map info to the client |
| 1665 |
strncpy_s(message.u.MapPath, sizeof(message.u.MapPath), NetworkMapName.c_str(), NetworkMapName.size()); |
| 1666 |
message.MapUID = htonl(Map.Info.MapUID); |
| 1667 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1668 |
DebugPrint("Sending InitReply Message Map: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1669 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1670 |
NetStates[h].MsgCnt++; |
| 1671 |
if (NetStates[h].MsgCnt > 50) { |
| 1672 |
// FIXME: Client sends waiting, but doesn't receive our map.... |
| 1673 |
; |
| 1674 |
} |
| 1675 |
break; |
| 1676 |
case ccs_mapinfo: |
| 1677 |
NetStates[h].State = ccs_synced; |
| 1678 |
NetStates[h].MsgCnt = 0; |
| 1679 |
for (i = 1; i < PlayerMax - 1; ++i) { |
| 1680 |
if (i != h && Hosts[i].PlyNr) { |
| 1681 |
// Notify other clients |
| 1682 |
NetStates[i].State = ccs_async; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
/* Fall through */ |
| 1686 |
case ccs_synced: |
| 1687 |
// the wanted state - do nothing.. until start... |
| 1688 |
NetStates[h].MsgCnt = 0; |
| 1689 |
break; |
| 1690 |
|
| 1691 |
case ccs_async: |
| 1692 |
// Server User has changed menu selection. This state is set by MENU code |
| 1693 |
// OR we have received a new client/other client has changed data |
| 1694 |
|
| 1695 |
// this code path happens until client acknoledges the state change |
| 1696 |
// by sending ICMResync |
| 1697 |
message.Type = MessageInitReply; |
| 1698 |
message.SubType = ICMState; // Send new state info to the client |
| 1699 |
message.u.State = ServerSetupState; |
| 1700 |
message.MapUID = htonl(Map.Info.MapUID); |
| 1701 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1702 |
DebugPrint("Sending InitReply Message State: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1703 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1704 |
NetStates[h].MsgCnt++; |
| 1705 |
if (NetStates[h].MsgCnt > 50) { |
| 1706 |
// FIXME: Client sends waiting, but doesn't receive our state info.... |
| 1707 |
; |
| 1708 |
} |
| 1709 |
break; |
| 1710 |
|
| 1711 |
default: |
| 1712 |
DebugPrint("Server: ICMWaiting: Unhandled state %d Host %d\n" _C_ |
| 1713 |
NetStates[h].State _C_ h); |
| 1714 |
break; |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
/** |
| 1719 |
** Parse client map info acknoledge message |
| 1720 |
** |
| 1721 |
** @param h slot number of host msg originates from |
| 1722 |
*/ |
| 1723 |
static void ServerParseMap(const int h) |
| 1724 |
{ |
| 1725 |
int n; |
| 1726 |
CInitMessage message; |
| 1727 |
|
| 1728 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1729 |
switch (NetStates[h].State) { |
| 1730 |
// client has recvd map info waiting for state info |
| 1731 |
case ccs_connected: |
| 1732 |
NetStates[h].State = ccs_mapinfo; |
| 1733 |
NetStates[h].MsgCnt = 0; |
| 1734 |
/* Fall through */ |
| 1735 |
case ccs_mapinfo: |
| 1736 |
// this code path happens until client acknoledges the state info |
| 1737 |
// by falling back to ICMWaiting with prev. State synced |
| 1738 |
message.Type = MessageInitReply; |
| 1739 |
message.SubType = ICMState; // Send State info to the client |
| 1740 |
message.u.State = ServerSetupState; |
| 1741 |
message.MapUID = htonl(Map.Info.MapUID); |
| 1742 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1743 |
DebugPrint("Sending InitReply Message State: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1744 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1745 |
NetStates[h].MsgCnt++; |
| 1746 |
if (NetStates[h].MsgCnt > 50) { |
| 1747 |
// FIXME: Client sends mapinfo, but doesn't receive our state info.... |
| 1748 |
; |
| 1749 |
} |
| 1750 |
break; |
| 1751 |
default: |
| 1752 |
DebugPrint("Server: ICMMap: Unhandled state %d Host %d\n" _C_ |
| 1753 |
NetStates[h].State _C_ h); |
| 1754 |
break; |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
/** |
| 1759 |
** Parse locate state change notifiction or initial state info request of client |
| 1760 |
** |
| 1761 |
** @param h slot number of host msg originates from |
| 1762 |
** @param msg message received |
| 1763 |
*/ |
| 1764 |
static void ServerParseState(const int h, const CInitMessage *msg) |
| 1765 |
{ |
| 1766 |
int i; |
| 1767 |
int n; |
| 1768 |
CInitMessage message; |
| 1769 |
|
| 1770 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1771 |
switch (NetStates[h].State) { |
| 1772 |
case ccs_mapinfo: |
| 1773 |
// User State Change right after connect - should not happen, but.. |
| 1774 |
/* Fall through */ |
| 1775 |
case ccs_synced: |
| 1776 |
// Default case: Client is in sync with us, but notes a local change |
| 1777 |
// NetStates[h].State = ccs_async; |
| 1778 |
NetStates[h].MsgCnt = 0; |
| 1779 |
// Use information supplied by the client: |
| 1780 |
ServerSetupState.Ready[h] = msg->u.State.Ready[h]; |
| 1781 |
// Add additional info usage here! |
| 1782 |
|
| 1783 |
// Resync other clients (and us..) |
| 1784 |
for (i = 1; i < PlayerMax - 1; ++i) { |
| 1785 |
if (Hosts[i].PlyNr) { |
| 1786 |
NetStates[i].State = ccs_async; |
| 1787 |
} |
| 1788 |
} |
| 1789 |
/* Fall through */ |
| 1790 |
case ccs_async: |
| 1791 |
// this code path happens until client acknoledges the state change reply |
| 1792 |
// by sending ICMResync |
| 1793 |
message.Type = MessageInitReply; |
| 1794 |
message.SubType = ICMState; // Send new state info to the client |
| 1795 |
message.u.State = ServerSetupState; |
| 1796 |
message.MapUID = htonl(Map.Info.MapUID); |
| 1797 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1798 |
DebugPrint("Sending InitReply Message State: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1799 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1800 |
NetStates[h].MsgCnt++; |
| 1801 |
if (NetStates[h].MsgCnt > 50) { |
| 1802 |
// FIXME: Client sends State, but doesn't receive our state info.... |
| 1803 |
; |
| 1804 |
} |
| 1805 |
break; |
| 1806 |
default: |
| 1807 |
DebugPrint("Server: ICMState: Unhandled state %d Host %d\n" _C_ |
| 1808 |
NetStates[h].State _C_ h); |
| 1809 |
break; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
/** |
| 1814 |
** Parse the disconnect request of a client by sending out good bye |
| 1815 |
** |
| 1816 |
** @param h slot number of host msg originates from |
| 1817 |
*/ |
| 1818 |
static void ServerParseGoodBye(const int h) |
| 1819 |
{ |
| 1820 |
int n; |
| 1821 |
CInitMessage message; |
| 1822 |
|
| 1823 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1824 |
switch (NetStates[h].State) { |
| 1825 |
default: |
| 1826 |
// We can enter here from _ANY_ state! |
| 1827 |
NetStates[h].MsgCnt = 0; |
| 1828 |
NetStates[h].State = ccs_detaching; |
| 1829 |
/* Fall through */ |
| 1830 |
case ccs_detaching: |
| 1831 |
// this code path happens until client acknoledges the GoodBye |
| 1832 |
// by sending ICMSeeYou; |
| 1833 |
message.Type = MessageInitReply; |
| 1834 |
message.SubType = ICMGoodBye; |
| 1835 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1836 |
DebugPrint("Sending InitReply Message GoodBye: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1837 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1838 |
NetStates[h].MsgCnt++; |
| 1839 |
if (NetStates[h].MsgCnt > 10) { |
| 1840 |
// FIXME: Client sends GoodBye, but doesn't receive our GoodBye.... |
| 1841 |
; |
| 1842 |
} |
| 1843 |
break; |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
/** |
| 1848 |
** Parse the final see you msg of a disconnecting client |
| 1849 |
** |
| 1850 |
** @param h slot number of host msg originates from |
| 1851 |
*/ |
| 1852 |
static void ServerParseSeeYou(const int h) |
| 1853 |
{ |
| 1854 |
switch (NetStates[h].State) { |
| 1855 |
case ccs_detaching: |
| 1856 |
KickDeadClient(h); |
| 1857 |
break; |
| 1858 |
|
| 1859 |
default: |
| 1860 |
DebugPrint("Server: ICMSeeYou: Unhandled state %d Host %d\n" _C_ |
| 1861 |
NetStates[h].State _C_ h); |
| 1862 |
break; |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
/** |
| 1867 |
** Parse the 'I am Here' reply to the servers' 'Are you there' msg |
| 1868 |
** |
| 1869 |
** @param h slot number of host msg originates from |
| 1870 |
*/ |
| 1871 |
static void ServerParseIAmHere(const int h) |
| 1872 |
{ |
| 1873 |
// client found us again - update timestamp |
| 1874 |
ServerSetupState.LastFrame[h] = FrameCounter; |
| 1875 |
} |
| 1876 |
|
| 1877 |
/** |
| 1878 |
** Check if the Stratagus version and Network Protocol match |
| 1879 |
** |
| 1880 |
** @param msg message received |
| 1881 |
** |
| 1882 |
** @return 0 if the versions match, -1 otherwise |
| 1883 |
*/ |
| 1884 |
static int CheckVersions(const CInitMessage *msg) |
| 1885 |
{ |
| 1886 |
int n; |
| 1887 |
CInitMessage message; |
| 1888 |
|
| 1889 |
if (ntohl(msg->Stratagus) != StratagusVersion) { |
| 1890 |
fprintf(stderr, "Incompatible Bos Wars version " |
| 1891 |
StratagusFormatString " <-> " |
| 1892 |
StratagusFormatString "\n" |
| 1893 |
"from %d.%d.%d.%d:%d\n", |
| 1894 |
StratagusFormatArgs((int)ntohl(msg->Stratagus)), |
| 1895 |
StratagusFormatArgs(StratagusVersion), |
| 1896 |
NIPQUAD(ntohl(NetLastHost)),ntohs(NetLastPort)); |
| 1897 |
|
| 1898 |
message.Type = MessageInitReply; |
| 1899 |
message.SubType = ICMEngineMismatch; // Stratagus engine version doesn't match |
| 1900 |
message.MapUID = 0L; |
| 1901 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1902 |
DebugPrint("Sending InitReply Message EngineMismatch: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1903 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1904 |
return -1; |
| 1905 |
} |
| 1906 |
|
| 1907 |
if (ntohl(msg->Version) != NetworkProtocolVersion) { |
| 1908 |
fprintf(stderr, "Incompatible network protocol version " |
| 1909 |
NetworkProtocolFormatString " <-> " |
| 1910 |
NetworkProtocolFormatString "\n" |
| 1911 |
"from %d.%d.%d.%d:%d\n", |
| 1912 |
NetworkProtocolFormatArgs((int)ntohl(msg->Version)), |
| 1913 |
NetworkProtocolFormatArgs(NetworkProtocolVersion), |
| 1914 |
NIPQUAD(ntohl(NetLastHost)),ntohs(NetLastPort)); |
| 1915 |
|
| 1916 |
message.Type = MessageInitReply; |
| 1917 |
message.SubType = ICMProtocolMismatch; // Network protocol version doesn't match |
| 1918 |
message.MapUID = 0L; |
| 1919 |
n = NetworkSendICMessage(NetLastHost, NetLastPort, &message); |
| 1920 |
DebugPrint("Sending InitReply Message ProtocolMismatch: (%d) to %d.%d.%d.%d:%d\n" _C_ |
| 1921 |
n _C_ NIPQUAD(ntohl(NetLastHost)) _C_ ntohs(NetLastPort)); |
| 1922 |
return -1; |
| 1923 |
} |
| 1924 |
|
| 1925 |
return 0; |
| 1926 |
} |
| 1927 |
|
| 1928 |
/** |
| 1929 |
** Parse a Network menu packet. |
| 1930 |
** |
| 1931 |
** @param msg message received |
| 1932 |
*/ |
| 1933 |
static void NetworkParseMenuPacket(const CInitMessage *msg) |
| 1934 |
{ |
| 1935 |
DebugPrint("Received %s Init Message %d:%d from %d.%d.%d.%d:%d (%ld)\n" _C_ |
| 1936 |
icmsgsubtypenames[msg->SubType] _C_ msg->Type _C_ msg->SubType _C_ NIPQUAD(ntohl(NetLastHost)) _C_ |
| 1937 |
ntohs(NetLastPort) _C_ FrameCounter); |
| 1938 |
|
| 1939 |
if (NetConnectRunning == 2) { // client |
| 1940 |
if (msg->Type == MessageInitReply) { |
| 1941 |
if (msg->SubType == ICMServerQuit) { // Server user canceled, should work in all states |
| 1942 |
NetLocalState = ccs_serverquits; |
| 1943 |
NetConnectRunning = 0; // End the menu.. |
| 1944 |
// No ack here - Server will spew out a few Quit msgs, which has to be enough |
| 1945 |
return; |
| 1946 |
} |
| 1947 |
if (msg->SubType == ICMAYT) { // Server is checking for our presence |
| 1948 |
ClientParseAreYouThere(msg); |
| 1949 |
return; |
| 1950 |
} |
| 1951 |
switch(NetLocalState) { |
| 1952 |
case ccs_disconnected: |
| 1953 |
#ifdef DEBUG |
| 1954 |
ClientParseDisconnected(msg); |
| 1955 |
#endif |
| 1956 |
break; |
| 1957 |
|
| 1958 |
case ccs_detaching: |
| 1959 |
ClientParseDetaching(msg); |
| 1960 |
break; |
| 1961 |
|
| 1962 |
case ccs_connecting: |
| 1963 |
ClientParseConnecting(msg); |
| 1964 |
break; |
| 1965 |
|
| 1966 |
case ccs_connected: |
| 1967 |
ClientParseConnected(msg); |
| 1968 |
break; |
| 1969 |
|
| 1970 |
case ccs_mapinfo: |
| 1971 |
ClientParseMapInfo(msg); |
| 1972 |
break; |
| 1973 |
|
| 1974 |
case ccs_changed: |
| 1975 |
case ccs_synced: |
| 1976 |
ClientParseSynced(msg); |
| 1977 |
break; |
| 1978 |
|
| 1979 |
case ccs_async: |
| 1980 |
ClientParseAsync(msg); |
| 1981 |
break; |
| 1982 |
|
| 1983 |
case ccs_goahead: |
| 1984 |
ClientParseGoAhead(msg); |
| 1985 |
break; |
| 1986 |
|
| 1987 |
case ccs_badmap: |
| 1988 |
ClientParseBadMap(msg); |
| 1989 |
break; |
| 1990 |
|
| 1991 |
case ccs_started: |
| 1992 |
ClientParseStarted(msg); |
| 1993 |
break; |
| 1994 |
|
| 1995 |
default: |
| 1996 |
DebugPrint("Client: Unhandled state %d\n" _C_ NetLocalState); |
| 1997 |
break; |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
} else if (NetConnectRunning == 1) { // server |
| 2002 |
int i; |
| 2003 |
|
| 2004 |
if (CheckVersions(msg)) { |
| 2005 |
return; |
| 2006 |
} |
| 2007 |
|
| 2008 |
// look up the host - avoid last player slot (15) which is special |
| 2009 |
for (i = 0; i < PlayerMax - 1; ++i) { |
| 2010 |
if (Hosts[i].Host == NetLastHost && Hosts[i].Port == NetLastPort) { |
| 2011 |
switch(msg->SubType) { |
| 2012 |
case ICMHello: // a new client has arrived |
| 2013 |
ServerParseHello(i, msg); |
| 2014 |
break; |
| 2015 |
|
| 2016 |
case ICMResync: |
| 2017 |
ServerParseResync(i); |
| 2018 |
break; |
| 2019 |
|
| 2020 |
case ICMWaiting: |
| 2021 |
ServerParseWaiting(i); |
| 2022 |
break; |
| 2023 |
|
| 2024 |
case ICMMap: |
| 2025 |
ServerParseMap(i); |
| 2026 |
break; |
| 2027 |
|
| 2028 |
case ICMState: |
| 2029 |
ServerParseState(i, msg); |
| 2030 |
break; |
| 2031 |
|
| 2032 |
case ICMMapUidMismatch: |
| 2033 |
case ICMGoodBye: |
| 2034 |
ServerParseGoodBye(i); |
| 2035 |
break; |
| 2036 |
|
| 2037 |
case ICMSeeYou: |
| 2038 |
ServerParseSeeYou(i); |
| 2039 |
break; |
| 2040 |
|
| 2041 |
case ICMIAH: |
| 2042 |
ServerParseIAmHere(i); |
| 2043 |
break; |
| 2044 |
|
| 2045 |
default: |
| 2046 |
DebugPrint("Server: Unhandled subtype %d from host %d\n" _C_ msg->SubType _C_ i); |
| 2047 |
break; |
| 2048 |
} |
| 2049 |
break; |
| 2050 |
} |
| 2051 |
} |
| 2052 |
if (i == PlayerMax - 1 && msg->SubType == ICMHello) { |
| 2053 |
// Special case: a new client has arrived |
| 2054 |
ServerParseHello(-1, msg); |
| 2055 |
} |
| 2056 |
} |
| 2057 |
} |
| 2058 |
|
| 2059 |
/** |
| 2060 |
** Parse a setup event. (Command type <= MessageInitEvent) |
| 2061 |
** |
| 2062 |
** @param buf Packet received |
| 2063 |
** @param size size of the received packet. |
| 2064 |
** |
| 2065 |
** @return 1 if packet is an InitConfig message, 0 otherwise |
| 2066 |
*/ |
| 2067 |
int NetworkParseSetupEvent(const unsigned char *buf, int size) |
| 2068 |
{ |
| 2069 |
CInitMessage msg; |
| 2070 |
|
| 2071 |
if (size != (int)CInitMessage::Size()) { |
| 2072 |
// FIXME: could be a bad packet |
| 2073 |
if (NetConnectRunning == 2 && NetLocalState == ccs_started) { |
| 2074 |
// Client has acked ready to start and receives first real network packet. |
| 2075 |
// This indicates that we missed the 'Go' in started state and the game |
| 2076 |
// has been started by the server, so do the same for the client. |
| 2077 |
NetConnectRunning = 0; // End the menu.. |
| 2078 |
} |
| 2079 |
return 0; |
| 2080 |
} |
| 2081 |
|
| 2082 |
msg.Deserialize(buf); |
| 2083 |
if (msg.Type > MessageInitConfig) { |
| 2084 |
if (NetConnectRunning == 2 && NetLocalState == ccs_started) { |
| 2085 |
// Client has acked ready to start and receives first real network packet. |
| 2086 |
// This indicates that we missed the 'Go' in started state and the game |
| 2087 |
// has been started by the server, so do the same for the client. |
| 2088 |
NetConnectRunning = 0; // End the menu.. |
| 2089 |
} |
| 2090 |
return 0; |
| 2091 |
} |
| 2092 |
|
| 2093 |
if (InterfaceState == IfaceStateMenu) { |
| 2094 |
NetworkParseMenuPacket(&msg); |
| 2095 |
return 1; |
| 2096 |
} |
| 2097 |
return 0; |
| 2098 |
} |
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
//@} |