示例#1
0
        /// <summary>
        /// 解析一个数据包
        /// 不足一个封包时返回null
        /// </summary>
        /// <param name="builder">接收到的历史数据</param>
        /// <returns></returns>
        public static FastPacket GetPacket(ByteBuilder builder)
        {
            // 包头长度
            const int headLength = 12;

            // 不会少于12
            if (builder.Length < headLength)
            {
                return(null);
            }

            // 包长
            int totalLength = builder.ToInt32(0, Endians.Big);

            // 包长要小于等于数据长度
            if (totalLength > builder.Length || totalLength < headLength)
            {
                return(null);
            }

            // cmd
            int cmd = builder.ToInt32(4, Endians.Big);
            // 哈希值
            int hashCode = builder.ToInt32(8, Endians.Big);

            // 实体数据
            byte[] body = builder.ToArray(12, totalLength - headLength);

            // 清空本条数据
            builder.Remove(totalLength);
            return(new FastPacket(cmd, hashCode, body));
        }
示例#2
0
        /// <summary>
        /// 解析请求的数据
        /// 返回请求数据包
        /// </summary>
        /// <param name="builder">所有收到的数据</param>
        /// <param name="resultBuilder">用于保存数理后的数据</param>
        /// <returns></returns>
        public unsafe static Hybi13Packet GetPacket(ByteBuilder builder, ByteBuilder resultBuilder)
        {
            if (builder.Length < 2)
            {
                return(null);
            }

            var isFinal      = (builder.ToByte(0) & 128) != 0;
            var reservedBits = builder.ToByte(0) & 112;
            var frameType    = (FrameTypes)(builder.ToByte(0) & 15);
            var isMasked     = (builder.ToByte(1) & 128) != 0;
            var length       = builder.ToByte(1) & 127;

            if (isMasked == false || Enum.IsDefined(typeof(FrameTypes), frameType) == false || reservedBits != 0)
            {
                return(null);
            }

            // 计算数据长度和mask索引
            var maskIndex = 2;

            if (length == 127)
            {
                if (builder.Length < maskIndex + 8)
                {
                    return(null);
                }
                length    = (int)builder.ToUInt64(maskIndex, Endians.Big);
                maskIndex = maskIndex + 8;
            }
            else if (length == 126)
            {
                if (builder.Length < maskIndex + 2)
                {
                    return(null);
                }
                length    = (int)builder.ToUInt16(maskIndex, Endians.Big);
                maskIndex = maskIndex + 2;
            }

            // 检查数据长度
            if (builder.Length < maskIndex + Math.Max(4, length))
            {
                return(null);
            }

            // 数据内容的索引位置
            var dataIndex = maskIndex + 4;

            if (length > 0)
            {
                fixed(byte *pdata = &builder.Source[dataIndex], pmask = &builder.Source[maskIndex])
                {
                    for (var i = 0; i < length; i++)
                    {
                        *(pdata + i) = (byte)(*(pdata + i) ^ *(pmask + i % 4));
                    }
                }
            }

            // 将数据放到resultBuilder
            resultBuilder.Add(builder.Source, dataIndex, length);
            // 清除已分析的数据
            builder.Remove(dataIndex + length);

            // 检查数据是否传输完成
            if (isFinal == true && frameType != FrameTypes.Continuation)
            {
                var bytes = resultBuilder.ToArrayThenClear();
                return(new RequestPacket(frameType, bytes));
            }
            else
            {
                return(null);
            }
        }