public virtual TPackageInfo Filter(ReceiveCache data, out int rest)
        {
            rest = 0;
            var total = data.Total;

            //Haven't received a full request package
            if (total < m_Size)
            {
                return(default(TPackageInfo));
            }

            //There is more data after parse one request
            if (total > m_Size)
            {
                rest = total - m_Size;
                data.SetLastItemLength(data.Current.Count - rest);
            }

            if (!CanResolvePackage(data))
            {
                return(default(TPackageInfo));
            }

            return(ResolvePackage(data));
        }
示例#2
0
        public TPackageInfo Filter(ReceiveCache data, out int rest)
        {
            rest = 0;

            var currentSegment = data.Current;
            var readBuffer     = currentSegment.Array;
            var offset         = currentSegment.Offset;
            var length         = currentSegment.Count;

            int parsedLength, pos;

            while (m_SpliterFoundCount < m_SpliterCount)
            {
                pos = readBuffer.SearchMark(offset, length, m_SpliterSearchState, out parsedLength);

                if (pos < 0)
                {
                    return(default(TPackageInfo));
                }

                m_SpliterFoundCount++;
                offset += parsedLength;
                length -= parsedLength;
            }

            //Found enougth spliters
            data.SetLastItemLength(offset - currentSegment.Offset);
            Reset();
            rest = length;

            return(ResolvePackage(data));
        }
 public DefaultPipelineProcessor(IPackageHandler <TPackageInfo> packageHandler, IReceiveFilter <TPackageInfo> receiveFilter, int maxPackageLength = 0, IBufferRecycler bufferRecycler = null)
 {
     m_PackageHandler   = packageHandler;
     m_ReceiveFilter    = receiveFilter;
     m_BufferRecycler   = bufferRecycler ?? s_NullBufferRecycler;
     m_ReceiveCache     = new ReceiveCache();
     m_MaxPackageLength = maxPackageLength;
 }
        public TPackageInfo Filter(ReceiveCache data, out int rest)
        {
            rest = 0;

            var current = data.Current;

            int prevMatched = m_SearchState.Matched;

            int parsedLength;
            int result = current.Array.SearchMark(current.Offset, current.Count, m_SearchState, out parsedLength);

            if (result < 0) //Not found
            {
                return(NullPackageInfo);
            }

            //Found
            data.SetLastItemLength(parsedLength);
            rest = current.Count - parsedLength;

            return(ResolvePackage(data));
        }
示例#5
0
 public RawPackageInfo(TKey key, ReceiveCache rawData)
 {
     Key     = key;;
     RawData = rawData;
 }
        public virtual TPackageInfo Filter(ReceiveCache data, out int rest)
        {
            rest = 0;

            int searchEndMarkOffset;
            int searchEndMarkLength;

            var currentSegment = data.Current;
            var readBuffer     = currentSegment.Array;
            var offset         = currentSegment.Offset;
            var length         = currentSegment.Count;

            int totalParsed = 0;

            if (!m_FoundBegin)
            {
                int pos = readBuffer.SearchMark(offset, length, m_BeginSearchState, out totalParsed);

                if (pos < 0)
                {
                    //All received data is part of the begin mark
                    if (m_BeginSearchState.Matched > 0 && data.Total == m_BeginSearchState.Matched)
                    {
                        return(default(TPackageInfo));
                    }

                    //Invalid data, contains invalid data before the regular begin mark
                    State = FilterState.Error;
                    return(default(TPackageInfo));
                }

                //Found the matched begin mark
                if (pos != offset)//But not at the beginning, contains invalid data before the regular begin mark
                {
                    State = FilterState.Error;
                    return(default(TPackageInfo));
                }

                //Found start mark, then search end mark
                m_FoundBegin = true;

                searchEndMarkOffset = offset + totalParsed;

                //Reach end
                if (offset + length <= searchEndMarkOffset)
                {
                    return(default(TPackageInfo));
                }

                searchEndMarkLength = offset + length - searchEndMarkOffset;
            }
            else//Already found begin mark
            {
                searchEndMarkOffset = offset;
                searchEndMarkLength = length;
            }

            while (true)
            {
                int parsedLength;
                var endPos = readBuffer.SearchMark(searchEndMarkOffset, searchEndMarkLength, m_EndSearchState, out parsedLength);

                //Haven't found end mark
                if (endPos < 0)
                {
                    return(default(TPackageInfo));
                }

                totalParsed += parsedLength; //include begin mark if the mark is found in this round receiving
                rest         = length - totalParsed;

                data.SetLastItemLength(totalParsed);

                var packageInfo = ResolvePackage(data);

                if (!ReferenceEquals(packageInfo, default(TPackageInfo)))
                {
                    Reset();
                    return(packageInfo);
                }

                if (rest > 0)
                {
                    searchEndMarkOffset = endPos + m_EndSearchState.Mark.Length;
                    searchEndMarkLength = rest;
                    continue;
                }

                //Not found end mark
                return(default(TPackageInfo));
            }
        }
        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);
            }
        }