浏览代码

Added missing ConfigureAwaits

release/3.x.x
Christian Kratky 7 年前
父节点
当前提交
7dfc19e588
共有 9 个文件被更改,包括 46 次插入33 次删除
  1. +1
    -1
      MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs
  2. +2
    -2
      MQTTnet.Core/Client/MqttClient.cs
  3. +6
    -2
      MQTTnet.Core/Diagnostics/MqttNetLogger.cs
  4. +4
    -2
      MQTTnet.Core/Diagnostics/MqttNetTrace.cs
  5. +10
    -7
      MQTTnet.Core/Serializer/MqttPacketSerializer.cs
  6. +3
    -3
      MQTTnet.Core/Serializer/MqttPacketWriter.cs
  7. +2
    -2
      MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs
  8. +10
    -10
      MQTTnet.Core/Server/MqttClientSession.cs
  9. +8
    -4
      Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs

+ 1
- 1
MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs 查看文件

@@ -87,7 +87,7 @@ namespace MQTTnet.Core.Adapter
{
try
{
await _semaphore.WaitAsync(cancellationToken);
await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

foreach (var packet in packets)
{


+ 2
- 2
MQTTnet.Core/Client/MqttClient.cs 查看文件

@@ -151,7 +151,7 @@ namespace MQTTnet.Core.Client
case MqttQualityOfServiceLevel.AtMostOnce:
{
// No packet identifier is used for QoS 0 [3.3.2.2 Packet Identifier]
await _adapter.SendPacketsAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token, qosPackets);
await _adapter.SendPacketsAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token, qosPackets).ConfigureAwait(false);
break;
}
case MqttQualityOfServiceLevel.AtLeastOnce:
@@ -159,7 +159,7 @@ namespace MQTTnet.Core.Client
foreach (var publishPacket in qosPackets)
{
publishPacket.PacketIdentifier = GetNewPacketIdentifier();
await SendAndReceiveAsync<MqttPubAckPacket>(publishPacket);
await SendAndReceiveAsync<MqttPubAckPacket>(publishPacket).ConfigureAwait(false);
}

break;


+ 6
- 2
MQTTnet.Core/Diagnostics/MqttNetLogger.cs 查看文件

@@ -21,15 +21,19 @@ namespace MQTTnet.Core.Diagnostics
throw new ArgumentNullException(nameof(formatter));
}

var message = formatter(state, exception);
if (!MqttNetTrace.HasListeners)
{
return;
}

var message = formatter(state, exception);
var traceMessage = new MqttNetTraceMessage(DateTime.Now, Environment.CurrentManagedThreadId, _categoryName, logLevel, message, exception);
_mqttNetTrace.Publish(traceMessage);
}

public bool IsEnabled(LogLevel logLevel)
{
return true;
return MqttNetTrace.HasListeners;
}

//not supported: async local requires netstandard1.3


+ 4
- 2
MQTTnet.Core/Diagnostics/MqttNetTrace.cs 查看文件

@@ -10,9 +10,11 @@ namespace MQTTnet.Core.Diagnostics

public static event EventHandler<MqttNetTraceMessagePublishedEventArgs> TraceMessagePublished;

public void Publish(MqttNetTraceMessage msg)
public static bool HasListeners => TraceMessagePublished != null;

public void Publish(MqttNetTraceMessage traceMessage)
{
TraceMessagePublished?.Invoke(this, new MqttNetTraceMessagePublishedEventArgs(msg));
TraceMessagePublished?.Invoke(this, new MqttNetTraceMessagePublishedEventArgs(traceMessage));
}

public void Dispose()


+ 10
- 7
MQTTnet.Core/Serializer/MqttPacketSerializer.cs 查看文件

@@ -24,15 +24,18 @@ namespace MQTTnet.Core.Serializer
using (var stream = new MemoryStream())
using (var writer = new MqttPacketWriter(stream))
{
var header = new List<byte> { SerializePacket(packet, writer) };
var fixedHeader = SerializePacket(packet, writer);
var headerBuffer = new List<byte> { fixedHeader };
MqttPacketWriter.WriteRemainingLength((int)stream.Length, headerBuffer);

var header = headerBuffer.ToArray();
var body = stream.ToArray();
MqttPacketWriter.BuildLengthHeader(body.Length, header);
var headerArray = header.ToArray();
var writeBuffer = new byte[header.Count + body.Length];
Buffer.BlockCopy(headerArray, 0, writeBuffer, 0, headerArray.Length);
Buffer.BlockCopy(body, 0, writeBuffer, headerArray.Length, body.Length);

return writeBuffer;
var buffer = new byte[header.Length + body.Length];
Buffer.BlockCopy(header, 0, buffer, 0, header.Length);
Buffer.BlockCopy(body, 0, buffer, header.Length, body.Length);

return buffer;
}
}



+ 3
- 3
MQTTnet.Core/Serializer/MqttPacketWriter.cs 查看文件

@@ -56,11 +56,11 @@ namespace MQTTnet.Core.Serializer
Write(value);
}

public static void BuildLengthHeader(int length, List<byte> header)
public static void WriteRemainingLength(int length, List<byte> target)
{
if (length == 0)
{
header.Add(0);
target.Add(0);
return;
}

@@ -75,7 +75,7 @@ namespace MQTTnet.Core.Serializer
encodedByte = encodedByte | 128;
}

header.Add((byte)encodedByte);
target.Add((byte)encodedByte);
} while (x > 0);
}
}


+ 2
- 2
MQTTnet.Core/Server/MqttClientRetainedMessagesManager.cs 查看文件

@@ -54,7 +54,7 @@ namespace MQTTnet.Core.Server
{
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));

await _gate.WaitAsync();
await _gate.WaitAsync().ConfigureAwait(false);
try
{
var saveIsRequired = false;
@@ -108,7 +108,7 @@ namespace MQTTnet.Core.Server
{
var retainedMessages = new List<MqttApplicationMessage>();

await _gate.WaitAsync();
await _gate.WaitAsync().ConfigureAwait(false);
try
{
foreach (var retainedMessage in _retainedMessages.Values)


+ 10
- 10
MQTTnet.Core/Server/MqttClientSession.cs 查看文件

@@ -29,11 +29,11 @@ namespace MQTTnet.Core.Server
private MqttApplicationMessage _willMessage;

public MqttClientSession(
string clientId,
string clientId,
IOptions<MqttServerOptions> options,
MqttClientSessionsManager sessionsManager,
MqttClientSubscriptionsManager subscriptionsManager,
ILogger<MqttClientSession> logger,
ILogger<MqttClientSession> logger,
ILogger<MqttClientPendingMessagesQueue> messageQueueLogger,
IMqttClientRetainedMessageManager clientRetainedMessageManager)
{
@@ -65,7 +65,7 @@ namespace MQTTnet.Core.Server
_cancellationTokenSource = new CancellationTokenSource();

_pendingMessagesQueue.Start(adapter, _cancellationTokenSource.Token);
await ReceivePacketsAsync(adapter, _cancellationTokenSource.Token);
await ReceivePacketsAsync(adapter, _cancellationTokenSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
@@ -175,7 +175,7 @@ namespace MQTTnet.Core.Server
{
return adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, _subscriptionsManager.Unsubscribe(unsubscribePacket));
}
if (packet is MqttDisconnectPacket || packet is MqttConnectPacket)
{
Stop();
@@ -192,19 +192,19 @@ namespace MQTTnet.Core.Server
{
var subscribeResult = _subscriptionsManager.Subscribe(subscribePacket, ClientId);

await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, subscribeResult.ResponsePacket);
await EnqueueSubscribedRetainedMessagesAsync(subscribePacket);
await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, subscribeResult.ResponsePacket).ConfigureAwait(false);
await EnqueueSubscribedRetainedMessagesAsync(subscribePacket).ConfigureAwait(false);

if (subscribeResult.CloseConnection)
{
await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, new MqttDisconnectPacket());
await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken, new MqttDisconnectPacket()).ConfigureAwait(false);
Stop();
}
}

private async Task EnqueueSubscribedRetainedMessagesAsync(MqttSubscribePacket subscribePacket)
{
var retainedMessages = await _clientRetainedMessageManager.GetSubscribedMessagesAsync(subscribePacket);
var retainedMessages = await _clientRetainedMessageManager.GetSubscribedMessagesAsync(subscribePacket).ConfigureAwait(false);
foreach (var publishPacket in retainedMessages)
{
EnqueuePublishPacket(publishPacket.ToPublishPacket());
@@ -225,7 +225,7 @@ namespace MQTTnet.Core.Server

if (applicationMessage.Retain)
{
await _clientRetainedMessageManager.HandleMessageAsync(ClientId, applicationMessage);
await _clientRetainedMessageManager.HandleMessageAsync(ClientId, applicationMessage).ConfigureAwait(false);
}

switch (applicationMessage.QualityOfServiceLevel)
@@ -255,7 +255,7 @@ namespace MQTTnet.Core.Server
_sessionsManager.DispatchApplicationMessage(this, applicationMessage);

await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, cancellationToken,
new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier });
new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier }).ConfigureAwait(false);

return;
}


+ 8
- 4
Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs 查看文件

@@ -41,7 +41,7 @@ namespace MQTTnet.TestApp.NetCore
.AddLogging()
.BuildServiceProvider();

services.GetService<ILoggerFactory>().AddConsole(LogLevel.Warning, true);
//services.GetService<ILoggerFactory>().AddConsole(LogLevel.Warning, true);

Console.WriteLine("Press 'c' for concurrent sends. Otherwise in one batch.");
var concurrent = Console.ReadKey(true).KeyChar == 'c';
@@ -120,11 +120,15 @@ namespace MQTTnet.TestApp.NetCore
stopwatch.Stop();
Console.WriteLine($"Sent 10.000 messages within {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / (float)testMessageCount} ms / message).");

stopwatch.Restart();

var messages = new[] { message };
var sentMessagesCount = 0;

stopwatch.Restart();

while (stopwatch.ElapsedMilliseconds < 1000)
{
await client.PublishAsync(message);
await client.PublishAsync(messages).ConfigureAwait(false);
sentMessagesCount++;
}

@@ -180,7 +184,7 @@ namespace MQTTnet.TestApp.NetCore
{
Topic = "A/B/C",
Payload = Encoding.UTF8.GetBytes("Hello World"),
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
};
}



正在加载...
取消
保存