コード例 #1
0
        void ReturnOtherThanLastBuffer()
        {
            var bufferList     = m_ReceiveCache.GetAllCachedItems();
            var count          = bufferList.Count;
            var lastBufferItem = bufferList[count - 1].Key.Array;

            for (var i = count - 2; i >= 0; i--)
            {
                if (bufferList[i].Key.Array != lastBufferItem)
                {
                    m_BufferRecycler.Return(bufferList, 0, i + 1);
                    break;
                }
            }

            m_ReceiveCache.Clear();
        }
コード例 #2
0
        /// <summary>
        /// Processes the input segment.
        /// </summary>
        /// <param name="segment">The input segment.</param>
        /// <returns>
        /// the processing result
        /// </returns>
        public virtual ProcessResult Process(ArraySegment <byte> segment)
        {
            var receiveCache = m_ReceiveCache;

            receiveCache.Add(segment);

            var rest = 0;

            var currentReceiveFilter = m_ReceiveFilter;

            SingleItemList <IPackageInfo> singlePackage = null;

            List <IPackageInfo> packageList = null;

            while (true)
            {
                var packageInfo = currentReceiveFilter.Filter(receiveCache, out rest);

                if (currentReceiveFilter.State == FilterState.Error)
                {
                    return(ProcessResult.Create(ProcessState.Error));
                }

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

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


                var nextReceiveFilter = currentReceiveFilter.NextReceiveFilter;

                // don't reset the filter if no request is resolved
                if (packageInfo != null)
                {
                    currentReceiveFilter.Reset();
                }

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

                // continue receive
                if (packageInfo == null)
                {
                    if (rest > 0)
                    {
                        var last = receiveCache.Last;

                        if (rest != last.Count)
                        {
                            PushResetData(segment, rest);
                        }

                        continue;
                    }

                    return(ProcessResult.Create(ProcessState.Cached, GetNotNullOne(packageList, singlePackage)));
                }

                if (packageList != null)
                {
                    packageList.Add(packageInfo);
                }
                else if (singlePackage == null)
                {
                    singlePackage = new SingleItemList <IPackageInfo>(packageInfo);
                }
                else
                {
                    if (packageList == null)
                    {
                        packageList = new List <IPackageInfo>();
                    }

                    packageList.Add(singlePackage[0]);
                    packageList.Add(packageInfo);
                    singlePackage = null;
                }

                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 = receiveCache = new BufferList();

                    if (rest <= 0)
                    {
                        return(ProcessResult.Create(ProcessState.Cached, GetNotNullOne(packageList, singlePackage)));
                    }
                }
                else
                {
                    m_ReceiveCache.Clear();

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

                PushResetData(segment, rest);
            }
        }