예제 #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);
        }
예제 #2
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;
        }