Code cleanup.

This commit is contained in:
Christian
2022-03-06 12:47:48 +01:00
parent 9a94b0465d
commit 7d95c238c3
18 changed files with 97 additions and 97 deletions
@@ -40,7 +40,7 @@ namespace MQTTnet.AspNetCore
public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure = null) public static IServiceCollection AddHostedMqttServer(this IServiceCollection services, Action<MqttServerOptionsBuilder> configure = null)
{ {
services.AddSingleton<MqttServerOptions>(s => services.AddSingleton(s =>
{ {
var serverOptionsBuilder = new MqttServerOptionsBuilder(); var serverOptionsBuilder = new MqttServerOptionsBuilder();
configure?.Invoke(serverOptionsBuilder); configure?.Invoke(serverOptionsBuilder);
@@ -54,7 +54,7 @@ namespace MQTTnet.AspNetCore
public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure) public static IServiceCollection AddHostedMqttServerWithServices(this IServiceCollection services, Action<AspNetMqttServerOptionsBuilder> configure)
{ {
services.AddSingleton<MqttServerOptions>(s => services.AddSingleton(s =>
{ {
var builder = new AspNetMqttServerOptionsBuilder(s); var builder = new AspNetMqttServerOptionsBuilder(s);
configure(builder); configure(builder);
@@ -11,6 +11,7 @@ using System.IO;
using System.Threading; using System.Threading;
using MQTTnet.Diagnostics; using MQTTnet.Diagnostics;
using MQTTnet.Formatter; using MQTTnet.Formatter;
using MQTTnet.Tests.Mockups;
namespace MQTTnet.Benchmarks namespace MQTTnet.Benchmarks
{ {
@@ -56,9 +56,9 @@ namespace MQTTnet.Extensions.WebSocket4Net
} }
#if NET48 || NETCOREAPP3_1 || NET5 || NET6 #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 #else
var sslProtocols = _webSocketOptions?.TlsOptions.SslProtocol ?? SslProtocols.Tls12 | (SslProtocols)0x00003000 /*Tls13*/; var sslProtocols = _webSocketOptions?.TlsOptions?.SslProtocol ?? SslProtocols.Tls12 | (SslProtocols)0x00003000 /*Tls13*/;
#endif #endif
var subProtocol = _webSocketOptions.SubProtocols.FirstOrDefault() ?? string.Empty; var subProtocol = _webSocketOptions.SubProtocols.FirstOrDefault() ?? string.Empty;
@@ -17,6 +17,7 @@ using MQTTnet.Internal;
using MQTTnet.Packets; using MQTTnet.Packets;
using MQTTnet.Protocol; using MQTTnet.Protocol;
using MQTTnet.Tests.Extensions; using MQTTnet.Tests.Extensions;
using MQTTnet.Tests.Mockups;
namespace MQTTnet.Tests namespace MQTTnet.Tests
{ {
@@ -8,7 +8,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MQTTnet.Channel; using MQTTnet.Channel;
namespace MQTTnet.Internal namespace MQTTnet.Tests.Mockups
{ {
public sealed class TestMqttChannel : IMqttChannel public sealed class TestMqttChannel : IMqttChannel
{ {
@@ -5,6 +5,7 @@ using MQTTnet.Diagnostics;
using MQTTnet.Formatter; using MQTTnet.Formatter;
using MQTTnet.Internal; using MQTTnet.Internal;
using MQTTnet.Packets; using MQTTnet.Packets;
using MQTTnet.Tests.Mockups;
namespace MQTTnet.Tests namespace MQTTnet.Tests
{ {
@@ -10,7 +10,7 @@ using System.Security.Cryptography.X509Certificates;
namespace MQTTnet.Client namespace MQTTnet.Client
{ {
public class MqttClientOptionsBuilderTlsParameters public sealed class MqttClientOptionsBuilderTlsParameters
{ {
public bool UseTls { get; set; } public bool UseTls { get; set; }
@@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using MQTTnet.Exceptions; using MQTTnet.Exceptions;
using MQTTnet.Packets; using MQTTnet.Packets;
@@ -7,12 +7,17 @@ using System;
namespace MQTTnet.Diagnostics namespace MQTTnet.Diagnostics
{ {
/// <summary> /// <summary>
/// This logger does nothing with the messages. /// This logger does nothing with the messages.
/// </summary> /// </summary>
public sealed class MqttNetNullLogger : IMqttNetLogger public sealed class MqttNetNullLogger : IMqttNetLogger
{ {
public MqttNetNullLogger()
{
IsEnabled = false;
}
public static MqttNetNullLogger Instance { get; } = new MqttNetNullLogger(); public static MqttNetNullLogger Instance { get; } = new MqttNetNullLogger();
public bool IsEnabled { get; } public bool IsEnabled { get; }
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
@@ -3,12 +3,10 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using MQTTnet.Exceptions; using MQTTnet.Exceptions;
using MQTTnet.Implementations; using MQTTnet.Implementations;
using MQTTnet.Protocol;
namespace MQTTnet.Formatter namespace MQTTnet.Formatter
{ {
@@ -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. // 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); // 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". // 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); set => _socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, value ? 1 : 0);
} }
-1
View File
@@ -4,7 +4,6 @@
using MQTTnet.Exceptions; using MQTTnet.Exceptions;
using MQTTnet.Protocol; using MQTTnet.Protocol;
using System;
using MQTTnet.Packets; using MQTTnet.Packets;
namespace MQTTnet namespace MQTTnet
@@ -4,41 +4,40 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Threading; using System.Threading;
namespace MQTTnet.Server namespace MQTTnet.Server
{ {
public sealed class InterceptingPublishEventArgs : EventArgs public sealed class InterceptingPublishEventArgs : EventArgs
{ {
/// <summary>
/// Gets the client identifier.
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
/// </summary>
public string ClientId { get; internal set; }
public MqttApplicationMessage ApplicationMessage { get; set; } public MqttApplicationMessage ApplicationMessage { get; set; }
/// <summary> /// <summary>
/// Gets or sets a key/value collection that can be used to share data within the scope of this session. /// Gets the cancellation token which can indicate that the client connection gets down.
/// </summary>
public IDictionary SessionItems { get; internal set; }
/// <summary>
/// Gets the response which will be sent to the client via the PUBACK etc. packets.
/// </summary>
public PublishResponse Response { get; } = new PublishResponse();
/// <summary>
/// Gets or sets whether the publish should be processed internally.
/// </summary>
public bool ProcessPublish { get; set; } = true;
public bool CloseConnection { get; set; }
/// <summary>
/// Gets the cancellation token which can indicate that the client connection gets down.
/// </summary> /// </summary>
public CancellationToken CancellationToken { get; internal set; } public CancellationToken CancellationToken { get; internal set; }
/// <summary>
/// Gets the client identifier.
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
/// </summary>
public string ClientId { get; internal set; }
public bool CloseConnection { get; set; }
/// <summary>
/// Gets or sets whether the publish should be processed internally.
/// </summary>
public bool ProcessPublish { get; set; } = true;
/// <summary>
/// Gets the response which will be sent to the client via the PUBACK etc. packets.
/// </summary>
public PublishResponse Response { get; } = new PublishResponse();
/// <summary>
/// Gets or sets a key/value collection that can be used to share data within the scope of this session.
/// </summary>
public IDictionary SessionItems { get; internal set; }
} }
} }
@@ -4,7 +4,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Threading; using System.Threading;
namespace MQTTnet.Server namespace MQTTnet.Server
@@ -12,43 +11,45 @@ namespace MQTTnet.Server
public sealed class InterceptingUnsubscriptionEventArgs : EventArgs public sealed class InterceptingUnsubscriptionEventArgs : EventArgs
{ {
/// <summary> /// <summary>
/// Gets the client identifier. /// Gets the cancellation token which can indicate that the client connection gets down.
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. /// </summary>
public CancellationToken CancellationToken { get; internal set; }
/// <summary>
/// Gets the client identifier.
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
/// </summary> /// </summary>
public string ClientId { get; internal set; } public string ClientId { get; internal set; }
/// <summary> /// <summary>
/// Gets or sets the MQTT topic. /// Gets or sets whether the broker should close the client connection.
/// 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).
/// </summary> /// </summary>
public string Topic { get; internal set; } public bool CloseConnection { get; set; }
/// <summary> /// <summary>
/// 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.
/// </summary>
public bool ProcessUnsubscription { get; set; } = true;
/// <summary>
/// Gets the response which will be sent to the client via the UNSUBACK pocket.
/// </summary>
public UnsubscribeResponse Response { get; } = new UnsubscribeResponse();
/// <summary>
/// Gets or sets a key/value collection that can be used to share data within the scope of this session.
/// </summary> /// </summary>
public IDictionary SessionItems { get; internal set; } public IDictionary SessionItems { get; internal set; }
/// <summary> /// <summary>
/// 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).
/// </summary> /// </summary>
public UnsubscribeResponse Response { get; } = new UnsubscribeResponse(); public string Topic { get; internal set; }
/// <summary>
/// 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.
/// </summary>
public bool ProcessUnsubscription { get; set; } = true;
/// <summary>
/// Gets or sets whether the broker should close the client connection.
/// </summary>
public bool CloseConnection { get; set; }
/// <summary>
/// Gets the cancellation token which can indicate that the client connection gets down.
/// </summary>
public CancellationToken CancellationToken { get; internal set; }
} }
} }
@@ -11,9 +11,9 @@ namespace MQTTnet.Server
public sealed class PreparingSessionEventArgs : EventArgs public sealed class PreparingSessionEventArgs : EventArgs
{ {
public string Id { get; internal set; } public string Id { get; internal set; }
// TODO: Allow adding of packets to the queue etc. // TODO: Allow adding of packets to the queue etc.
/* /*
* The Session State in the Server consists of: * The Session State in the Server consists of:
· The existence of a Session, even if the rest of the Session State is empty. · 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 · 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. · 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 bool IsExistingSession { get; set; }
public IDictionary<object, object> Items { get; set; }
public List<MqttPublishPacket> PublishPackets { get; } = new List<MqttPublishPacket>();
DateTime? SessionExpiryTimestamp { get; set; }
public List<MqttSubscription> Subscriptions { get; } = new List<MqttSubscription>(); public List<MqttSubscription> Subscriptions { get; } = new List<MqttSubscription>();
public List<MqttPublishPacket> PublishPackets { get; } = new List<MqttPublishPacket>();
public IDictionary<object, object> Items { get; set; }
/// <summary> /// <summary>
/// Gets the last will message. /// Gets the will delay interval.
/// In MQTT, you use the last will message feature to notify other clients about an ungracefully disconnected client. /// This is the time between the client disconnect and the time the will message will be sent.
/// </summary> /// </summary>
public uint? WillDelayInterval { get; set; }
// <summary>
// Gets the last will message.
// In MQTT, you use the last will message feature to notify other clients about an ungracefully disconnected client.
// </summary>
// TODO: Use single properties. No entire will message. // TODO: Use single properties. No entire will message.
MqttApplicationMessage WillMessage { get; set; } //MqttApplicationMessage WillMessage { get; set; }
/// <summary>
/// Gets the will delay interval.
/// This is the time between the client disconnect and the time the will message will be sent.
/// </summary>
uint? WillDelayInterval { get; set; }
DateTime? SessionExpiryTimestamp { get; set; }
} }
} }
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
namespace MQTTnet.Server namespace MQTTnet.Server
{ {
public interface ISubscriptionChangedNotification public interface ISubscriptionChangedNotification
{ {
void OnSubscriptionsAdded(MqttSession clientSession, List<string> subscriptionsTopics); void OnSubscriptionsAdded(MqttSession clientSession, List<string> subscriptionsTopics);
void OnSubscriptionsRemoved(MqttSession clientSession, List<string> subscriptionTopics); void OnSubscriptionsRemoved(MqttSession clientSession, List<string> subscriptionTopics);
} }
} }
+5 -5
View File
@@ -103,24 +103,24 @@ namespace MQTTnet.Server
Session.LatestConnectPacket = _connectPacket; Session.LatestConnectPacket = _connectPacket;
Session.WillMessageSent = false; Session.WillMessageSent = false;
using (var cancellationToken = new CancellationTokenSource()) using (_cancellationToken = new CancellationTokenSource())
{ {
_cancellationToken = cancellationToken; var cancellationToken = _cancellationToken.Token;
try try
{ {
Task.Run(() => SendPacketsLoop(cancellationToken.Token), cancellationToken.Token).RunInBackground(_logger); Task.Run(() => SendPacketsLoop(cancellationToken), cancellationToken).RunInBackground(_logger);
IsRunning = true; IsRunning = true;
await ReceivePackagesLoop(cancellationToken.Token).ConfigureAwait(false); await ReceivePackagesLoop(cancellationToken).ConfigureAwait(false);
} }
finally finally
{ {
IsRunning = false; IsRunning = false;
_cancellationToken?.Cancel();
_cancellationToken = null; _cancellationToken = null;
cancellationToken.Cancel();
} }
} }
@@ -2,9 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using MQTTnet.Protocol;
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MQTTnet.Packets; using MQTTnet.Packets;