From 314fca3f899685f532df92e857c15b0eb6f7615c Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 8 Jun 2017 14:25:52 +0200 Subject: [PATCH] Optimize task management --- MQTTnet.Core/Client/MqttClient.cs | 42 +++++++++++++++---------------- MQTTnet.nuspec | 5 ++-- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/MQTTnet.Core/Client/MqttClient.cs b/MQTTnet.Core/Client/MqttClient.cs index 72b718e..cc3ebee 100644 --- a/MQTTnet.Core/Client/MqttClient.cs +++ b/MQTTnet.Core/Client/MqttClient.cs @@ -191,34 +191,30 @@ namespace MQTTnet.Core.Client } } - private async void ProcessReceivedPacketAsync(MqttBasePacket mqttPacket) + private Task ProcessReceivedPacketAsync(MqttBasePacket mqttPacket) { try { if (mqttPacket is MqttPingReqPacket) { - await SendAsync(new MqttPingRespPacket()); - return; + return SendAsync(new MqttPingRespPacket()); } if (mqttPacket is MqttDisconnectPacket) { - await DisconnectAsync(); - return; + return DisconnectAsync(); } var publishPacket = mqttPacket as MqttPublishPacket; if (publishPacket != null) { - await ProcessReceivedPublishPacket(publishPacket); - return; + return ProcessReceivedPublishPacket(publishPacket); } var pubRelPacket = mqttPacket as MqttPubRelPacket; if (pubRelPacket != null) { - await ProcessReceivedPubRelPacket(pubRelPacket); - return; + return ProcessReceivedPubRelPacket(pubRelPacket); } _packetDispatcher.Dispatch(mqttPacket); @@ -227,6 +223,8 @@ namespace MQTTnet.Core.Client { MqttTrace.Error(nameof(MqttClient), exception, "Error while processing received packet."); } + + return Task.FromResult(0); } private void FireApplicationMessageReceivedEvent(MqttPublishPacket publishPacket) @@ -246,25 +244,27 @@ namespace MQTTnet.Core.Client ApplicationMessageReceived?.Invoke(this, new MqttApplicationMessageReceivedEventArgs(applicationMessage)); } - private async Task ProcessReceivedPublishPacket(MqttPublishPacket publishPacket) + private Task ProcessReceivedPublishPacket(MqttPublishPacket publishPacket) { if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce) { FireApplicationMessageReceivedEvent(publishPacket); + return Task.FromResult(0); } - else + + if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce) { - if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce) - { - FireApplicationMessageReceivedEvent(publishPacket); - await SendAsync(new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier }); - } - else if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce) - { - _pendingExactlyOncePublishPackets[publishPacket.PacketIdentifier] = publishPacket; - await SendAsync(new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier }); - } + FireApplicationMessageReceivedEvent(publishPacket); + return SendAsync(new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier }); } + + if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce) + { + _pendingExactlyOncePublishPackets[publishPacket.PacketIdentifier] = publishPacket; + return SendAsync(new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier }); + } + + throw new InvalidOperationException(); } private async Task ProcessReceivedPubRelPacket(MqttPubRelPacket pubRelPacket) diff --git a/MQTTnet.nuspec b/MQTTnet.nuspec index 2ff14f0..5acb5b5 100644 --- a/MQTTnet.nuspec +++ b/MQTTnet.nuspec @@ -12,8 +12,9 @@ MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). * [Core] Any exception while accessing the underlying data source is now rethrown as "MqttCommunicationException". * [Core] Extended exception information when an invalid packet type is received. -* [Server] Added TLS 1.2 support (thanks to Zazzmatazz) -* [Client] Added TLS 1.2 support (thanks to Zazzmatazz) +* [Server] Added TLS 1.2 support (but not for UWP) (thanks to Zazzmatazz) +* [Client] Added TLS 1.2 support (thanks to Zazzmatazz) +* [Core] Optimized async task management Copyright Christian Kratky 2016-2017 MQTT MQTTClient MQTTServer MQTTBroker Broker