From df1dd0eec7ecc01ac4ad025ea0ce3ab916cb6387 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Sat, 21 Oct 2017 20:39:38 +0200 Subject: [PATCH] Refactoring --- MQTTnet.Core/Server/MqttClientSession.cs | 52 +++++++++++-------- .../Server/MqttTopicFilterComparer.cs | 25 +++------ 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/MQTTnet.Core/Server/MqttClientSession.cs b/MQTTnet.Core/Server/MqttClientSession.cs index 9b6cce5..969ec01 100644 --- a/MQTTnet.Core/Server/MqttClientSession.cs +++ b/MQTTnet.Core/Server/MqttClientSession.cs @@ -15,7 +15,7 @@ namespace MQTTnet.Core.Server public sealed class MqttClientSession : IDisposable { private readonly HashSet _unacknowledgedPublishPackets = new HashSet(); - + private readonly MqttClientSubscriptionsManager _subscriptionsManager = new MqttClientSubscriptionsManager(); private readonly MqttClientSessionsManager _mqttClientSessionsManager; private readonly MqttClientPendingMessagesQueue _pendingMessagesQueue; @@ -186,34 +186,40 @@ namespace MQTTnet.Core.Server await _mqttClientSessionsManager.RetainedMessagesManager.HandleMessageAsync(ClientId, publishPacket); } - if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce) + switch (publishPacket.QualityOfServiceLevel) { - _mqttClientSessionsManager.DispatchPublishPacket(this, publishPacket); - return; - } + case MqttQualityOfServiceLevel.AtMostOnce: + { + _mqttClientSessionsManager.DispatchPublishPacket(this, publishPacket); + return; + } + case MqttQualityOfServiceLevel.AtLeastOnce: + { + _mqttClientSessionsManager.DispatchPublishPacket(this, publishPacket); - if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce) - { - _mqttClientSessionsManager.DispatchPublishPacket(this, publishPacket); - await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, _cancellationTokenSource.Token, new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier }); - return; - } + await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, _cancellationTokenSource.Token, + new MqttPubAckPacket { PacketIdentifier = publishPacket.PacketIdentifier }); - if (publishPacket.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce) - { - // QoS 2 is implement as method "B" [4.3.3 QoS 2: Exactly once delivery] - lock (_unacknowledgedPublishPackets) - { - _unacknowledgedPublishPackets.Add(publishPacket.PacketIdentifier); - } + return; + } + case MqttQualityOfServiceLevel.ExactlyOnce: + { + // QoS 2 is implement as method "B" [4.3.3 QoS 2: Exactly once delivery] + lock (_unacknowledgedPublishPackets) + { + _unacknowledgedPublishPackets.Add(publishPacket.PacketIdentifier); + } - _mqttClientSessionsManager.DispatchPublishPacket(this, publishPacket); + _mqttClientSessionsManager.DispatchPublishPacket(this, publishPacket); - await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, _cancellationTokenSource.Token, new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier }); - return; - } + await adapter.SendPacketsAsync(_options.DefaultCommunicationTimeout, _cancellationTokenSource.Token, + new MqttPubRecPacket { PacketIdentifier = publishPacket.PacketIdentifier }); - throw new MqttCommunicationException("Received a not supported QoS level."); + return; + } + default: + throw new MqttCommunicationException("Received a not supported QoS level."); + } } private Task HandleIncomingPubRelPacketAsync(IMqttCommunicationAdapter adapter, MqttPubRelPacket pubRelPacket) diff --git a/MQTTnet.Core/Server/MqttTopicFilterComparer.cs b/MQTTnet.Core/Server/MqttTopicFilterComparer.cs index b74852c..987a71f 100644 --- a/MQTTnet.Core/Server/MqttTopicFilterComparer.cs +++ b/MQTTnet.Core/Server/MqttTopicFilterComparer.cs @@ -4,7 +4,7 @@ namespace MQTTnet.Core.Server { public static class MqttTopicFilterComparer { - private const char TopicLevelSeparator = '/'; + private static readonly char[] TopicLevelSeparator = { '/' }; public static bool IsMatch(string topic, string filter) { @@ -16,19 +16,15 @@ namespace MQTTnet.Core.Server return true; } - var fragmentsTopic = topic.Split(new[] { TopicLevelSeparator }, StringSplitOptions.None); - var fragmentsFilter = filter.Split(new[] { TopicLevelSeparator }, StringSplitOptions.None); + var fragmentsTopic = topic.Split(TopicLevelSeparator, StringSplitOptions.None); + var fragmentsFilter = filter.Split(TopicLevelSeparator, StringSplitOptions.None); for (var i = 0; i < fragmentsFilter.Length; i++) { - if (fragmentsFilter[i] == "+") + switch (fragmentsFilter[i]) { - continue; - } - - if (fragmentsFilter[i] == "#" && i == fragmentsFilter.Length - 1) - { - return true; + case "+": continue; + case "#" when i == fragmentsFilter.Length - 1: return true; } if (i >= fragmentsTopic.Length) @@ -36,18 +32,13 @@ namespace MQTTnet.Core.Server return false; } - if (!string.Equals(fragmentsFilter[i], fragmentsTopic[i])) + if (!string.Equals(fragmentsFilter[i], fragmentsTopic[i], StringComparison.Ordinal)) { return false; } } - if (fragmentsTopic.Length > fragmentsFilter.Length) - { - return false; - } - - return true; + return fragmentsTopic.Length <= fragmentsFilter.Length; } } }