コード例 #1
0
        /// <summary>
        /// Note that the packetTick may not be the tick this event was created on
        /// if we're re-trying to send this event in subsequent packets. This tick
        /// is intended for use in tick diffs for compression.
        /// </summary>
        internal static RailEvent Decode(
            RailBitBuffer buffer,
            Tick packetTick)
        {
            // Read: [EventType]
            int factoryType = buffer.ReadInt(RailEvent.FactoryTypeCompressor);

            RailEvent evnt = RailEvent.Create(factoryType);

            // Read: [EventId]
            evnt.EventId = buffer.ReadSequenceId();

            // Read: [HasEntityId]
            bool hasEntityId = buffer.ReadBool();

            if (hasEntityId)
            {
                // Read: [EntityId]
                evnt.EntityId = buffer.ReadEntityId();
            }

            // Read: [EventData]
            evnt.DecodeData(buffer, packetTick);

            return(evnt);
        }
コード例 #2
0
        /// <summary>
        /// Overwrites this buffer with an array of byte data.
        /// </summary>
        public void Load(byte[] data, int length)
        {
            int numChunks = (length / 4) + 1;

            if (this.chunks.Length < numChunks)
            {
                this.chunks = new uint[numChunks];
            }

            for (int i = 0; i < numChunks; i++)
            {
                int  dataIdx = i * 4;
                uint chunk   =
                    (uint)data[dataIdx] |
                    (uint)data[dataIdx + 1] << 8 |
                        (uint)data[dataIdx + 2] << 16 |
                        (uint)data[dataIdx + 3] << 24;
                this.chunks[i] = chunk;
            }

            int positionInByte = RailBitBuffer.FindHighestBitPosition(data[length - 1]);

            // Take one off the position to backtrack from the sentinel bit
            this.writePos = ((length - 1) * 8) + (positionInByte - 1);
            this.readPos  = 0;
        }
コード例 #3
0
        private void DecodeDeltas(RailBitBuffer buffer)
        {
            this.deltas.Decode(
                buffer,
                () => RailState.DecodeDelta(buffer, this.SenderTick));
#endif
        }
コード例 #4
0
 protected void EncodeCommands(RailBitBuffer buffer)
 {
     this.commandUpdates.Encode(
         buffer,
         RailConfig.PACKCAP_COMMANDS,
         RailConfig.MAXSIZE_COMMANDUPDATE,
         (commandUpdate) => commandUpdate.Encode(buffer));
 }
コード例 #5
0
        protected override void EncodePayload(
            RailBitBuffer buffer,
            int reservedBytes)
        {
#if SERVER
            // Write: [Deltas]
            this.EncodeDeltas(buffer, reservedBytes);
        }
コード例 #6
0
        protected override void DecodePayload(RailBitBuffer buffer)
        {
#if SERVER
            // Read: [Commands]
            this.DecodeCommands(buffer);

            // Read: [View]
            this.DecodeView(buffer);
        }
コード例 #7
0
ファイル: RailPacket.cs プロジェクト: longde123/RailgunNet
        private void DecodeHeader(RailBitBuffer buffer)
        {
            // Read: [LocalTick]
            this.senderTick = buffer.ReadTick();

            // Read: [AckTick]
            this.ackTick = buffer.ReadTick();

            // Read: [AckReliableEventId]
            this.ackEventId = buffer.ReadSequenceId();
        }
コード例 #8
0
ファイル: RailPacket.cs プロジェクト: longde123/RailgunNet
        private void EncodeHeader(RailBitBuffer buffer)
        {
            // Write: [LocalTick]
            buffer.WriteTick(this.senderTick);

            // Write: [AckTick]
            buffer.WriteTick(this.ackTick);

            // Write: [AckReliableEventId]
            buffer.WriteSequenceId(this.ackEventId);
        }
コード例 #9
0
        protected override void EncodePayload(
            RailBitBuffer buffer,
            int reservedBytes)
        {
#if CLIENT
            // Write: [Commands]
            this.EncodeCommands(buffer);

            // Write: [View]
            this.EncodeView(buffer, reservedBytes);
        }
コード例 #10
0
        public void Decode(
            RailBitBuffer buffer,
            Func <T> decode)
        {
            IEnumerable <T> decoded = buffer.UnpackAll(decode);

            foreach (T delta in decoded)
            {
                this.received.Add(delta);
            }
        }
コード例 #11
0
        private void EncodeDeltas(
            RailBitBuffer buffer,
            int reservedBytes)
        {
            this.deltas.Encode(
                buffer,
                RailConfig.PACKCAP_MESSAGE_TOTAL - reservedBytes,
                RailConfig.MAXSIZE_ENTITY,
                (delta) => RailState.EncodeDelta(buffer, delta));
#endif
        }
コード例 #12
0
        internal static FixedByteBuffer8 Read(RailBitBuffer buffer)
        {
            uint first  = 0;
            uint second = 0;

            first = buffer.ReadUInt();
            if (buffer.ReadBool())
            {
                second = buffer.ReadUInt();
            }
            return(new FixedByteBuffer8(first, second));
        }
コード例 #13
0
ファイル: RailPacket.cs プロジェクト: longde123/RailgunNet
        private void DecodeEvents(
            RailBitBuffer buffer)
        {
            IEnumerable <RailEvent> decoded =
                buffer.UnpackAll(
                    () => RailEvent.Decode(buffer, this.SenderTick));

            foreach (RailEvent evnt in decoded)
            {
                this.events.Add(evnt);
            }
        }
コード例 #14
0
ファイル: RailPacket.cs プロジェクト: longde123/RailgunNet
 /// <summary>
 /// Writes as many events as possible up to maxSize and returns the number
 /// of events written in the batch. Also increments the total counter.
 /// </summary>
 private void EncodeEvents(
     RailBitBuffer buffer,
     int maxSize)
 {
     this.eventsWritten +=
         buffer.PackToSize(
             maxSize,
             RailConfig.MAXSIZE_EVENT,
             this.GetNextEvents(),
             (evnt) => evnt.Encode(buffer, this.senderTick),
             (evnt) => evnt.RegisterSent());
 }
コード例 #15
0
 public void Encode(
     RailBitBuffer buffer,
     int maxTotalSize,
     int maxIndividualSize,
     Action <T> encode)
 {
     buffer.PackToSize(
         maxTotalSize,
         maxIndividualSize,
         this.pending,
         encode,
         (val) => this.sent.Add(val));
 }
コード例 #16
0
ファイル: RailPacket.cs プロジェクト: longde123/RailgunNet
        internal void Decode(RailBitBuffer buffer)
        {
            // Read: [Header]
            this.DecodeHeader(buffer);

            // Read: [Events] (Early Pack)
            this.DecodeEvents(buffer);

            // Read: [Payload]
            this.DecodePayload(buffer);

            // Read: [Events] (Fill Pack)
            this.DecodeEvents(buffer);
        }
コード例 #17
0
ファイル: RailPacket.cs プロジェクト: longde123/RailgunNet
        /// <summary>
        /// After writing the header we write the packet data in three passes.
        /// The first pass is a fill of events up to a percentage of the packet.
        /// The second pass is the payload value, which will try to fill the
        /// remaining packet space. If more space is available, we will try
        /// to fill it with any remaining events, up to the maximum packet size.
        /// </summary>
        public void Encode(RailBitBuffer buffer)
        {
            // Write: [Header]
            this.EncodeHeader(buffer);

            // Write: [Events] (Early Pack)
            this.EncodeEvents(buffer, RailConfig.PACKCAP_EARLY_EVENTS);

            // Write: [Payload]
            this.EncodePayload(buffer, 1); // Leave one byte for the event count

            // Write: [Events] (Fill Pack)
            this.EncodeEvents(buffer, RailConfig.PACKCAP_MESSAGE_TOTAL);
        }
コード例 #18
0
        internal void Encode(RailBitBuffer buffer)
        {
            // Write: [EntityId]
            buffer.WriteEntityId(this.entityId);

            // Write: [Count]
            buffer.Write(BUFFER_COUNT_BITS, (uint)this.commands.Count);

            // Write: [Commands]
            foreach (RailCommand command in this.commands.GetValues())
            {
                command.Encode(buffer);
            }
        }
コード例 #19
0
        protected void EncodeView(RailBitBuffer buffer, int reservedBytes)
        {
            buffer.PackToSize(
                RailConfig.PACKCAP_MESSAGE_TOTAL - reservedBytes,
                int.MaxValue,
                this.view.GetOrdered(),
                (pair) =>
            {
                buffer.WriteEntityId(pair.Key);        // Write: [EntityId]
                buffer.WriteTick(pair.Value.Tick);     // Write: [Tick]
                buffer.WriteBool(pair.Value.IsFrozen); // Write: [IsFrozen]
            });
#endif
        }
コード例 #20
0
        internal void Write(RailBitBuffer buffer)
        {
            uint first =
                FixedByteBuffer8.Pack(this.val0, this.val1, this.val2, this.val3);
            uint second =
                FixedByteBuffer8.Pack(this.val4, this.val5, this.val6, this.val7);
            bool writeSecond = (second > 0);

            buffer.WriteUInt(first);
            buffer.WriteBool(writeSecond);
            if (writeSecond)
            {
                buffer.WriteUInt(second);
            }
        }
コード例 #21
0
        protected void OnPayloadReceived(IRailNetPeer peer, byte[] buffer, int length)
        {
            RailBitBuffer bitBuffer = this.interpreter.LoadData(buffer, length);

            this.reusableIncoming.Decode(bitBuffer);
            if (bitBuffer.IsFinished)
            {
                this.ProcessPacket(this.reusableIncoming);
            }
            else
            {
                RailDebug.LogError("Bad packet read, discarding...");
            }
            this.reusableIncoming.Reset();
        }
コード例 #22
0
        internal static RailCommandUpdate Decode(RailBitBuffer buffer)
        {
            RailCommandUpdate update = RailResource.Instance.CreateCommandUpdate();

            // Read: [EntityId]
            update.entityId = buffer.ReadEntityId();

            // Read: [Count]
            int count = (int)buffer.Read(BUFFER_COUNT_BITS);

            // Read: [Commands]
            for (int i = 0; i < count; i++)
            {
                update.commands.Store(RailCommand.Decode(buffer));
            }

            return(update);
        }
コード例 #23
0
        public void DecodeView(RailBitBuffer buffer)
        {
            IEnumerable <KeyValuePair <EntityId, RailViewEntry> > decoded =
                buffer.UnpackAll(
                    () =>
            {
                return(new KeyValuePair <EntityId, RailViewEntry>(
                           buffer.ReadEntityId(),    // Read: [EntityId]
                           new RailViewEntry(
                               buffer.ReadTick(),    // Read: [Tick]
                               buffer.ReadBool()))); // Read: [IsFrozen]
            });

            foreach (var pair in decoded)
            {
                this.view.RecordUpdate(pair.Key, pair.Value);
            }
#endif
        }
コード例 #24
0
        public void WriteString(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            uint length = (uint)value.Length;

            RailDebug.Assert(length <= RailConfig.STRING_LENGTH_MAX, value);
            if (value.Length > RailConfig.STRING_LENGTH_MAX)
            {
                length = RailConfig.STRING_LENGTH_MAX;
            }

            this.Write(RailBitBuffer.STRING_LENGTH_BITS, length);
            for (int i = 0; i < length; i++)
            {
                this.Write(RailBitBuffer.ASCII_BITS, RailBitBuffer.ToASCII(value[i]));
            }
        }
コード例 #25
0
        /// <summary>
        /// Note that the packetTick may not be the tick this event was created on
        /// if we're re-trying to send this event in subsequent packets. This tick
        /// is intended for use in tick diffs for compression.
        /// </summary>
        internal void Encode(
            RailBitBuffer buffer,
            Tick packetTick)
        {
            // Write: [EventType]
            buffer.WriteInt(RailEvent.FactoryTypeCompressor, this.factoryType);

            // Write: [EventId]
            buffer.WriteSequenceId(this.EventId);

            // Write: [HasEntityId]
            buffer.WriteBool(this.EntityId.IsValid);

            if (this.EntityId.IsValid)
            {
                // Write: [EntityId]
                buffer.WriteEntityId(this.EntityId);
            }

            // Write: [EventData]
            this.EncodeData(buffer, packetTick);
        }
コード例 #26
0
 protected void DecodeCommands(RailBitBuffer buffer)
 {
     this.commandUpdates.Decode(
         buffer,
         () => RailCommandUpdate.Decode(buffer));
 }
コード例 #27
0
ファイル: EntityId.cs プロジェクト: longde123/RailgunNet
 public static EntityId PeekEntityId(this RailBitBuffer buffer)
 {
     return(EntityId.Peek(buffer));
 }
コード例 #28
0
ファイル: EntityId.cs プロジェクト: longde123/RailgunNet
 internal static EntityId Peek(RailBitBuffer buffer)
 {
     return(new EntityId(buffer.PeekUInt()));
 }
コード例 #29
0
ファイル: EntityId.cs プロジェクト: longde123/RailgunNet
 internal static EntityId Read(RailBitBuffer buffer)
 {
     return(new EntityId(buffer.ReadUInt()));
 }
コード例 #30
0
ファイル: EntityId.cs プロジェクト: longde123/RailgunNet
 internal void Write(RailBitBuffer buffer)
 {
     buffer.WriteUInt(this.idValue);
 }