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 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));
        }
        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));
            }
        }