コード例 #1
0
 private bool FilterDoublePackets(PayloadPacketHeader header)
 {
     if (_packetIdCache.Contains(header.PacketId))
     {
         return(false);
     }
     _packetIdCache.Enqueue(header.PacketId);
     while (_packetIdCache.Count > _maxPacketIdCacheSize)
     {
         _packetIdCache.TryDequeue(out var id);
     }
     return(true);
 }
コード例 #2
0
        private void OnData(V2ExtensionPacket v2ExtensionPacket)
        {
            try
            {
                Interlocked.Increment(ref _txPacketsCounter);

                PayloadPacketHeader packetInfo = null;

                using (var ms = new MemoryStream(v2ExtensionPacket.Payload.Payload))
                {
                    packetInfo = PayloadHelper.ReadData <PayloadPacketHeader>(ms);

                    if (FilterDoublePackets(packetInfo) == false)
                    {
                        Interlocked.Increment(ref _doublePacketsCounter);
                        return;
                    }


                    var data = new byte[ms.Length - ms.Position];
                    ms.Read(data, 0, data.Length);
                    var pkt = new V2Packet
                    {
                        Device = new DeviceIdentity {
                            ComponentId = v2ExtensionPacket.ComponenId, SystemId = v2ExtensionPacket.SystemId
                        },
                        Header      = packetInfo,
                        Data        = data,
                        MessageType = v2ExtensionPacket.Payload.MessageType,
                    };
                    _onData.OnNext(pkt);
                }
            }
            catch (Exception e)
            {
                _logger.Warn($"Error execute data:{e.Message}");
            }
        }