Преглед изворни кода

Optimize task management

release/3.x.x
Christian Kratky пре 7 година
родитељ
комит
314fca3f89
2 измењених фајлова са 24 додато и 23 уклоњено
  1. +21
    -21
      MQTTnet.Core/Client/MqttClient.cs
  2. +3
    -2
      MQTTnet.nuspec

+ 21
- 21
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)


+ 3
- 2
MQTTnet.nuspec Прегледај датотеку

@@ -12,8 +12,9 @@
<description>MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description>
<releaseNotes>* [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)</releaseNotes>
* [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</releaseNotes>
<copyright>Copyright Christian Kratky 2016-2017</copyright>
<tags>MQTT MQTTClient MQTTServer MQTTBroker Broker</tags>
</metadata>


Loading…
Откажи
Сачувај