mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 11:03:07 +08:00
Refactor code.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Internal;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.Server.Internal
|
||||
{
|
||||
@@ -12,6 +14,8 @@ namespace MQTTnet.Server.Internal
|
||||
{
|
||||
readonly MqttPacketBus _packetBus = new MqttPacketBus();
|
||||
|
||||
readonly Dictionary<ushort, MqttPublishPacket> _publishPacketsWithoutAcknowledge = new Dictionary<ushort, MqttPublishPacket>();
|
||||
|
||||
readonly IMqttServerOptions _serverOptions;
|
||||
readonly MqttClientSessionsManager _clientSessionsManager;
|
||||
|
||||
@@ -24,52 +28,70 @@ namespace MQTTnet.Server.Internal
|
||||
{
|
||||
_serverOptions = serverOptions ?? throw new ArgumentNullException(nameof(serverOptions));
|
||||
_clientSessionsManager = clientSessionsManager ?? throw new ArgumentNullException(nameof(clientSessionsManager));
|
||||
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
|
||||
Id = clientId ?? throw new ArgumentNullException(nameof(clientId));
|
||||
Items = items ?? throw new ArgumentNullException(nameof(items));
|
||||
|
||||
SubscriptionsManager = new MqttClientSubscriptionsManager(this, serverOptions, eventDispatcher, retainedMessagesManager);
|
||||
//ApplicationMessagesQueue = new MqttClientSessionApplicationMessagesQueue(serverOptions);
|
||||
}
|
||||
|
||||
public event EventHandler Deleted;
|
||||
|
||||
public string ClientId { get; }
|
||||
public string Id { get; }
|
||||
|
||||
public MqttPacketIdentifierProvider PacketIdentifierProvider { get; } = new MqttPacketIdentifierProvider();
|
||||
|
||||
public DateTime CreatedTimestamp { get; } = DateTime.UtcNow;
|
||||
|
||||
public bool IsCleanSession { get; set; } = true;
|
||||
//public bool IsCleanSession { get; set; } = true;
|
||||
|
||||
public MqttApplicationMessage WillMessage { get; set; }
|
||||
public MqttConnectPacket LatestConnectPacket { get; set; }
|
||||
|
||||
public MqttClientSubscriptionsManager SubscriptionsManager { get; }
|
||||
|
||||
//public MqttClientSessionApplicationMessagesQueue ApplicationMessagesQueue { get; }
|
||||
|
||||
public IDictionary<object, object> Items { get; }
|
||||
|
||||
public void EnqueuePacket(MqttPacketBusItem packet)
|
||||
public bool WillMessageSent { get; set; }
|
||||
|
||||
public long PendingDataPacketsCount => _packetBus.DataPacketsCount;
|
||||
|
||||
public void AcknowledgePublishPacket(ushort packetIdentifier)
|
||||
{
|
||||
if (packet == null) throw new ArgumentNullException(nameof(packet));
|
||||
_publishPacketsWithoutAcknowledge.Remove(packetIdentifier);
|
||||
}
|
||||
|
||||
public void EnqueuePacket(MqttPacketBusItem packetBusItem)
|
||||
{
|
||||
if (packetBusItem == null) throw new ArgumentNullException(nameof(packetBusItem));
|
||||
|
||||
if (_packetBus.PacketsCount >= _serverOptions.MaxPendingMessagesPerClient)
|
||||
{
|
||||
// TODO: Log!
|
||||
return;
|
||||
if (_serverOptions.PendingMessagesOverflowStrategy == MqttPendingMessagesOverflowStrategy.DropNewMessage)
|
||||
{
|
||||
// TODO: Log!
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Implement.
|
||||
}
|
||||
}
|
||||
|
||||
if (packet.Packet is MqttPublishPacket)
|
||||
if (packetBusItem.Packet is MqttPublishPacket publishPacket)
|
||||
{
|
||||
_packetBus.Enqueue(packet, MqttPacketBusPartition.Data);
|
||||
if (publishPacket.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
|
||||
{
|
||||
_publishPacketsWithoutAcknowledge[publishPacket.PacketIdentifier] = publishPacket;
|
||||
}
|
||||
|
||||
_packetBus.Enqueue(packetBusItem, MqttPacketBusPartition.Data);
|
||||
}
|
||||
else if (packet.Packet is MqttPingReqPacket || packet.Packet is MqttPingRespPacket)
|
||||
else if (packetBusItem.Packet is MqttPingReqPacket || packetBusItem.Packet is MqttPingRespPacket)
|
||||
{
|
||||
_packetBus.Enqueue(packet, MqttPacketBusPartition.Health);
|
||||
_packetBus.Enqueue(packetBusItem, MqttPacketBusPartition.Health);
|
||||
}
|
||||
else
|
||||
{
|
||||
_packetBus.Enqueue(packet, MqttPacketBusPartition.Control);
|
||||
_packetBus.Enqueue(packetBusItem, MqttPacketBusPartition.Control);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,13 +102,25 @@ namespace MQTTnet.Server.Internal
|
||||
|
||||
public Task DeleteAsync()
|
||||
{
|
||||
return _clientSessionsManager.DeleteSessionAsync(ClientId);
|
||||
return _clientSessionsManager.DeleteSessionAsync(Id);
|
||||
}
|
||||
|
||||
public void OnDeleted()
|
||||
{
|
||||
Deleted?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Recover()
|
||||
{
|
||||
// TODO: Keep the bus and only insert pending items again.
|
||||
// TODO: Check if packet identifier must be restarted or not.
|
||||
_packetBus.Clear();
|
||||
|
||||
foreach (var publishPacket in _publishPacketsWithoutAcknowledge.Values.ToList())
|
||||
{
|
||||
EnqueuePacket(new MqttPacketBusItem(publishPacket));
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user