Ejemplo n.º 1
0
        // serialization functions
        public void Serialize(BitStream message, UserCommand?old)
        {
            if (old == null)
            {
                message.WriteUInt32(ServerTime);
                message.WriteInt16((short)Buttons);
            }
            else
            {
                var oldCommand = old.Value;

                if ((ServerTime - oldCommand.ServerTime) < 255)
                {
                    message.WriteBool(true);
                    message.WriteByte((byte)(ServerTime - oldCommand.ServerTime));
                }
                else
                {
                    message.WriteBool(false);
                    message.WriteUInt32(ServerTime);
                }

                message.WriteDeltaInt16((short)Buttons, (short)oldCommand.Buttons);
            }
        }
Ejemplo n.º 2
0
        public void WriteUInt32(uint value)
        {
            _newBaseStream.WriteUInt32(value);

            if (_baseStream != null && value == _baseStream.ReadUInt32())
            {
                _bitStream.WriteBool(false);
            }
            else
            {
                _bitStream.WriteBool(true);
                _bitStream.WriteUInt32(value);
            }
        }
Ejemplo n.º 3
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++;
        }
Ejemplo n.º 4
0
        public void SendMessage(byte[] message)
        {
            // message header
            var outMessage = new BitStream();

            outMessage.WriteUInt32(SequenceOut);

            SequenceOut++;

            // write the message
            outMessage.WriteBytes(message);

            NetManager.SendPacket(ChannelType, Address, outMessage.Bytes);

            // log the packet
            if (ConVar.GetValue <bool>("net_showpackets"))
            {
                Log.Write(LogLevel.Debug, "sending {0}b, seq={1}", outMessage.Bytes.Length, SequenceOut);
            }
        }
Ejemplo n.º 5
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);
        }