示例#1
0
        public List <NecPacket> Read(byte[] data, NecClient client)
        {
            List <NecPacket> packets = new List <NecPacket>();

            if (_buffer == null)
            {
                _buffer = BufferProvider.Provide(data);
            }
            else
            {
                _buffer.SetPositionEnd();
                _buffer.WriteBytes(data);
            }

            _buffer.Position = _position;

            bool read = true;

            while (read)
            {
                read = false;

                if (!_readPacketLengthType &&
                    _buffer.Size - _buffer.Position > PacketLengthTypeSize)
                {
                    byte lengthType = _buffer.ReadByte();
                    if (!Enum.IsDefined(typeof(PacketLengthType), lengthType))
                    {
                        _logger.Error($"PacketLengthType: '{lengthType}' not found");
                        byte[] dataDump = _buffer.GetBytes(_buffer.Position - 1, _buffer.Size);
                        _logger.LogErrorPacket(client, dataDump, null);
                        Reset();
                        return(packets);
                    }

                    _readPacketLengthType = true;
                    _packetLengthType     = (PacketLengthType)lengthType;
                    _headerSize           = CalculateHeaderSize(_packetLengthType);
                }

                if (_readPacketLengthType &&
                    !_readHeader &&
                    _buffer.Size - _buffer.Position >= _headerSize - PacketLengthTypeSize)
                {
                    // TODO aquire 1st byte differently incase -1 doesnt work
                    _header = _buffer.GetBytes(_buffer.Position - PacketLengthTypeSize, _headerSize);

                    switch (_packetLengthType)
                    {
                    case PacketLengthType.Byte:
                    {
                        _dataSize = _buffer.ReadByte();
                        break;
                    }

                    case PacketLengthType.UInt16:
                    {
                        _dataSize = _buffer.ReadUInt16();
                        break;
                    }

                    case PacketLengthType.UInt32:
                    {
                        _dataSize = _buffer.ReadUInt32();
                        break;
                    }

                    default:
                    {
                        // TODO update arrowgene service to read uint24 && int24
                        _logger.Error($"PacketLengthType: '{_packetLengthType}' not supported");
                        Reset();
                        return(packets);
                    }
                    }

                    _dataSize  -= PacketIdSize;
                    _id         = _buffer.ReadUInt16();
                    _readHeader = true;
                }

                if (_readPacketLengthType &&
                    _readHeader &&
                    _buffer.Size - _buffer.Position >= _dataSize)
                {
                    // TODO update arrowgene service to read uint
                    byte[]    packetData = _buffer.ReadBytes((int)_dataSize);
                    IBuffer   buffer     = BufferProvider.Provide(packetData);
                    NecPacket packet     = new NecPacket(_id, buffer);
                    packet.Header = _header;
                    packets.Add(packet);
                    _readPacketLengthType = false;
                    _readHeader           = false;
                    read = _buffer.Position != _buffer.Size;
                }
            }

            if (_buffer.Position == _buffer.Size)
            {
                Reset();
            }
            else
            {
                _position = _buffer.Position;
            }

            return(packets);
        }