Пример #1
0
        /// <summary>
        /// 查找一个完整的消息,如果不符合条件,则返回空,否则返回一个完整的消息
        /// </summary>
        /// <param name="readBuffer">标本从流中读取的缓冲数组</param>
        /// <param name="rest">查找到一个完整的消息后,剩余未处理的数组长度,查找不到完整消息时为0</param>
        /// <returns></returns>
        public byte[] Filter(List <byte> readBuffer, out int rest)
        {
            int beginIndex = 0;
            int endIndex   = 0;

            byte[] receive = null;

            if (HasReceivedData)
            {
                //如果已接收过数据,则应先从0开始找结束标志
                //如果有,则返回处理
                endIndex = FindEndIndex(readBuffer, -1);//-1表示从开始0开始查找
                if (endIndex >= 0)
                {
                    ByteReceived.AddRange(readBuffer.Take(endIndex + m_endMark.Length));

                    //此时一个消息已接收完整,则返回处理
                    rest    = readBuffer.Count - (endIndex + m_endMark.Length);
                    receive = ByteReceived.ToArray();
                    return(receive);
                }
                else
                {
                    ByteReceived.AddRange(readBuffer);
                    NextReceiveDataFilter = this;
                    rest = 0;
                    return(null);
                }
            }

            beginIndex = FindStartIndex(readBuffer);
            if (beginIndex == -1)
            {
                //如果没有开始标记,则判断是否已接收过数据
                //如果已接收过数据,再判断是否有结束标志,
                //  如果有结束标志,则将结束标志前的数据添加到已接收数据,
                //  如果没有结束标志,则将整个缓冲数组添加到已接收数据;
                //如果没有开始标记,又未接收过数据,则丢弃该段数据
                if (HasReceivedData)
                {
                    endIndex = FindEndIndex(readBuffer, beginIndex);
                    if (endIndex >= 0)
                    {
                        ByteReceived.AddRange(readBuffer.Take(endIndex + m_endMark.Length));

                        //此时一个消息已接收完整,则返回处理
                        rest    = readBuffer.Count - (endIndex + m_endMark.Length);
                        receive = ByteReceived.ToArray();
                        return(receive);
                    }
                    else
                    {
                        ByteReceived.AddRange(readBuffer);
                        NextReceiveDataFilter = this;
                        rest = 0;
                        return(null);
                    }
                }
                else
                {
                    rest = 0;
                    Reset();

                    //如果有异常,可能是接收的数据不符合通信协议,记录日志
                    Log("Received data: " + System.Text.Encoding.Default.GetString(readBuffer.ToArray()));

                    return(null);
                }
            }

            endIndex = FindEndIndex(readBuffer, beginIndex);
            if (endIndex == -1)
            {
                //如果有开始标志,没有结束标记,则将开始标志后面的数据添加到已接收数据
                ByteReceived = new List <byte>();
                ByteReceived.AddRange(readBuffer.Skip(beginIndex).ToList());
                NextReceiveDataFilter = this;
                HasReceivedData       = true;
                rest = 0;
                return(null);
            }
            else
            {
                //如果有开始和结束标志,则说明是一个完整的消息,则直接返回处理
                List <byte> temp = readBuffer.Take(endIndex + m_endMark.Length).ToList(); //先获取结束标志前的所有数据
                temp = temp.Skip(beginIndex).ToList();                                    //再从开始位置取后面所有数据

                ByteReceived = new List <byte>();
                ByteReceived.AddRange(temp);

                //此时一个消息已接收完整,则返回处理
                rest    = readBuffer.Count - (endIndex + m_endMark.Length);
                receive = ByteReceived.ToArray();
                return(receive);
            }
        }
Пример #2
0
        /// <summary>
        /// 查找一个完整的消息,如果不符合条件,则返回空,否则返回一个完整的消息
        /// </summary>
        /// <param name="readBuffer">标本从流中读取的缓冲数组</param>
        /// <param name="rest">查找到一个完整的消息后,剩余未处理的数组长度,查找不到完整消息时为0</param>
        /// <returns></returns>
        public byte[] Filter(List <byte> readBuffer, out int rest)
        {
            int beginIndex = 0;

            byte[] receive = null;

            if (HasReceivedData)
            {
                if (m_dataLen > 0)
                {
                    //表示开头已经接收了消息长度,判断消息长度是否足够即可
                    List <byte> bufferAfterBegin = new List <byte>();
                    bufferAfterBegin.AddRange(ByteReceived);
                    bufferAfterBegin.AddRange(readBuffer);

                    if (bufferAfterBegin.Count >= m_dataLen)
                    {
                        ByteReceived.AddRange(bufferAfterBegin.Take(m_dataLen));

                        if (CheckVerifyCode(ByteReceived))
                        {
                            rest    = bufferAfterBegin.Count - m_dataLen;
                            receive = ByteReceived.ToArray();
                            return(receive);
                        }
                        else
                        {
                            rest = bufferAfterBegin.Count - m_dataLen;
                            Reset();
                            return(null);
                        }
                    }
                    else
                    {
                        ByteReceived.AddRange(bufferAfterBegin);

                        NextReceiveDataFilter = this;
                        rest = 0;
                        return(null);
                    }
                }
                else
                {
                    List <byte> bufferAfterBegin = new List <byte>();
                    bufferAfterBegin.AddRange(ByteReceived);
                    bufferAfterBegin.AddRange(readBuffer);
                    if (bufferAfterBegin.Count > (m_beginMark.Length + m_dataLenByteCount))
                    {
                        //获取数据长度的值
                        byte[] dataLenByteArray = bufferAfterBegin.Take(m_beginMark.Length + m_dataLenByteCount).ToArray();
                        dataLenByteArray = dataLenByteArray.Skip(m_beginMark.Length).ToArray();
                        m_dataLen        = BitConverter.ToInt32(dataLenByteArray.Reverse().ToArray(), 0);

                        //计算是否接收了完整消息
                        if (bufferAfterBegin.Count >= m_dataLen)
                        {
                            ByteReceived.AddRange(bufferAfterBegin.Take(m_dataLen));

                            if (CheckVerifyCode(ByteReceived))
                            {
                                rest    = bufferAfterBegin.Count - m_dataLen;
                                receive = ByteReceived.ToArray();
                                return(receive);
                            }
                            else
                            {
                                rest = bufferAfterBegin.Count - m_dataLen;
                                Reset();
                                return(null);
                            }
                        }
                        else
                        {
                            ByteReceived.AddRange(bufferAfterBegin);

                            NextReceiveDataFilter = this;
                            rest = 0;
                            return(null);
                        }
                    }
                    else
                    {
                        ByteReceived.AddRange(bufferAfterBegin);

                        NextReceiveDataFilter = this;
                        rest = 0;
                        return(null);
                    }
                }
            }

            beginIndex = FindStartIndex(readBuffer);
            if (beginIndex == -1)
            {
                rest = 0;
                Reset();

                //如果有异常,可能是接收的数据不符合通信协议,记录日志
                Log("Received data: " + System.Text.Encoding.Default.GetString(readBuffer.ToArray()));

                return(null);
            }
            else
            {
                //获取数据长度
                //开始标志后的数据长度如果足够长(开始标志长度+数据长度开始位置+数据长度字节数),
                //则获取数据长度信息,如果不够长,则继续接收数据
                List <byte> bufferAfterBegin = readBuffer.Skip(beginIndex).ToList();
                if (bufferAfterBegin.Count > (m_beginMark.Length + m_dataLenByteCount))
                {
                    //获取数据长度的值
                    byte[] dataLenByteArray = bufferAfterBegin.Take(m_beginMark.Length + m_dataLenByteCount).ToArray();
                    dataLenByteArray = dataLenByteArray.Skip(m_beginMark.Length).ToArray();
                    m_dataLen        = BitConverter.ToInt32(dataLenByteArray.Reverse().ToArray(), 0);

                    ByteReceived = new List <byte>();

                    //计算是否接收了完整消息
                    if (bufferAfterBegin.Count >= m_dataLen)
                    {
                        ByteReceived.AddRange(bufferAfterBegin.Take(m_dataLen));

                        if (CheckVerifyCode(ByteReceived))
                        {
                            rest    = bufferAfterBegin.Count - m_dataLen;
                            receive = ByteReceived.ToArray();
                            return(receive);
                        }
                        else
                        {
                            rest = bufferAfterBegin.Count - m_dataLen;
                            Reset();
                            return(null);
                        }
                    }
                    else
                    {
                        ByteReceived.AddRange(bufferAfterBegin);

                        NextReceiveDataFilter = this;
                        rest = 0;
                        return(null);
                    }
                }
                else
                {
                    ByteReceived = new List <byte>();
                    ByteReceived.AddRange(bufferAfterBegin);

                    NextReceiveDataFilter = this;
                    rest = 0;
                    return(null);
                }
            }
        }