public void WriteByte(byte value) { _newBaseStream.WriteByte(value); if (_baseStream != null && value == _baseStream.ReadByte()) { _bitStream.WriteBool(false); } else { _bitStream.WriteBool(true); _bitStream.WriteByte(value); } }
public void Deserialize(BitStream message, UserCommand?old) { if (old == null) { ServerTime = message.ReadUInt32(); Buttons = (ClientButtons)message.ReadInt16(); } else { var oldCommand = old.Value; // read server time if (message.ReadBool()) { // is delta time ServerTime = oldCommand.ServerTime + message.ReadByte(); } else { // is absolute time ServerTime = message.ReadUInt32(); } // read buttons Buttons = (ClientButtons)message.ReadDeltaInt16((short)oldCommand.Buttons); } }
public byte ReadByte() { byte value = (_baseStream == null) ? (byte)0 : _baseStream.ReadByte(); if (_bitStream.ReadBool()) { value = _bitStream.ReadByte(); } _newBaseStream.WriteByte(value); return(value); }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { if (args.Length >= 1) { if (args[0] == "exportMap") { ExportMap.ExportMain(args); return; } if (args[0] == "exportStyle") { ExportStyle.ExportMain(args); return; } if (args[0] == "sty2mat") { Sty2Mat.ToolMain(args); return; } } var outStream = new BitStream(); outStream.WriteBool(true); outStream.WriteByte(125); outStream.WriteInt16(32766); outStream.WriteInt32(300000); outStream.WriteInt32(300000, 20); outStream.WriteSingle(0.1f); var inStream = new BitStream(outStream.Bytes); Console.WriteLine(inStream.ReadBool()); Console.WriteLine(inStream.ReadByte()); Console.WriteLine(inStream.ReadInt16()); Console.WriteLine(inStream.ReadInt32()); Console.WriteLine(inStream.ReadInt32(20)); Console.WriteLine(inStream.ReadSingle()); Game.Initialize(); while (true) { Game.Process(); } }
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 ProcessServerMessage(BitStream message) { _lastMessageReceivedAt = _clientTime; _lastServerMessage = message.ReadUInt32(); byte command = 0; #if !DEBUG try { #endif do { command = message.ReadByte(); switch (command) { case Server.SnapshotNumber: ProcessSnapshot(message); break; case Server.ReliableCommandNumber: ProcessReliableCommand(message); break; case Server.InitStateNumber: ProcessInitializationState(message); break; } } while (command != 0xFF); #if !DEBUG } catch (Exception e) { Log.Write(LogLevel.Warning, "Error while processing client message: " + e.ToString()); // TODO: disconnect client } #endif }
private static void ProcessClientUserCommand(ServerClient client, BitStream message) { var numCommands = message.ReadByte(); var commands = new UserCommand[numCommands]; UserCommand?lastCommand = null; for (int i = 0; i < numCommands; i++) { commands[i] = new UserCommand(); commands[i].Deserialize(message, lastCommand); lastCommand = commands[i]; } if (client.State != ServerClientState.Active && client.State != ServerClientState.Zombie) { client.InitializeEntity(); } foreach (var command in commands) { client.ProcessCommand(command); } }