Beispiel #1
0
        public override IMessageEntity Resolve()
        {
            DataBufferObject _receiveData = BufferStream;

            //查找开头
            int headerIndex = _receiveData.IndexOf(_headerBytes);

            if (headerIndex >= 0)
            {
                //有效值
                if (headerIndex + 8 > _receiveData.Buffer.Count)
                {
                    Thread.Sleep(10);
                    return(null);
                }
                else
                {
                    //取数据
                    byte[] results = _receiveData.GetAndRemoveRangeWithLock(headerIndex, 8);
                    return(new IMessageEntity(results, DateTime.Now.Ticks, 8, null));
                }
            }
            else
            {
                //无效的值,丢弃这些数据
                _receiveData.ClearWithLock();
                return(null);
            }
        }
Beispiel #2
0
        public override IMessageEntity Resolve()
        {
            DataBufferObject _recievedData = BufferStream;

            if (!resAssembling)
            {
                while (headerIndex + 3 < _recievedData.Buffer.Count && !(_recievedData.Buffer[headerIndex] == 0xFD && _recievedData.Buffer[headerIndex + 1] == 0x00 && _recievedData.Buffer[headerIndex + 2] == 0x80 && _recievedData.Buffer[headerIndex + 3] == 0x00))
                {
                    headerIndex++;
                }

                if (headerIndex >= _recievedData.Buffer.Count)
                {
                    _recievedData.ClearWithLock();
                }

                resAssembling = true;
            }

            if (headerIndex + 2 >= _recievedData.Buffer.Count)
            {
                Thread.Sleep(10);
            }

            // 帧长度=数据区长度+1
            int length = 264;

            if (headerIndex + length > _recievedData.Buffer.Count)
            {
                Thread.Sleep(10);
            }

            if (_recievedData.Buffer.Count >= 264)
            {
                return(new IMessageEntity(_recievedData.GetAndRemoveRangeWithLock(headerIndex, 264), 0, 264, null));
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        public override IMessageEntity Resolve()
        {
            DataBufferObject _recievedData = BufferStream;

            try
            {
                //尝试查找包头
                headerIndex = _recievedData.IndexOf(_headerBytes);
                if (headerIndex >= 0)
                {
                    if (headerIndex + 8 >= _recievedData.Buffer.Count)
                    {
                        Thread.Sleep(10);
                    }

                    int length = BitConverter.ToInt16(new byte[] { _recievedData.Buffer[headerIndex + 3], _recievedData.Buffer[headerIndex + 4] }, 0);
                    if (length >= 0)
                    {
                        if (headerIndex + length + 8 > _recievedData.Buffer.Count)
                        {
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        //无效数据
                        _recievedData.ClearWithLock();
                    }

                    if (_recievedData.Buffer.Count >= length + 8)
                    {
                        byte[] msg = _recievedData.Buffer.GetRange(headerIndex, length + 8).ToArray();

                        int newOffset = IRobotMessageDataAdapter.IndexOf(msg, _headerBytes.Length + 1, _headerBytes);
                        if (newOffset < 0)
                        {
                            //解析SeqID并且回复确认
                            int id = 0;
                            if (msg[msg.Length - 1] == Utils.CalcCheckCode(new List <byte>(msg)))
                            {
                                //设置SeqId
                                id = BitConverter.ToInt16(new byte[] { msg[5], msg[6] }, 0);
                                Form1.AIUIConnectionObj.packetBuilder.setSeqId(id);

                                //自动回复
                                Form1.AIUIConnectionObj.SendConfirmMessage();
                            }

                            //取数据
                            byte[] bytes = new byte[0];
                            if (msg[2] == 0x04)
                            {
                                bytes = _recievedData.GetAndRemoveRangeWithLock(headerIndex + 7, length).ToArray();
                                return(new IMessageEntity(bytes, id, bytes.Length, null));
                            }
                            else
                            {
                                _recievedData.RemoveRangeWithLock(0, headerIndex + length + 8);
                                return(null);
                            }
                        }
                        else
                        {
                            //可能已经丢包了,废弃这个包
                            _recievedData.RemoveRangeWithLock(0, headerIndex + newOffset);
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    //无效数据
                    _recievedData.ClearWithLock();
                    return(null);
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        public override IMessageEntity Resolve()
        {
            DataBufferObject _recievedData = BufferStream;

            try
            {
                //尝试查找包头
                headerIndex = _recievedData.IndexOf(_headerBytes);
                if (headerIndex >= 0)
                {
                    if (headerIndex + 8 >= _recievedData.Buffer.Count)
                    {
                        Thread.Sleep(10);
                        return(null);
                    }
                    else
                    {
                        int length = ((_recievedData.Buffer[headerIndex + 4] & 0xff) << 8) + (_recievedData.Buffer[headerIndex + 3] & 0xff);
                        if (length > 0)
                        {
                            if (headerIndex + length + 8 > _recievedData.Buffer.Count)
                            {
                                Thread.Sleep(10);
                            }
                        }
                        else
                        {
                            //无效数据
                            _recievedData.ClearWithLock();
                        }

                        if (_recievedData.Buffer.Count >= length + 8)
                        {
                            byte[] msg       = _recievedData.Buffer.GetRange(headerIndex, length + 8).ToArray();
                            int    newOffset = IRobotMessageDataAdapter.IndexOf(msg, _headerBytes.Length + 1, _headerBytes);
                            if (newOffset < 0)
                            {
                                int id = 0;
                                //解析SeqID并且回复确认
                                if (msg[msg.Length - 1] == Utils.CalcCheckCode(new List <byte>(msg)))
                                {
                                    //设置SeqId
                                    id = ((msg[6] & 0xff) << 8) + msg[5];
                                    MainService.AiuiOnlineService.AiuiConnection.packetBuilder.setSeqId(id);

                                    //自动回复
                                    MainService.AiuiOnlineService.AiuiConnection.SendConfirmMessage();
                                }

                                //取数据
                                byte[] bytes = new byte[0];
                                if (msg[2] == 0x04)
                                {
                                    bytes = _recievedData.Buffer.GetRange(headerIndex + 7, length).ToArray();

                                    //删除解析过的数据
                                    _recievedData.RemoveRangeWithLock(0, headerIndex + length + 8);

                                    return(new IMessageEntity(bytes, id, bytes.Length, null));
                                }
                                else
                                {
                                    //删除解析过的数据
                                    _recievedData.RemoveRangeWithLock(0, headerIndex + length + 8);

                                    return(null);
                                }
                            }
                            else
                            {
                                //可能已经丢包了,废弃这个包
                                _recievedData.RemoveRangeWithLock(0, headerIndex + newOffset);
                                return(null);
                            }
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                else
                {
                    //无效数据
                    _recievedData.ClearWithLock();

                    return(null);
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }