Parcourir la source

Refactoring

release/3.x.x
Christian Kratky il y a 7 ans
Parent
révision
df1dd0eec7
2 fichiers modifiés avec 37 ajouts et 40 suppressions
  1. +29
    -23
      MQTTnet.Core/Server/MqttClientSession.cs
  2. +8
    -17
      MQTTnet.Core/Server/MqttTopicFilterComparer.cs

+ 29
- 23
MQTTnet.Core/Server/MqttClientSession.cs Voir le fichier

@@ -15,7 +15,7 @@ namespace MQTTnet.Core.Server
public sealed class MqttClientSession : IDisposable
{
private readonly HashSet<ushort> _unacknowledgedPublishPackets = new HashSet<ushort>();
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)


+ 8
- 17
MQTTnet.Core/Server/MqttTopicFilterComparer.cs Voir le fichier

@@ -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;
}
}
}

Chargement…
Annuler
Enregistrer