Example #1
0
        /// <summary>
        /// 解析请求的数据
        /// 返回请求数据包
        /// </summary>
        /// <param name="streamReader">数据读取器</param>
        /// <param name="requiredMask">是否要求必须Mask</param>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns></returns>
        public static FrameRequest Parse(IByteStream streamReader, bool requiredMask = true)
        {
            if (streamReader.Length < 2)
            {
                return(null);
            }

            ByteBits   byte0     = streamReader.ReadByte();
            bool       fin       = byte0[0];
            ByteBits   rsv       = byte0.Take(1, 3);
            FrameCodes frameCode = (FrameCodes)(byte)byte0.Take(4, 4);

            ByteBits byte1 = streamReader.ReadByte();
            bool     mask  = byte1[0];

            if (requiredMask && mask == false)
            {
                throw new NotSupportedException("mask is required");
            }

            if (Enum.IsDefined(typeof(FrameCodes), frameCode) == false || rsv != 0)
            {
                throw new NotSupportedException();
            }

            int contentSize   = 0;
            int contentLength = byte1.Take(1, 7);

            if (contentLength == 127)
            {
                contentSize   = 8;
                contentLength = (int)streamReader.ReadLong();
            }
            else if (contentLength == 126)
            {
                contentSize   = 2;
                contentLength = (ushort)streamReader.ReadShort();
            }

            int maskSize     = mask ? 4 : 0;
            int packetLength = 2 + maskSize + contentSize + contentLength;

            if (streamReader.Length < packetLength)
            {
                return(null);
            }

            byte[] maskingKey = mask ? streamReader.ReadBytes(4) : null;
            byte[] content    = streamReader.ReadBytes(contentLength);

            if (mask && contentLength > 0)
            {
                for (int i = 0; i < contentLength; i++)
                {
                    content[i] = (byte)(content[i] ^ maskingKey[i % 4]);
                }
            }

            return(new FrameRequest
            {
                Fin = fin,
                Rsv = rsv,
                Mask = mask,
                Frame = frameCode,
                ContentLength = contentLength,
                MaskingKey = maskingKey,
                Content = content
            });
        }
Example #2
0
 public byte ReadByte()
 {
     return(_stream.ReadByte());
 }