private static void ProcessInitializationState(BitStream message) { _svStateID = message.ReadInt32(); // load the server's current map var mapName = message.ReadString(); MapManager.Load(string.Format("Maps/{0}.gmp", mapName)); }
private static void ProcessClientMessage(ServerClient client, BitStream message) { client.LastMessageReceivedAt = _serverTime; client.LastAcknowledgedMessage = message.ReadUInt32(); client.ReliableAcknowledged = message.ReadUInt32(); // check if this command is from an older initialization state var stateID = message.ReadInt32(); if (stateID != _stateID) { if (client.LastAcknowledgedMessage >= client.LastInitStateMessage) { SendInitializationState(client); } return; } byte command = 0; try { do { command = message.ReadByte(); switch (command) { case UserCommand.CommandType: ProcessClientUserCommand(client, message); break; case 2: // TODO: magic number fix ProcessClientReliableCommand(client, message); break; } } while (command != 0xFF); } catch (Exception e) { Log.Write(LogLevel.Warning, "Error while processing client message: " + e.ToString()); DropClient(client, "Error while processing client message."); } }
private static void ProcessSnapshot(BitStream message) { if (State == ClientState.Connected) { State = ClientState.Ingame; } var snapshot = new Snapshot(); snapshot.Sequence = _serverChannel.SequenceIn; snapshot.ServerTime = message.ReadUInt32(); _serverTimeDelta = (int)((int)snapshot.ServerTime - (int)_clientTime); _reliableAcknowledged = message.ReadInt32(); int entityNumber = message.ReadInt32(12); while (entityNumber != 4095) { var entityBase = (_entityBases[entityNumber] != null) ? _entityBases[entityNumber] : null; var deltaMessage = new DeltaBitStream(entityBase, message); var spawnKey = deltaMessage.ReadInt32(20); var typeCode = deltaMessage.ReadInt32(4); snapshot.Entities[entityNumber] = Entity.Create(typeCode); snapshot.Entities[entityNumber].Deserialize(deltaMessage); _entityBases[entityNumber] = deltaMessage.NewBase; entityNumber = message.ReadInt32(12); } // push snapshot history array back by one for (int i = (_snapHistory.Length - 2); i >= 0; i--) { _snapHistory[i + 1] = _snapHistory[i]; } // and set the first snapshot to the current one _snapHistory[0] = snapshot; // define current and previous snapshots int delayMsec = cl_snapDelay.GetValue <int>(); int numSnaps = 0; for (int i = 0; i < _snapHistory.Length; i++) { if (_snapHistory[i] != null) { if (_snapHistory[i].ServerTime < (_snapHistory[0].ServerTime - delayMsec)) { numSnaps = i; break; } } } int snapDelay = Math.Max(0, Math.Min(_snapHistory.Length - 2, numSnaps)); _lastSnap = _snapHistory[snapDelay + 1]; _curSnap = _snapHistory[snapDelay]; if (_curSnap == null) { _curSnap = _snapHistory[0]; } _curSnap.ClientTime = _clientTime; }