Restore undelivered event.

This commit is contained in:
Christian
2021-10-27 22:10:42 +02:00
parent d411aa4f97
commit 369b262e07
5 changed files with 47 additions and 32 deletions
@@ -407,14 +407,7 @@ namespace MQTTnet.Server.Internal
}
_logger.Verbose("Client '{0}': Queued application message with topic '{1}'.", clientSession.Id, applicationMessage.Topic);
// var queuedApplicationMessage = new MqttQueuedApplicationMessage
// {
// ApplicationMessage = applicationMessage,
// SubscriptionQualityOfServiceLevel = checkSubscriptionsResult.QualityOfServiceLevel,
// SubscriptionIdentifiers = checkSubscriptionsResult.SubscriptionIdentifiers
// };
var newPublishPacket = _packetFactories.Publish.Create(applicationMessage);
newPublishPacket.QualityOfServiceLevel = checkSubscriptionsResult.QualityOfServiceLevel;
newPublishPacket.Properties.SubscriptionIdentifiers.AddRange(checkSubscriptionsResult.SubscriptionIdentifiers);
@@ -441,15 +434,11 @@ namespace MQTTnet.Server.Internal
if (deliveryCount == 0)
{
// TODO: Addd event for not delivered publish packets.
// var undeliveredMessageInterceptor = _options.UndeliveredMessageInterceptor;
// if (undeliveredMessageInterceptor == null)
// {
// return;
// }
//
// // The delegate signature is the same as for regular message interceptor. So the call is fine and just uses a different interceptor.
// await InterceptApplicationMessageAsync(undeliveredMessageInterceptor, sender, applicationMessage).ConfigureAwait(false);
await _eventContainer.ApplicationMessageNotConsumedEvent.InvokeAsync(() => new MqttApplicationMessageNotConsumedEventArgs
{
ApplicationMessage = applicationMessage,
SenderClientId = senderClientId
}).ConfigureAwait(false);
}
}
catch (Exception exception)
@@ -28,6 +28,9 @@ namespace MQTTnet.Server.Internal
public AsyncEvent<MqttServerClientUnsubscribedTopicEventArgs> ClientUnsubscribedTopicEvent { get; } = new AsyncEvent<MqttServerClientUnsubscribedTopicEventArgs>();
public AsyncEvent<PreparingMqttClientSessionEventArgs> PreparingClientSessionEvent { get; } = new AsyncEvent<PreparingMqttClientSessionEventArgs>();
public AsyncEvent<MqttApplicationMessageNotConsumedEventArgs> ApplicationMessageNotConsumedEvent { get; } = new AsyncEvent<MqttApplicationMessageNotConsumedEventArgs>();
public AsyncEvent<EventArgs> StartedEvent { get; } = new AsyncEvent<EventArgs>();
public AsyncEvent<EventArgs> StoppedEvent { get; } = new AsyncEvent<EventArgs>();
@@ -0,0 +1,17 @@
using System;
namespace MQTTnet.Server
{
public sealed class MqttApplicationMessageNotConsumedEventArgs : EventArgs
{
/// <summary>
/// Gets the application message which was not consumed by any client.
/// </summary>
public MqttApplicationMessage ApplicationMessage { get; internal set; }
/// <summary>
/// Gets the ID of the client which has sent the affected application message.
/// </summary>
public string SenderClientId { get; internal set; }
}
}
+6
View File
@@ -107,6 +107,12 @@ namespace MQTTnet.Server
remove => _eventContainer.PreparingClientSessionEvent.RemoveHandler(value);
}
public event Func<MqttApplicationMessageNotConsumedEventArgs, Task> ApplicationMessageNotConsumedAsync
{
add => _eventContainer.ApplicationMessageNotConsumedEvent.AddHandler(value);
remove => _eventContainer.ApplicationMessageNotConsumedEvent.RemoveHandler(value);
}
public bool IsStarted => _cancellationTokenSource != null;
public Task<IList<IMqttClientStatus>> GetClientStatusAsync()
+15 -15
View File
@@ -1027,22 +1027,22 @@ namespace MQTTnet.Tests.Server
{
var undeliverd = string.Empty;
throw new NotImplementedException();
var server = await testEnvironment.StartServer();
server.ApplicationMessageNotConsumedAsync += e =>
{
undeliverd = e.ApplicationMessage.Topic;
return Task.CompletedTask;
};
// var options = new MqttServerOptionsBuilder().WithUndeliveredMessageInterceptor(
// context => { undeliverd = context.ApplicationMessage.Topic; });
//
// await testEnvironment.StartServer(options);
//
// var client = await testEnvironment.ConnectClient();
//
// await client.SubscribeAsync("b");
//
// await client.PublishAsync("a", null, MqttQualityOfServiceLevel.ExactlyOnce);
//
// await Task.Delay(500);
//
// Assert.AreEqual("a", undeliverd);
var client = await testEnvironment.ConnectClient();
await client.SubscribeAsync("b");
await client.PublishAsync("a", null, MqttQualityOfServiceLevel.ExactlyOnce);
await Task.Delay(500);
Assert.AreEqual("a", undeliverd);
}
}