/// <summary>
        /// Filters the received data.
        /// </summary>
        /// <param name="data">The received data.</param>
        /// <param name="rest">The length of the rest data after filtering.</param>
        /// <returns>the received packageInfo instance</returns>
        public virtual TPackageInfo Filter(BufferList 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.Last.Count - rest);
            }

            var bufferStream = this.GetBufferStream(data);

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

            return(ResolvePackage(bufferStream));
        }
        /// <summary>
        /// Filters the received data.
        /// </summary>
        /// <param name="data">The received data.</param>
        /// <param name="rest">The length of the rest data after filtering.</param>
        /// <returns>the received packageInfo instance</returns>
        public TPackageInfo Filter(BufferList data, out int rest)
        {
            rest = 0;

            var currentSegment = data.Last;
            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(this.GetBufferStream(data)));
        }
示例#3
0
        public virtual TPackageInfo Filter(BufferList data, out int rest)
        {
            rest = 0;
            int total = data.Total;

            if (total < m_Size)
            {
                return(default(TPackageInfo));
            }
            if (total > m_Size)
            {
                rest = total - m_Size;
                data.SetLastItemLength(data.Last.Count - rest);
            }
            BufferStream bufferStream = this.GetBufferStream(data);

            if (!CanResolvePackage(bufferStream))
            {
                return(default(TPackageInfo));
            }
            return(ResolvePackage(bufferStream));
        }
        /// <summary>
        /// Filters the received data.
        /// </summary>
        /// <param name="data">The received data.</param>
        /// <param name="rest">The length of the rest data after filtering.</param>
        /// <returns>the received packageInfo instance</returns>
        public virtual TPackageInfo Filter(BufferList data, out int rest)
        {
            rest = 0;

            int searchEndMarkOffset;
            int searchEndMarkLength;

            var currentSegment = data.Last;
            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(this.GetBufferStream(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));
            }
        }