Example #1
0
        private static void SendCommandPacket()
        {
            var         message = new BitStream();
            UserCommand?command, lastCommand = null;

            // last received server message
            message.WriteUInt32(_lastServerMessage);

            // last received reliable message
            message.WriteUInt32(_lastReliableMessage);

            // current server state ID
            message.WriteInt32(_svStateID);

            // reliable messages
            for (int i = _reliableAcknowledged + 1; i <= _reliableSequence; i++)
            {
                // header byte
                message.WriteByte(2);

                // command number
                message.WriteInt32(i);

                // command
                message.WriteString(_reliableCommands[i & (_reliableCommands.Length - 1)]);
            }

            // header byte
            message.WriteByte(UserCommand.CommandType);

            // command count
            message.WriteByte((byte)ClientInput.NumCommands);

            while ((command = ClientInput.GetCommand()) != null)
            {
                // write a command
                command.Value.Serialize(message, lastCommand);

                // set the new command as base
                lastCommand = command;
            }

            // end of command
            message.WriteByte(0xFF);

            // now send the command to the server
            _serverChannel.SendMessage(message);

            _commandSequence++;
        }
Example #2
0
        private static void SendInitializationState(ServerClient client)
        {
            // send enough state for the client to initialize the game
            var message = new BitStream();

            message.WriteByte(Server.InitStateNumber);

            // current state id
            message.WriteInt32(_stateID);

            // map name
            message.WriteString(_mapName);

            // and send the message
            message.WriteByte(0xFF);
            client.Channel.SendMessage(message);

            client.LastInitStateMessage = client.Channel.SequenceOut;
        }
Example #3
0
        private static void SendSnapshot(ServerClient client)
        {
            // send entity states
            var message = new BitStream();

            // reliable messages
            for (uint i = client.ReliableAcknowledged + 1; i <= client.ReliableSequence; i++)
            {
                // header byte
                message.WriteByte(Server.ReliableCommandNumber);

                // command number
                message.WriteUInt32(i);

                // command
                message.WriteString(client.ReliableCommands[i & (client.ReliableCommands.Length - 1)]);
            }

            message.WriteByte(Server.SnapshotNumber);
            message.WriteUInt32(_serverTime);
            message.WriteUInt32(client.LastReceivedReliableCommand);

            var useDelta = true;

            if ((client.Channel.SequenceOut - client.LastAcknowledgedMessage) >= 28)
            {
                useDelta = false;
                Log.Write(LogLevel.Warning, "client {0} lagged behind more than [max stored old packets]", client.Name);
            }

            for (int i = 0; i < Entities.Length; i++)
            {
                var entity = Entities[i];

                if (entity == null)
                {
                    continue;
                }

                // write the entity number
                message.WriteInt32(i, 12);

                var entityBase = (useDelta) ? ((client.EntityBases[i] != null) ? client.EntityBases[i].Get() : null) : null;

                var deltaMessage = new DeltaBitStream(entityBase, message);

                // write the spawn key
                deltaMessage.WriteInt32(entity.SpawnKey, 20);

                // write the type code
                deltaMessage.WriteInt32(entity.TypeCode, 4);

                // write the actual data
                entity.Serialize(deltaMessage);

                // set the new base
                if (client.EntityBases[i] == null)
                {
                    client.EntityBases[i] = new ServerClientEntityBase(client);
                }

                client.EntityBases[i].Set(deltaMessage.NewBase);
            }

            message.WriteInt32(4095, 12);
            message.WriteByte(0xFF);

            // send the message
            client.Channel.SendMessage(message);
        }