diff --git a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs index 8933bca0..90f25157 100644 --- a/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Source/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -40,7 +40,7 @@ namespace MQTTnet.AspNetCore public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action configure = null) { - services.AddSingleton(s => + services.AddSingleton(s => { var serverOptionsBuilder = new MqttServerOptionsBuilder(); configure?.Invoke(serverOptionsBuilder); @@ -54,7 +54,7 @@ namespace MQTTnet.AspNetCore public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action configure) { - services.AddSingleton(s => + services.AddSingleton(s => { var builder = new AspNetMqttServerOptionsBuilder(s); configure(builder); diff --git a/Source/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs b/Source/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs index 96c6419b..e51ab6a8 100644 --- a/Source/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs +++ b/Source/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs @@ -11,6 +11,7 @@ using System.IO; using System.Threading; using MQTTnet.Diagnostics; using MQTTnet.Formatter; +using MQTTnet.Tests.Mockups; namespace MQTTnet.Benchmarks { diff --git a/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttChannel.cs b/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttChannel.cs index a248be48..c628f542 100644 --- a/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttChannel.cs +++ b/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttChannel.cs @@ -56,9 +56,9 @@ namespace MQTTnet.Extensions.WebSocket4Net } #if NET48 || NETCOREAPP3_1 || NET5 || NET6 - var sslProtocols = _webSocketOptions?.TlsOptions.SslProtocol ?? SslProtocols.Tls12 | SslProtocols.Tls13; + var sslProtocols = _webSocketOptions?.TlsOptions?.SslProtocol ?? SslProtocols.Tls12 | SslProtocols.Tls13; #else - var sslProtocols = _webSocketOptions?.TlsOptions.SslProtocol ?? SslProtocols.Tls12 | (SslProtocols)0x00003000 /*Tls13*/; + var sslProtocols = _webSocketOptions?.TlsOptions?.SslProtocol ?? SslProtocols.Tls12 | (SslProtocols)0x00003000 /*Tls13*/; #endif var subProtocol = _webSocketOptions.SubProtocols.FirstOrDefault() ?? string.Empty; diff --git a/Source/MQTTnet.Tests/Formatter/MqttPacketSerialization_V3_Binary_Tests.cs b/Source/MQTTnet.Tests/Formatter/MqttPacketSerialization_V3_Binary_Tests.cs index c268dff0..aa299d7e 100644 --- a/Source/MQTTnet.Tests/Formatter/MqttPacketSerialization_V3_Binary_Tests.cs +++ b/Source/MQTTnet.Tests/Formatter/MqttPacketSerialization_V3_Binary_Tests.cs @@ -17,6 +17,7 @@ using MQTTnet.Internal; using MQTTnet.Packets; using MQTTnet.Protocol; using MQTTnet.Tests.Extensions; +using MQTTnet.Tests.Mockups; namespace MQTTnet.Tests { diff --git a/Source/MQTTnet/Internal/TestMqttChannel.cs b/Source/MQTTnet.Tests/Mockups/TestMqttChannel.cs similarity index 98% rename from Source/MQTTnet/Internal/TestMqttChannel.cs rename to Source/MQTTnet.Tests/Mockups/TestMqttChannel.cs index 45c8bd80..d6165510 100644 --- a/Source/MQTTnet/Internal/TestMqttChannel.cs +++ b/Source/MQTTnet.Tests/Mockups/TestMqttChannel.cs @@ -8,7 +8,7 @@ using System.Threading; using System.Threading.Tasks; using MQTTnet.Channel; -namespace MQTTnet.Internal +namespace MQTTnet.Tests.Mockups { public sealed class TestMqttChannel : IMqttChannel { diff --git a/Source/MQTTnet.Tests/MqttPacketSerializationHelper.cs b/Source/MQTTnet.Tests/MqttPacketSerializationHelper.cs index 4d4a6719..4d98e8c2 100644 --- a/Source/MQTTnet.Tests/MqttPacketSerializationHelper.cs +++ b/Source/MQTTnet.Tests/MqttPacketSerializationHelper.cs @@ -5,6 +5,7 @@ using MQTTnet.Diagnostics; using MQTTnet.Formatter; using MQTTnet.Internal; using MQTTnet.Packets; +using MQTTnet.Tests.Mockups; namespace MQTTnet.Tests { diff --git a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilderTlsParameters.cs b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilderTlsParameters.cs index 7ca1d7a5..c065bb72 100644 --- a/Source/MQTTnet/Client/Options/MqttClientOptionsBuilderTlsParameters.cs +++ b/Source/MQTTnet/Client/Options/MqttClientOptionsBuilderTlsParameters.cs @@ -10,7 +10,7 @@ using System.Security.Cryptography.X509Certificates; namespace MQTTnet.Client { - public class MqttClientOptionsBuilderTlsParameters + public sealed class MqttClientOptionsBuilderTlsParameters { public bool UseTls { get; set; } diff --git a/Source/MQTTnet/Client/Unsubscribing/MqttClientUnsubscribeResultFactory.cs b/Source/MQTTnet/Client/Unsubscribing/MqttClientUnsubscribeResultFactory.cs index bee34991..0f872958 100644 --- a/Source/MQTTnet/Client/Unsubscribing/MqttClientUnsubscribeResultFactory.cs +++ b/Source/MQTTnet/Client/Unsubscribing/MqttClientUnsubscribeResultFactory.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.Linq; using MQTTnet.Exceptions; using MQTTnet.Packets; diff --git a/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs index c83fc9aa..20208e35 100644 --- a/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs @@ -7,12 +7,17 @@ using System; namespace MQTTnet.Diagnostics { /// - /// This logger does nothing with the messages. + /// This logger does nothing with the messages. /// public sealed class MqttNetNullLogger : IMqttNetLogger { + public MqttNetNullLogger() + { + IsEnabled = false; + } + public static MqttNetNullLogger Instance { get; } = new MqttNetNullLogger(); - + public bool IsEnabled { get; } public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) diff --git a/Source/MQTTnet/Formatter/MqttBufferReader.cs b/Source/MQTTnet/Formatter/MqttBufferReader.cs index 75f367dd..4c5cc36e 100644 --- a/Source/MQTTnet/Formatter/MqttBufferReader.cs +++ b/Source/MQTTnet/Formatter/MqttBufferReader.cs @@ -3,12 +3,10 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; using MQTTnet.Exceptions; using MQTTnet.Implementations; -using MQTTnet.Protocol; namespace MQTTnet.Formatter { diff --git a/Source/MQTTnet/Implementations/CrossPlatformSocket.cs b/Source/MQTTnet/Implementations/CrossPlatformSocket.cs index 79aadcaa..1d14ffcd 100644 --- a/Source/MQTTnet/Implementations/CrossPlatformSocket.cs +++ b/Source/MQTTnet/Implementations/CrossPlatformSocket.cs @@ -47,7 +47,7 @@ namespace MQTTnet.Implementations // We cannot use the _NoDelay_ property from the socket because there is an issue in .NET 4.5.2, 4.6. // The decompiled code is: this.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Debug, value ? 1 : 0); // Which is wrong because the "NoDelay" should be set and not "Debug". - get => (int)_socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay) != 0; + get => (int?)_socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay) != 0; set => _socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, value ? 1 : 0); } diff --git a/Source/MQTTnet/MqttTopicFilterBuilder.cs b/Source/MQTTnet/MqttTopicFilterBuilder.cs index 81631785..2140745a 100644 --- a/Source/MQTTnet/MqttTopicFilterBuilder.cs +++ b/Source/MQTTnet/MqttTopicFilterBuilder.cs @@ -4,7 +4,6 @@ using MQTTnet.Exceptions; using MQTTnet.Protocol; -using System; using MQTTnet.Packets; namespace MQTTnet diff --git a/Source/MQTTnet/Server/Events/InterceptingPublishEventArgs.cs b/Source/MQTTnet/Server/Events/InterceptingPublishEventArgs.cs index d677d11e..09a4d76e 100644 --- a/Source/MQTTnet/Server/Events/InterceptingPublishEventArgs.cs +++ b/Source/MQTTnet/Server/Events/InterceptingPublishEventArgs.cs @@ -4,41 +4,40 @@ using System; using System.Collections; -using System.Collections.Generic; using System.Threading; namespace MQTTnet.Server { public sealed class InterceptingPublishEventArgs : EventArgs - { - /// - /// Gets the client identifier. - /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. - /// - public string ClientId { get; internal set; } - + { public MqttApplicationMessage ApplicationMessage { get; set; } /// - /// Gets or sets a key/value collection that can be used to share data within the scope of this session. - /// - public IDictionary SessionItems { get; internal set; } - - /// - /// Gets the response which will be sent to the client via the PUBACK etc. packets. - /// - public PublishResponse Response { get; } = new PublishResponse(); - - /// - /// Gets or sets whether the publish should be processed internally. - /// - public bool ProcessPublish { get; set; } = true; - - public bool CloseConnection { get; set; } - - /// - /// Gets the cancellation token which can indicate that the client connection gets down. + /// Gets the cancellation token which can indicate that the client connection gets down. /// public CancellationToken CancellationToken { get; internal set; } + + /// + /// Gets the client identifier. + /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. + /// + public string ClientId { get; internal set; } + + public bool CloseConnection { get; set; } + + /// + /// Gets or sets whether the publish should be processed internally. + /// + public bool ProcessPublish { get; set; } = true; + + /// + /// Gets the response which will be sent to the client via the PUBACK etc. packets. + /// + public PublishResponse Response { get; } = new PublishResponse(); + + /// + /// Gets or sets a key/value collection that can be used to share data within the scope of this session. + /// + public IDictionary SessionItems { get; internal set; } } -} +} \ No newline at end of file diff --git a/Source/MQTTnet/Server/Events/InterceptingUnsubscriptionEventArgs.cs b/Source/MQTTnet/Server/Events/InterceptingUnsubscriptionEventArgs.cs index 2c785630..648ccd6f 100644 --- a/Source/MQTTnet/Server/Events/InterceptingUnsubscriptionEventArgs.cs +++ b/Source/MQTTnet/Server/Events/InterceptingUnsubscriptionEventArgs.cs @@ -4,7 +4,6 @@ using System; using System.Collections; -using System.Collections.Generic; using System.Threading; namespace MQTTnet.Server @@ -12,43 +11,45 @@ namespace MQTTnet.Server public sealed class InterceptingUnsubscriptionEventArgs : EventArgs { /// - /// Gets the client identifier. - /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. + /// Gets the cancellation token which can indicate that the client connection gets down. + /// + public CancellationToken CancellationToken { get; internal set; } + + /// + /// Gets the client identifier. + /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. /// public string ClientId { get; internal set; } /// - /// Gets or sets the MQTT topic. - /// In MQTT, the word topic refers to an UTF-8 string that the broker uses to filter messages for each connected client. - /// The topic consists of one or more topic levels. Each topic level is separated by a forward slash (topic level separator). + /// Gets or sets whether the broker should close the client connection. /// - public string Topic { get; internal set; } + public bool CloseConnection { get; set; } /// - /// Gets or sets a key/value collection that can be used to share data within the scope of this session. + /// Gets or sets whether the broker should remove an internal subscription for the client. + /// The broker can also avoid this and return "success" to the client. + /// This feature allows using the MQTT Broker as the Frontend and another system as the backend. + /// + public bool ProcessUnsubscription { get; set; } = true; + + /// + /// Gets the response which will be sent to the client via the UNSUBACK pocket. + /// + public UnsubscribeResponse Response { get; } = new UnsubscribeResponse(); + + /// + /// Gets or sets a key/value collection that can be used to share data within the scope of this session. /// public IDictionary SessionItems { get; internal set; } /// - /// Gets the response which will be sent to the client via the UNSUBACK pocket. + /// Gets or sets the MQTT topic. + /// In MQTT, the word topic refers to an UTF-8 string that the broker uses to filter messages for each connected + /// client. + /// The topic consists of one or more topic levels. Each topic level is separated by a forward slash (topic level + /// separator). /// - public UnsubscribeResponse Response { get; } = new UnsubscribeResponse(); - - /// - /// Gets or sets whether the broker should remove an internal subscription for the client. - /// The broker can also avoid this and return "success" to the client. - /// This feature allows using the MQTT Broker as the Frontend and another system as the backend. - /// - public bool ProcessUnsubscription { get; set; } = true; - - /// - /// Gets or sets whether the broker should close the client connection. - /// - public bool CloseConnection { get; set; } - - /// - /// Gets the cancellation token which can indicate that the client connection gets down. - /// - public CancellationToken CancellationToken { get; internal set; } + public string Topic { get; internal set; } } -} +} \ No newline at end of file diff --git a/Source/MQTTnet/Server/Events/PreparingSessionEventArgs.cs b/Source/MQTTnet/Server/Events/PreparingSessionEventArgs.cs index 68b82483..e635308a 100644 --- a/Source/MQTTnet/Server/Events/PreparingSessionEventArgs.cs +++ b/Source/MQTTnet/Server/Events/PreparingSessionEventArgs.cs @@ -11,9 +11,9 @@ namespace MQTTnet.Server public sealed class PreparingSessionEventArgs : EventArgs { public string Id { get; internal set; } - + // TODO: Allow adding of packets to the queue etc. - + /* * The Session State in the Server consists of: · The existence of a Session, even if the rest of the Session State is empty. @@ -23,28 +23,28 @@ namespace MQTTnet.Server · QoS 2 messages which have been received from the Client, but have not been completely acknowledged.The Will Message and the Will Delay Interval · If the Session is currently not connected, the time at which the Session will end and Session State will be discarded. */ - + public bool IsExistingSession { get; set; } + public IDictionary Items { get; set; } + + public List PublishPackets { get; } = new List(); + + DateTime? SessionExpiryTimestamp { get; set; } + public List Subscriptions { get; } = new List(); - public List PublishPackets { get; } = new List(); - - public IDictionary Items { get; set; } - /// - /// Gets the last will message. - /// In MQTT, you use the last will message feature to notify other clients about an ungracefully disconnected client. + /// Gets the will delay interval. + /// This is the time between the client disconnect and the time the will message will be sent. /// + public uint? WillDelayInterval { get; set; } + + // + // Gets the last will message. + // In MQTT, you use the last will message feature to notify other clients about an ungracefully disconnected client. + // // TODO: Use single properties. No entire will message. - MqttApplicationMessage WillMessage { get; set; } - - /// - /// Gets the will delay interval. - /// This is the time between the client disconnect and the time the will message will be sent. - /// - uint? WillDelayInterval { get; set; } - - DateTime? SessionExpiryTimestamp { get; set; } + //MqttApplicationMessage WillMessage { get; set; } } } \ No newline at end of file diff --git a/Source/MQTTnet/Server/Internal/ISubscriptionChangedNotification.cs b/Source/MQTTnet/Server/Internal/ISubscriptionChangedNotification.cs index 01b13f6f..bff6769a 100644 --- a/Source/MQTTnet/Server/Internal/ISubscriptionChangedNotification.cs +++ b/Source/MQTTnet/Server/Internal/ISubscriptionChangedNotification.cs @@ -1,13 +1,11 @@ -using System; using System.Collections.Generic; -using System.Text; namespace MQTTnet.Server { public interface ISubscriptionChangedNotification { void OnSubscriptionsAdded(MqttSession clientSession, List subscriptionsTopics); - + void OnSubscriptionsRemoved(MqttSession clientSession, List subscriptionTopics); } -} +} \ No newline at end of file diff --git a/Source/MQTTnet/Server/Internal/MqttClient.cs b/Source/MQTTnet/Server/Internal/MqttClient.cs index fd4c7f6e..718b4850 100644 --- a/Source/MQTTnet/Server/Internal/MqttClient.cs +++ b/Source/MQTTnet/Server/Internal/MqttClient.cs @@ -103,24 +103,24 @@ namespace MQTTnet.Server Session.LatestConnectPacket = _connectPacket; Session.WillMessageSent = false; - using (var cancellationToken = new CancellationTokenSource()) + using (_cancellationToken = new CancellationTokenSource()) { - _cancellationToken = cancellationToken; + var cancellationToken = _cancellationToken.Token; try { - Task.Run(() => SendPacketsLoop(cancellationToken.Token), cancellationToken.Token).RunInBackground(_logger); + Task.Run(() => SendPacketsLoop(cancellationToken), cancellationToken).RunInBackground(_logger); IsRunning = true; - await ReceivePackagesLoop(cancellationToken.Token).ConfigureAwait(false); + await ReceivePackagesLoop(cancellationToken).ConfigureAwait(false); } finally { IsRunning = false; + _cancellationToken?.Cancel(); _cancellationToken = null; - cancellationToken.Cancel(); } } diff --git a/Source/MQTTnet/Server/MqttServerExtensions.cs b/Source/MQTTnet/Server/MqttServerExtensions.cs index c027c593..22d8538c 100644 --- a/Source/MQTTnet/Server/MqttServerExtensions.cs +++ b/Source/MQTTnet/Server/MqttServerExtensions.cs @@ -2,9 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using MQTTnet.Protocol; using System; -using System.Threading; using System.Threading.Tasks; using MQTTnet.Packets;