/// <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)));
        }
        /// <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 current = data.Last;

            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
            rest = current.Count - parsedLength;

            if (rest > 0)
            {
                data.SetLastItemLength(parsedLength);
            }

            return(ResolvePackage(this.GetBufferStream(data)));
        }
예제 #4
0
        /// <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)
            {
                if (m_packageLen == 0)//未获取包总长度值
                {
                    if (searchEndMarkLength > 0)
                    {
                        if (m_packagelenbuffer == null)
                        {
                            m_packagelenbuffer = new byte[m_FixedLen];
                            m_IsReadLen        = 0;
                        }
                        for (int i = 0; i < searchEndMarkLength; i++)
                        {
                            if (m_IsReadLen < m_FixedLen)
                            {
                                m_packagelenbuffer[m_IsReadLen] = readBuffer[searchEndMarkOffset + i];
                                m_IsReadLen++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (m_IsReadLen == m_FixedLen)
                        {
                            m_packageLen = BitConverter.ToInt32(m_packagelenbuffer, 0) + 20;
                            if (m_packageLen > searchEndMarkLength + m_EndSearchState.Mark.Length)
                            {
                                return(default(TPackageInfo));
                            }
                        }
                        else
                        {
                            return(default(TPackageInfo));
                        }
                    }
                }
                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
                var packagelen = data.Total - length + totalParsed;
                if (packagelen < m_packageLen)
                {
                    searchEndMarkOffset = endPos + m_EndSearchState.Mark.Length;
                    searchEndMarkLength = length - totalParsed;
                    if (searchEndMarkLength == 0)
                    {
                        return(default(TPackageInfo));
                    }
                    continue;
                }
                rest = length - totalParsed;

                if (rest > 0)
                {
                    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));
            }
        }
예제 #5
0
        /// <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(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));
            }
        }