예제 #1
0
        private async Task <TResponsePacket> SendAndReceiveAsync <TResponsePacket>(MqttBasePacket requestPacket) where TResponsePacket : MqttBasePacket
        {
            bool ResponsePacketSelector(MqttBasePacket p)
            {
                var p1 = p as TResponsePacket;

                if (p1 == null)
                {
                    return(false);
                }

                var pi1 = requestPacket as IPacketWithIdentifier;
                var pi2 = p as IPacketWithIdentifier;

                if (pi1 != null && pi2 != null)
                {
                    if (pi1.PacketIdentifier != pi2.PacketIdentifier)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            await _adapter.SendPacketAsync(requestPacket, _options.DefaultCommunicationTimeout);

            return((TResponsePacket)await _packetDispatcher.WaitForPacketAsync(ResponsePacketSelector, _options.DefaultCommunicationTimeout));
        }
예제 #2
0
        private async Task <TResponsePacket> SendAndReceiveAsync <TResponsePacket>(MqttBasePacket requestPacket) where TResponsePacket : MqttBasePacket
        {
            var packetAwaiter = _packetDispatcher.WaitForPacketAsync(requestPacket, typeof(TResponsePacket), _options.CommunicationTimeout);
            await _adapter.SendPacketsAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token, requestPacket).ConfigureAwait(false);

            return((TResponsePacket)await packetAwaiter.ConfigureAwait(false));
        }