From 53007425435113bdc17f348862d40ba4c53e20c7 Mon Sep 17 00:00:00 2001 From: Israel Lot Date: Mon, 16 Apr 2018 14:30:52 -0300 Subject: [PATCH] Avoid unnecessary enumeration for every packet --- .../Adapter/IMqttChannelAdapter.cs | 2 +- .../Adapter/MqttChannelAdapter.cs | 54 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Adapter/IMqttChannelAdapter.cs b/Frameworks/MQTTnet.NetStandard/Adapter/IMqttChannelAdapter.cs index 7a5c02a..8f03e65 100644 --- a/Frameworks/MQTTnet.NetStandard/Adapter/IMqttChannelAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Adapter/IMqttChannelAdapter.cs @@ -15,7 +15,7 @@ namespace MQTTnet.Adapter Task DisconnectAsync(TimeSpan timeout); - Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, IEnumerable packets); + Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket[] packets); Task ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken); } diff --git a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs index 787231e..7fffa16 100644 --- a/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Adapter/MqttChannelAdapter.cs @@ -51,40 +51,44 @@ namespace MQTTnet.Adapter return ExecuteAndWrapExceptionAsync(() => _channel.DisconnectAsync().TimeoutAfter(timeout)); } - public Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, IEnumerable packets) + public async Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket[] packets) + { + for(var i=0;i { - foreach (var packet in packets) + if (cancellationToken.IsCancellationRequested) { - if (cancellationToken.IsCancellationRequested) - { - return; - } - - if (packet == null) - { - continue; - } - - _logger.Verbose("TX >>> {0} [Timeout={1}]", packet, timeout); + return; + } + - var packetData = PacketSerializer.Serialize(packet); - if (cancellationToken.IsCancellationRequested) - { - return; - } - await _channel.SendStream.WriteAsync( - packetData.Array, - packetData.Offset, - (int)packetData.Count, - cancellationToken).ConfigureAwait(false); + _logger.Verbose("TX >>> {0} [Timeout={1}]", packet, timeout); + var packetData = PacketSerializer.Serialize(packet); + if (cancellationToken.IsCancellationRequested) + { + return; } - - + await _channel.SendStream.WriteAsync( + packetData.Array, + packetData.Offset, + (int)packetData.Count, + cancellationToken).ConfigureAwait(false); }); }