mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 12:16:02 +08:00
Add interceptor for unsubscriptions.
This commit is contained in:
@@ -15,6 +15,7 @@ namespace MQTTnet.Server
|
||||
|
||||
IMqttServerConnectionValidator ConnectionValidator { get; }
|
||||
IMqttServerSubscriptionInterceptor SubscriptionInterceptor { get; }
|
||||
IMqttServerUnsubscriptionInterceptor UnsubscriptionInterceptor { get; }
|
||||
IMqttServerApplicationMessageInterceptor ApplicationMessageInterceptor { get; }
|
||||
IMqttServerClientMessageQueueInterceptor ClientMessageQueueInterceptor { get; }
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
public interface IMqttServerUnsubscriptionInterceptor
|
||||
{
|
||||
Task InterceptUnsubscriptionAsync(MqttUnsubscriptionInterceptorContext context);
|
||||
}
|
||||
}
|
||||
@@ -107,9 +107,16 @@ namespace MQTTnet.Server
|
||||
PacketIdentifier = unsubscribePacket.PacketIdentifier
|
||||
};
|
||||
|
||||
lock (_subscriptions)
|
||||
foreach (var topicFilter in unsubscribePacket.TopicFilters)
|
||||
{
|
||||
foreach (var topicFilter in unsubscribePacket.TopicFilters)
|
||||
var interceptorContext = await InterceptUnsubscribeAsync(topicFilter).ConfigureAwait(false);
|
||||
if (!interceptorContext.AcceptUnsubscription)
|
||||
{
|
||||
unsubAckPacket.ReasonCodes.Add(MqttUnsubscribeReasonCode.ImplementationSpecificError);
|
||||
continue;
|
||||
}
|
||||
|
||||
lock (_subscriptions)
|
||||
{
|
||||
if (_subscriptions.Remove(topicFilter))
|
||||
{
|
||||
@@ -130,19 +137,23 @@ namespace MQTTnet.Server
|
||||
return unsubAckPacket;
|
||||
}
|
||||
|
||||
public Task UnsubscribeAsync(IEnumerable<string> topicFilters)
|
||||
public async Task UnsubscribeAsync(IEnumerable<string> topicFilters)
|
||||
{
|
||||
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
||||
|
||||
lock (_subscriptions)
|
||||
foreach (var topicFilter in topicFilters)
|
||||
{
|
||||
foreach (var topicFilter in topicFilters)
|
||||
var interceptorContext = await InterceptUnsubscribeAsync(topicFilter).ConfigureAwait(false);
|
||||
if (!interceptorContext.AcceptUnsubscription)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
lock (_subscriptions)
|
||||
{
|
||||
_subscriptions.Remove(topicFilter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public CheckSubscriptionsResult CheckSubscriptions(string topic, MqttQualityOfServiceLevel qosLevel)
|
||||
@@ -206,6 +217,17 @@ namespace MQTTnet.Server
|
||||
return context;
|
||||
}
|
||||
|
||||
private async Task<MqttUnsubscriptionInterceptorContext> InterceptUnsubscribeAsync(string topicFilter)
|
||||
{
|
||||
var context = new MqttUnsubscriptionInterceptorContext(_clientSession.ClientId, topicFilter, _clientSession.Items);
|
||||
if (_serverOptions.UnsubscriptionInterceptor != null)
|
||||
{
|
||||
await _serverOptions.UnsubscriptionInterceptor.InterceptUnsubscriptionAsync(context).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private static CheckSubscriptionsResult CreateSubscriptionResult(MqttQualityOfServiceLevel qosLevel, HashSet<MqttQualityOfServiceLevel> subscribedQoSLevels)
|
||||
{
|
||||
MqttQualityOfServiceLevel effectiveQoS;
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace MQTTnet.Server
|
||||
|
||||
public IMqttServerSubscriptionInterceptor SubscriptionInterceptor { get; set; }
|
||||
|
||||
public IMqttServerUnsubscriptionInterceptor UnsubscriptionInterceptor { get; set; }
|
||||
|
||||
public IMqttServerStorage Storage { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,6 +155,12 @@ namespace MQTTnet.Server
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttServerOptionsBuilder WithUnsubscriptionInterceptor(IMqttServerUnsubscriptionInterceptor value)
|
||||
{
|
||||
_options.UnsubscriptionInterceptor = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MqttServerOptionsBuilder WithSubscriptionInterceptor(Action<MqttSubscriptionInterceptorContext> value)
|
||||
{
|
||||
_options.SubscriptionInterceptor = new MqttServerSubscriptionInterceptorDelegate(value);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
public class MqttUnsubscriptionInterceptorContext
|
||||
{
|
||||
public MqttUnsubscriptionInterceptorContext(string clientId, string topic, IDictionary<object, object> sessionItems)
|
||||
{
|
||||
ClientId = clientId;
|
||||
Topic = topic;
|
||||
SessionItems = sessionItems;
|
||||
}
|
||||
|
||||
public string ClientId { get; }
|
||||
|
||||
public string Topic { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a key/value collection that can be used to share data within the scope of this session.
|
||||
/// </summary>
|
||||
public IDictionary<object, object> SessionItems { get; }
|
||||
|
||||
public bool AcceptUnsubscription { get; set; } = true;
|
||||
|
||||
public bool CloseConnection { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user