diff --git a/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedEventArgs.cs b/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedEventArgs.cs index 7515334c..0afd96a4 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedEventArgs.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedEventArgs.cs @@ -6,7 +6,7 @@ using System; namespace MQTTnet.Extensions.ManagedClient { - public class ApplicationMessageSkippedEventArgs : EventArgs + public sealed class ApplicationMessageSkippedEventArgs : EventArgs { public ApplicationMessageSkippedEventArgs(ManagedMqttApplicationMessage applicationMessage) { diff --git a/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedHandlerDelegate.cs b/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedHandlerDelegate.cs deleted file mode 100644 index c20e8efd..00000000 --- a/Source/MQTTnet.Extensions.ManagedClient/ApplicationMessageSkippedHandlerDelegate.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Threading.Tasks; - -namespace MQTTnet.Extensions.ManagedClient -{ - public class ApplicationMessageSkippedHandlerDelegate : IApplicationMessageSkippedHandler - { - private readonly Func _handler; - - public ApplicationMessageSkippedHandlerDelegate(Action handler) - { - if (handler == null) throw new ArgumentNullException(nameof(handler)); - - _handler = eventArgs => - { - handler(eventArgs); - return Task.FromResult(0); - }; - } - - public ApplicationMessageSkippedHandlerDelegate(Func handler) - { - _handler = handler ?? throw new ArgumentNullException(nameof(handler)); - } - - public Task HandleApplicationMessageSkippedAsync(ApplicationMessageSkippedEventArgs eventArgs) - { - return _handler(eventArgs); - } - } -} diff --git a/Source/MQTTnet.Extensions.ManagedClient/IApplicationMessageSkippedHandler.cs b/Source/MQTTnet.Extensions.ManagedClient/IApplicationMessageSkippedHandler.cs deleted file mode 100644 index fb51f61c..00000000 --- a/Source/MQTTnet.Extensions.ManagedClient/IApplicationMessageSkippedHandler.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Threading.Tasks; - -namespace MQTTnet.Extensions.ManagedClient -{ - public interface IApplicationMessageSkippedHandler - { - Task HandleApplicationMessageSkippedAsync(ApplicationMessageSkippedEventArgs eventArgs); - } -} diff --git a/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs index 80ce3e38..26fc6463 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/IManagedMqttClient.cs @@ -13,6 +13,8 @@ namespace MQTTnet.Extensions.ManagedClient event Func ApplicationMessageReceivedAsync; + event Func ApplicationMessageSkippedAsync; + event Func ConnectedAsync; event Func ConnectingFailedAsync; @@ -21,7 +23,7 @@ namespace MQTTnet.Extensions.ManagedClient event Func DisconnectedAsync; - IApplicationMessageSkippedHandler ApplicationMessageSkippedHandler { get; set; } + event Func SynchronizingSubscriptionsFailedAsync; IMqttClient InternalClient { get; } @@ -33,8 +35,6 @@ namespace MQTTnet.Extensions.ManagedClient int PendingApplicationMessagesCount { get; } - ISynchronizingSubscriptionsFailedHandler SynchronizingSubscriptionsFailedHandler { get; set; } - Task EnqueueAsync(MqttApplicationMessage applicationMessage); Task EnqueueAsync(ManagedMqttApplicationMessage applicationMessage); diff --git a/Source/MQTTnet.Extensions.ManagedClient/ISynchronizingSubscriptionsFailedHandler.cs b/Source/MQTTnet.Extensions.ManagedClient/ISynchronizingSubscriptionsFailedHandler.cs deleted file mode 100644 index 5287e6a2..00000000 --- a/Source/MQTTnet.Extensions.ManagedClient/ISynchronizingSubscriptionsFailedHandler.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Threading.Tasks; - -namespace MQTTnet.Extensions.ManagedClient -{ - public interface ISynchronizingSubscriptionsFailedHandler - { - Task HandleSynchronizingSubscriptionsFailedAsync(ManagedProcessFailedEventArgs eventArgs); - } -} diff --git a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs index 19c99a55..a36fc310 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs @@ -20,9 +20,11 @@ namespace MQTTnet.Extensions.ManagedClient public sealed class ManagedMqttClient : Disposable, IManagedMqttClient { readonly AsyncEvent _applicationMessageProcessedEvent = new AsyncEvent(); + readonly AsyncEvent _applicationMessageSkippedEvent = new AsyncEvent(); readonly AsyncEvent _connectingFailedEvent = new AsyncEvent(); readonly AsyncEvent _connectionStateChangedEvent = new AsyncEvent(); - + readonly AsyncEvent _synchronizingSubscriptionsFailedEvent = new AsyncEvent(); + readonly MqttNetSourceLogger _logger; readonly BlockingQueue _messageQueue = new BlockingQueue(); @@ -73,6 +75,12 @@ namespace MQTTnet.Extensions.ManagedClient _logger = logger.WithSource(nameof(ManagedMqttClient)); } + public event Func ApplicationMessageSkippedAsync + { + add => _applicationMessageSkippedEvent.AddHandler(value); + remove => _applicationMessageSkippedEvent.RemoveHandler(value); + } + public event Func ApplicationMessageProcessedAsync { add => _applicationMessageProcessedEvent.AddHandler(value); @@ -109,7 +117,11 @@ namespace MQTTnet.Extensions.ManagedClient remove => InternalClient.DisconnectedAsync -= value; } - public IApplicationMessageSkippedHandler ApplicationMessageSkippedHandler { get; set; } + public event Func SynchronizingSubscriptionsFailedAsync + { + add => _synchronizingSubscriptionsFailedEvent.AddHandler(value); + remove => _synchronizingSubscriptionsFailedEvent.RemoveHandler(value); + } public IMqttClient InternalClient { get; } @@ -120,9 +132,7 @@ namespace MQTTnet.Extensions.ManagedClient public ManagedMqttClientOptions Options { get; private set; } public int PendingApplicationMessagesCount => _messageQueue.Count; - - public ISynchronizingSubscriptionsFailedHandler SynchronizingSubscriptionsFailedHandler { get; set; } - + public async Task EnqueueAsync(MqttApplicationMessage applicationMessage) { ThrowIfDisposed(); @@ -191,13 +201,9 @@ namespace MQTTnet.Extensions.ManagedClient } finally { - if (applicationMessageSkippedEventArgs != null) + if (_applicationMessageSkippedEvent.HasHandlers) { - var applicationMessageSkippedHandler = ApplicationMessageSkippedHandler; - if (applicationMessageSkippedHandler != null) - { - await applicationMessageSkippedHandler.HandleApplicationMessageSkippedAsync(applicationMessageSkippedEventArgs).ConfigureAwait(false); - } + await _applicationMessageSkippedEvent.InvokeAsync(applicationMessageSkippedEventArgs).ConfigureAwait(false); } } } @@ -348,10 +354,9 @@ namespace MQTTnet.Extensions.ManagedClient { _logger.Warning(exception, "Synchronizing subscriptions failed."); - var synchronizingSubscriptionsFailedHandler = SynchronizingSubscriptionsFailedHandler; - if (SynchronizingSubscriptionsFailedHandler != null) + if (_synchronizingSubscriptionsFailedEvent.HasHandlers) { - await synchronizingSubscriptionsFailedHandler.HandleSynchronizingSubscriptionsFailedAsync(new ManagedProcessFailedEventArgs(exception)).ConfigureAwait(false); + await _synchronizingSubscriptionsFailedEvent.InvokeAsync(new ManagedProcessFailedEventArgs(exception)).ConfigureAwait(false); } } diff --git a/Source/MQTTnet.Extensions.ManagedClient/SynchronizingSubscriptionsFailedHandlerDelegate.cs b/Source/MQTTnet.Extensions.ManagedClient/SynchronizingSubscriptionsFailedHandlerDelegate.cs deleted file mode 100644 index 2a4b6b4c..00000000 --- a/Source/MQTTnet.Extensions.ManagedClient/SynchronizingSubscriptionsFailedHandlerDelegate.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Threading.Tasks; - -namespace MQTTnet.Extensions.ManagedClient -{ - public class SynchronizingSubscriptionsFailedHandlerDelegate : ISynchronizingSubscriptionsFailedHandler - { - private readonly Func _handler; - - public SynchronizingSubscriptionsFailedHandlerDelegate(Action handler) - { - if (handler == null) throw new ArgumentNullException(nameof(handler)); - - _handler = context => - { - handler(context); - return Task.FromResult(0); - }; - } - - public SynchronizingSubscriptionsFailedHandlerDelegate(Func handler) - { - _handler = handler ?? throw new ArgumentNullException(nameof(handler)); - } - - public Task HandleSynchronizingSubscriptionsFailedAsync(ManagedProcessFailedEventArgs eventArgs) - { - return _handler(eventArgs); - } - } -}