예제 #1
0
        /// <summary>
        /// 读取并解析数据
        /// </summary>
        /// <param name="session"></param>
        /// <param name="buffer"></param>
        /// <param name="consumed"></param>
        /// <param name="examined"></param>
        /// <returns></returns>
        private bool ReaderBuffer(ref ReadOnlySequence <byte> buffer, out SequencePosition consumed, out SequencePosition examined)
        {
            consumed = buffer.Start;
            examined = buffer.End;

            var bytesConsumedTotal = 0L;

            var maxPackageLength = ClientOption.MaxRequestLength;
            var seqReader        = new SequenceReader <byte>(buffer);

            while (true)
            {
mark:
                //过滤解析
                if (NextReceiveFilter != null)
                {
                    //如果修改了下一个过滤器则使用下一个过滤器
                    if (NextReceiveFilter.NextReceiveFilter != null)
                    {
                        NextReceiveFilter = NextReceiveFilter.NextReceiveFilter;
                    }

                    var packageInfo   = NextReceiveFilter.Filter(ref seqReader);
                    var bytesConsumed = seqReader.Consumed;
                    bytesConsumedTotal += bytesConsumed;

                    var len = bytesConsumed;

                    //数据未消费,需要更多的数据
                    if (len == 0)
                    {
                        len = seqReader.Length;
                    }

                    if (maxPackageLength > 0 && len > maxPackageLength)
                    {
                        LogHelper.Error($"数据长度不能超过{maxPackageLength}个字节");
                        //直接关闭连接
                        Close();
                        return(false);
                    }

                    //继续接收
                    if (packageInfo == null)
                    {
                        //如果修下一个过滤器不为BULL切设置了,说明使用了多过滤器,packageInfo返回null表示需要重走过滤器
                        if (NextReceiveFilter.NextReceiveFilter != null && NextReceiveFilter.NextReceiveFilter != NextReceiveFilter)
                        {
                            goto mark;
                        }
                        consumed = buffer.GetPosition(bytesConsumedTotal);
                        return(true);
                    }
                    if (!packageInfo.Success)
                    {
                        LogHelper.Info(packageInfo.Message);
                    }
                    else
                    {
                        OnReceived?.Invoke(this, packageInfo);
                    }

                    if (seqReader.End) //没有更多数据
                    {
                        examined = consumed = buffer.End;
                        return(true);
                    }

                    seqReader = new SequenceReader <byte>(seqReader.Sequence.Slice(bytesConsumed));
                }
                else
                {
                    examined = consumed = buffer.End;
                    var packageInfo = new TRequestInfo
                    {
                        Success = true,
                        Raw     = buffer.ToArray()
                    };
                    OnReceived?.Invoke(this, packageInfo);
                    return(true);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 读取并解析数据
        /// </summary>
        /// <param name="session"></param>
        /// <param name="buffer"></param>
        /// <param name="consumed"></param>
        /// <param name="examined"></param>
        /// <returns></returns>
        private bool ReaderBuffer(ref ReadOnlySequence <byte> buffer, out SequencePosition consumed, out SequencePosition examined, TSession session = default)
        {
            consumed = buffer.Start;
            examined = buffer.End;

            var bytesConsumedTotal = 0L;

            //最大请求(单位:字节)
            var maxPackageLength = ClientOption.MaxRequestLength;
            var seqReader        = new SequenceReader <byte>(buffer);

            while (true)
            {
mark:
                //过滤解析
                if (NextReceiveFilter != null)
                {
                    //有可能切换了过滤器,当前过滤器自定为下一个过滤器
                    if (NextReceiveFilter.NextReceiveFilter != null)
                    {
                        NextReceiveFilter = NextReceiveFilter.NextReceiveFilter;
                    }
                    //按用户指定的协议切割一个包
                    var packageInfo   = NextReceiveFilter.Filter(ref seqReader);
                    var bytesConsumed = seqReader.Consumed;
                    bytesConsumedTotal += bytesConsumed;

                    var len = bytesConsumed;

                    //数据未消费,需要更多的数据
                    if (len == 0)
                    {
                        len = seqReader.Length;
                    }

                    if (maxPackageLength > 0 && len > maxPackageLength)
                    {
                        LogHelper.Error($"数据长度不能超过{maxPackageLength}个字节");
                        //直接关闭连接
                        Close();
                        return(false);
                    }

                    //继续接收
                    if (packageInfo == null)
                    {
                        //如果下一个过滤器不为空,但是有没有收到完整的包,重走过滤器(一般是使用了多过滤器)
                        if (NextReceiveFilter.NextReceiveFilter != null && NextReceiveFilter.NextReceiveFilter != NextReceiveFilter)
                        {
                            goto mark;
                        }
                        consumed = buffer.GetPosition(bytesConsumedTotal);
                        return(true);
                    }
                    if (!packageInfo.Success)
                    {
                        LogHelper.Error(packageInfo.Message);
                    }
                    else
                    {
                        OnReceived?.Invoke(session == null ? this : (object)session, packageInfo);
                    }

                    if (seqReader.End) //没有更多数据
                    {
                        examined = consumed = buffer.End;
                        return(true);
                    }

                    seqReader = new SequenceReader <byte>(seqReader.Sequence.Slice(bytesConsumed));
                }
                else
                {
                    //没有过滤器来多少数据返回多少数据给用户
                    examined = consumed = buffer.End;
                    var packageInfo = new TRequestInfo
                    {
                        Success = true,
                        Raw     = buffer.ToArray()
                    };
                    OnReceived?.Invoke(session == null ? this : (object)session, packageInfo);
                    return(true);
                }
            }
        }