mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 02:53:06 +08:00
Avoid unnecessary enumeration for every packet
This commit is contained in:
@@ -15,7 +15,7 @@ namespace MQTTnet.Adapter
|
||||
|
||||
Task DisconnectAsync(TimeSpan timeout);
|
||||
|
||||
Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, IEnumerable<MqttBasePacket> packets);
|
||||
Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket[] packets);
|
||||
|
||||
Task<MqttBasePacket> ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -51,40 +51,44 @@ namespace MQTTnet.Adapter
|
||||
return ExecuteAndWrapExceptionAsync(() => _channel.DisconnectAsync().TimeoutAfter(timeout));
|
||||
}
|
||||
|
||||
public Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, IEnumerable<MqttBasePacket> packets)
|
||||
public async Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket[] packets)
|
||||
{
|
||||
for(var i=0;i<packets.Length;i++)
|
||||
{
|
||||
await SendPacketsAsync(timeout, cancellationToken, packets[i]).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Task SendPacketsAsync(TimeSpan timeout, CancellationToken cancellationToken, MqttBasePacket packet)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
if (packet == null)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
return ExecuteAndWrapExceptionAsync(async () =>
|
||||
{
|
||||
foreach (var packet in packets)
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.Verbose<MqttChannelAdapter>("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);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
_logger.Verbose<MqttChannelAdapter>("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);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user