Exemplo n.º 1
0
 public void Reset()
 {
     m_ReceiveCache.Clear();
     m_FirstReceiveFilter.Reset();
     if (m_ReceiveFilter != m_FirstReceiveFilter)
     {
         m_ReceiveFilter = m_FirstReceiveFilter;
     }
 }
        /// <summary>
        /// Processes the input segment.
        /// </summary>
        /// <param name="segment">The input segment.</param>
        /// <param name="state">The buffer state.</param>
        /// <returns>
        /// the processing result
        /// </returns>
        public virtual ProcessResult Process(ArraySegment <byte> segment, IBufferState state)
        {
            m_ReceiveCache.Add(segment, state);

            var rest = 0;

            while (true)
            {
                var packageInfo = m_ReceiveFilter.Filter(m_ReceiveCache, out rest);

                if (m_ReceiveFilter.State == FilterState.Error)
                {
                    m_BufferRecycler.Return(m_ReceiveCache.GetAllCachedItems(), 0, m_ReceiveCache.Count);
                    return(ProcessResult.Create(ProcessState.Error));
                }

                if (m_MaxPackageLength > 0)
                {
                    var length = m_ReceiveCache.Total;

                    if (length > m_MaxPackageLength)
                    {
                        m_BufferRecycler.Return(m_ReceiveCache.GetAllCachedItems(), 0, m_ReceiveCache.Count);
                        return(ProcessResult.Create(ProcessState.Error, string.Format("Max package length: {0}, current processed length: {1}", m_MaxPackageLength, length)));
                    }
                }

                //Receive continue
                if (packageInfo == null)
                {
                    if (rest > 0)
                    {
                        PushResetData(segment, rest, state);
                        continue;
                    }

                    return(ProcessResult.Create(ProcessState.Cached));
                }

                m_ReceiveFilter.Reset();

                var nextReceiveFilter = m_ReceiveFilter.NextReceiveFilter;

                if (nextReceiveFilter != null)
                {
                    m_ReceiveFilter = nextReceiveFilter;
                }

                m_PackageHandler.Handle(packageInfo);

                if (packageInfo is IBufferedPackageInfo && // is a buffered package
                    (packageInfo as IBufferedPackageInfo).Data is BufferList)        // and it uses receive buffer directly
                {
                    // so we need to create a new receive buffer container to use
                    m_ReceiveCache = new BufferList();

                    if (rest <= 0)
                    {
                        return(ProcessResult.Create(ProcessState.Cached));
                    }
                }
                else
                {
                    ReturnOtherThanLastBuffer();

                    if (rest <= 0)
                    {
                        return(ProcessResult.Create(ProcessState.Completed));
                    }
                }

                PushResetData(segment, rest, state);
            }
        }
Exemplo n.º 3
0
        // Token: 0x06000706 RID: 1798 RVA: 0x0001B5A8 File Offset: 0x000197A8
        public virtual ProcessResult Process(ArraySegment <byte> segment)
        {
            BufferList bufferList = this.m_ReceiveCache;

            bufferList.Add(segment);
            int num = 0;
            IReceiveFilter <TPackageInfo> receiveFilter  = this.m_ReceiveFilter;
            SingleItemList <IPackageInfo> singleItemList = null;
            List <IPackageInfo>           list           = null;
            int total;

            for (;;)
            {
                int          count        = bufferList.Last.Count;
                TPackageInfo tpackageInfo = receiveFilter.Filter(bufferList, out num);
                if (receiveFilter.State == FilterState.Error)
                {
                    break;
                }
                if (this.m_MaxPackageLength > 0)
                {
                    total = bufferList.Total;
                    if (total > this.m_MaxPackageLength)
                    {
                        goto Block_3;
                    }
                }
                IReceiveFilter <TPackageInfo> nextReceiveFilter = receiveFilter.NextReceiveFilter;
                if (tpackageInfo != null)
                {
                    receiveFilter.Reset();
                }
                if (nextReceiveFilter != null)
                {
                    receiveFilter        = nextReceiveFilter;
                    this.m_ReceiveFilter = receiveFilter;
                }
                if (tpackageInfo == null)
                {
                    if (num <= 0)
                    {
                        goto IL_D4;
                    }
                    ArraySegment <byte> last = bufferList.Last;
                    if (num != count)
                    {
                        this.PushResetData(segment, num);
                    }
                }
                else
                {
                    if (list != null)
                    {
                        list.Add(tpackageInfo);
                    }
                    else if (singleItemList == null)
                    {
                        singleItemList = new SingleItemList <IPackageInfo>(tpackageInfo);
                    }
                    else
                    {
                        if (list == null)
                        {
                            list = new List <IPackageInfo>();
                        }
                        list.Add(singleItemList[0]);
                        list.Add(tpackageInfo);
                        singleItemList = null;
                    }
                    if (tpackageInfo is IBufferedPackageInfo && (tpackageInfo as IBufferedPackageInfo).Data is BufferList)
                    {
                        bufferList = (this.m_ReceiveCache = new BufferList());
                        if (num <= 0)
                        {
                            goto Block_14;
                        }
                    }
                    else
                    {
                        this.m_ReceiveCache.Clear();
                        if (num <= 0)
                        {
                            goto Block_15;
                        }
                    }
                    this.PushResetData(segment, num);
                }
            }
            return(ProcessResult.Create(ProcessState.Error));

Block_3:
            return(ProcessResult.Create(ProcessState.Error, string.Format("Max package length: {0}, current processed length: {1}", this.m_MaxPackageLength, total)));

IL_D4:
            return(ProcessResult.Create(ProcessState.Cached, this.GetNotNullOne(list, singleItemList)));

Block_14:
            return(ProcessResult.Create(ProcessState.Cached, this.GetNotNullOne(list, singleItemList)));

Block_15:
            return(ProcessResult.Create(ProcessState.Completed, this.GetNotNullOne(list, singleItemList)));
        }
Exemplo n.º 4
0
        public virtual ProcessResult Process(ArraySegment <byte> segment)
        {
            BufferList bufferList = m_ReceiveCache;

            bufferList.Add(segment);
            int rest = 0;
            IReceiveFilter <TPackageInfo> receiveFilter  = m_ReceiveFilter;
            SingleItemList <IPackageInfo> singleItemList = null;
            List <IPackageInfo>           list           = null;

            while (true)
            {
                int          count = bufferList.Last.Count;
                TPackageInfo val   = receiveFilter.Filter(bufferList, out rest);
                if (receiveFilter.State == FilterState.Error)
                {
                    return(ProcessResult.Create(ProcessState.Error));
                }
                if (m_MaxPackageLength > 0)
                {
                    int total = bufferList.Total;
                    if (total > m_MaxPackageLength)
                    {
                        return(ProcessResult.Create(ProcessState.Error, $"Max package length: {m_MaxPackageLength}, current processed length: {total}"));
                    }
                }
                IReceiveFilter <TPackageInfo> nextReceiveFilter = receiveFilter.NextReceiveFilter;
                if (val != null)
                {
                    receiveFilter.Reset();
                }
                if (nextReceiveFilter != null)
                {
                    receiveFilter = (m_ReceiveFilter = nextReceiveFilter);
                }
                if (val == null)
                {
                    if (rest > 0)
                    {
                        ArraySegment <byte> last = bufferList.Last;
                        if (rest != count)
                        {
                            PushResetData(segment, rest);
                        }
                        continue;
                    }
                    return(ProcessResult.Create(ProcessState.Cached, GetNotNullOne(list, singleItemList)));
                }
                if (list != null)
                {
                    list.Add(val);
                }
                else if (singleItemList == null)
                {
                    singleItemList = new SingleItemList <IPackageInfo>(val);
                }
                else
                {
                    if (list == null)
                    {
                        list = new List <IPackageInfo>();
                    }
                    list.Add(singleItemList[0]);
                    list.Add(val);
                    singleItemList = null;
                }
                if (val is IBufferedPackageInfo && (val as IBufferedPackageInfo).Data is BufferList)
                {
                    bufferList = (m_ReceiveCache = new BufferList());
                    if (rest <= 0)
                    {
                        return(ProcessResult.Create(ProcessState.Cached, GetNotNullOne(list, singleItemList)));
                    }
                }
                else
                {
                    m_ReceiveCache.Clear();
                    if (rest <= 0)
                    {
                        break;
                    }
                }
                PushResetData(segment, rest);
            }
            return(ProcessResult.Create(ProcessState.Completed, GetNotNullOne(list, singleItemList)));
        }
        public virtual ProcessResult Process(ArraySegment <byte> segment, IBufferState state)
        {
            m_ReceiveCache.Add(segment, state);

            var rest = 0;

            while (true)
            {
                var packageInfo = m_ReceiveFilter.Filter(m_ReceiveCache, out rest);

                if (m_ReceiveFilter.State == FilterState.Error)
                {
                    m_BufferRecycler.Return(m_ReceiveCache.GetAllCachedItems(), 0, m_ReceiveCache.Count);
                    return(ProcessResult.Create(ProcessState.Error));
                }

                if (m_MaxPackageLength > 0)
                {
                    var length = m_ReceiveCache.Total;

                    if (length > m_MaxPackageLength)
                    {
                        m_BufferRecycler.Return(m_ReceiveCache.GetAllCachedItems(), 0, m_ReceiveCache.Count);
                        return(ProcessResult.Create(ProcessState.Error, string.Format("Max package length: {0}, current processed length: {1}", m_MaxPackageLength, length)));
                    }
                }

                //Receive continue
                if (packageInfo == null)
                {
                    if (rest > 0)
                    {
                        PushResetData(segment, rest, state);
                        continue;
                    }

                    //Because the current buffer is cached, so new buffer is required for receiving
                    FireNewReceiveBufferRequired();
                    return(ProcessResult.Create(ProcessState.Cached));
                }

                m_ReceiveFilter.Reset();

                var nextReceiveFilter = m_ReceiveFilter.NextReceiveFilter;

                if (nextReceiveFilter != null)
                {
                    m_ReceiveFilter = nextReceiveFilter;
                }

                m_PackageHandler.Handle(packageInfo);

                if (packageInfo is IRawPackageInfo)
                {
                    m_ReceiveCache = new ReceiveCache();

                    if (rest <= 0)
                    {
                        return(ProcessResult.Create(ProcessState.Cached));
                    }
                }
                else
                {
                    ReturnOtherThanLastBuffer();

                    if (rest <= 0)
                    {
                        return(ProcessResult.Create(ProcessState.Completed));
                    }
                }

                PushResetData(segment, rest, state);
            }
        }