QuicDotNet.Packets.RegularPacket.FromByteArray C# (CSharp) Method

FromByteArray() protected method

protected FromByteArray ( [ bytes ) : void
bytes [
return void
        protected internal void FromByteArray([NotNull] byte[] bytes)
        {
            var index = 0;

            // Message authentication hash
            if (this.PacketNumber == 1)
            {
                // First 12 bytes are the message authentication hash
                this.MessageAuthenticationHash = new byte[12];
                Array.Copy(bytes, this.MessageAuthenticationHash, 12);
                index += 12;
            }

            // Private header
            this.Entropy = (bytes[index] & (1 << 0)) != 0;
            if ((bytes[index] & (1 << 2)) != 0)
            {
                // https://tools.ietf.org/html/draft-tsvwg-quic-protocol-02#section-6.1
                throw new NotImplementedException("This implementation does not yet handle FLAG_FEC");
            }

            if ((bytes[index] & (1 << 1)) != 0)
            {
                index++;
                this.FecGroup = bytes[index];
            }
            index++;

            // Decode frames
            while (index < bytes.Length)
            {
                // PADDING frame
                if (bytes[index] == 0)
                {
                    this.AddFrame(new PaddingFrame(bytes.Length - index));
                    break;
                }

                // STREAM frame
                if ((bytes[index] & (1 << 7)) != 0)
                {
                    var sf = StreamFrame.FromByteArray(bytes, index);
                    this.AddFrame(sf.Item1);
                    index = sf.Item2;
                }
            }

            Console.WriteLine("Decoding frame");

        }

Usage Example

示例#1
0
        public static bool TryParse(byte[] packetBytes, out AbstractPacketBase packet)
        {
            var publicFlags = packetBytes[0];
            var index       = 1;
            var versionFlag = (publicFlags & (1 << 0)) != 0;
            var resetFlag   = (publicFlags & (1 << 1)) != 0;
            var cidFlag1    = (publicFlags & (1 << 2)) != 0;
            var cidFlag2    = (publicFlags & (1 << 3)) != 0;
            var pnFlag1     = (publicFlags & (1 << 4)) != 0;
            var pnFlag2     = (publicFlags & (1 << 5)) != 0;

            ulong?connectionId;

            if (cidFlag1 && cidFlag2)
            {
                connectionId = BitConverter.ToUInt64(packetBytes, index);
                index       += 8;
            }
            else if (!cidFlag1 && cidFlag2)
            {
                connectionId = BitConverter.ToUInt32(packetBytes, index);
                index       += 4;
            }
            else if (cidFlag1)
            {
                connectionId = packetBytes[1];
                index       += 1;
            }
            else
            {
                connectionId = null;
            }

            uint?version;

            if (versionFlag)
            {
                version = BitConverter.ToUInt32(packetBytes, index);
                index  += 4;
            }

            ulong packetNumber;

            if (pnFlag1 && pnFlag2)
            {
                var ba = new byte[8];
                Array.Copy(packetBytes, index, ba, 2, 6);
                packetNumber = BitConverter.ToUInt64(ba, 0);
                index       += 6;
            }
            else if (!pnFlag1 && pnFlag2)
            {
                packetNumber = BitConverter.ToUInt32(packetBytes, index);
                index       += 4;
            }
            else if (pnFlag1)
            {
                packetNumber = BitConverter.ToUInt16(packetBytes, index);
                index       += 2;
            }
            else
            {
                packetNumber = packetBytes[index];
                index       += 1;
            }

            var rp           = new RegularPacket(connectionId, packetNumber, null);
            var payloadBytes = new byte[packetBytes.Length - index];

            Array.Copy(packetBytes, index, payloadBytes, 0, payloadBytes.Length);
            rp.FromByteArray(payloadBytes);
            packet = rp;

            return(true);
        }
All Usage Examples Of QuicDotNet.Packets.RegularPacket::FromByteArray