Ejemplo n.º 1
0
static void NetworkParseInGameEvent(const unsigned char *buf, int len, const CHost &host)
{
	CNetworkPacket packet;
	int commands;
	packet.Deserialize(buf, len, &commands);

	int player = packet.Header.OrigPlayer;
	if (player == 255) {
		const int index = FindHostIndexBy(host);
		if (index == -1 || PlayerQuit[Hosts[index].PlyNr]) {
#ifdef DEBUG
			const std::string hostStr = host.toString();
			DebugPrint("Not a host in play: %s\n" _C_ hostStr.c_str());
#endif
			return;
		}
		player = Hosts[index].PlyNr;
	}
	if (NetConnectType == 1) {
		if (player != 255) {
			NetworkBroadcast(packet, commands, player);
		}
	}
	if (commands < 0) {
		DebugPrint("Bad packet read\n");
		return;
	}
	NetworkLastCycle[player] = packet.Header.Cycle;
	// Parse the packet commands.
	for (int i = 0; i != commands; ++i) {
		// Handle some messages.
		if (packet.Header.Type[i] == MessageQuit) {
			CNetworkCommandQuit nc;
			nc.Deserialize(&packet.Command[i][0]);
			const int playerNum = nc.player;

			if (playerNum >= 0 && playerNum < NumPlayers) {
				PlayerQuit[playerNum] = 1;
			}
		}
		if (packet.Header.Type[i] == MessageResend) {
			ParseResendCommand(packet);
			return;
		}
		// Receive statistic
		NetworkLastFrame[player] = FrameCounter;

		bool validCommand = IsAValidCommand(packet, i, player);
		// Place in network in
		if (validCommand) {
			// Destination cycle (time to execute).
			unsigned long n = ((GameCycle + 128) & ~0xFF) | packet.Header.Cycle;
			if (n > GameCycle + 128) {
				n -= 0x100;
			}
			NetworkIn[packet.Header.Cycle][player][i].Time = n;
			NetworkIn[packet.Header.Cycle][player][i].Type = packet.Header.Type[i];
			NetworkIn[packet.Header.Cycle][player][i].Data = packet.Command[i];
		} else {
			SetMessage(_("%s sent bad command"), Players[player].Name.c_str());
			DebugPrint("%s sent bad command: 0x%x\n" _C_ Players[player].Name.c_str()
					   _C_ packet.Header.Type[i] & 0x7F);
		}
	}
	for (int i = commands; i != MaxNetworkCommands; ++i) {
		NetworkIn[packet.Header.Cycle][player][i].Time = 0;
	}
	// Waiting for this time slot
	if (!NetworkInSync) {
		const int networkUpdates = CNetworkParameter::Instance.gameCyclesPerUpdate;
		unsigned long n = ((GameCycle / networkUpdates) + 1) * networkUpdates;
		if (IsNetworkCommandReady(n) == true) {
			NetworkInSync = true;
		}
	}
}