diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index 5f965a3..8f496ad 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -11,6 +11,7 @@ false MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). * [Core] Performance optimizations. +* [Core] Due to performance reasons the timestamp of log messages is now in UTC format. * [Client] Received messages are now processed in the worker thread by default. Added a new setting for switching back to dedicated threads. * [Server] Added support for other WebSocket sub protocol formats like mqttv-3.1.1 (thanks to @israellot). * [Server] The takeover of an existing client sessions is now treated as a _clean_ disconnect of the previous client. diff --git a/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs b/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs index 87b2038..e2497f1 100644 --- a/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs +++ b/Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs @@ -25,6 +25,11 @@ namespace MQTTnet.AspNetCore .FirstOrDefault(p => p.ToLower().StartsWith("mqtt")); } + if (subprotocol == null) + { + return; + } + var adapter = app.ApplicationServices.GetRequiredService(); using (var webSocket = await context.WebSockets.AcceptWebSocketAsync(subprotocol)) { diff --git a/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs b/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs index 666c71e..d5401dc 100644 --- a/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs +++ b/Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs @@ -31,12 +31,12 @@ namespace MQTTnet.AspNetCore var eventArgs = new MqttServerAdapterClientAcceptedEventArgs(clientAdapter); ClientAccepted?.Invoke(this, eventArgs); - return eventArgs.SessionTask; + return eventArgs.SessionTask ?? Task.CompletedTask; } public void Dispose() { - StopAsync(); + StopAsync().GetAwaiter().GetResult(); } } } \ No newline at end of file diff --git a/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs b/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs index 61354cd..5b8f161 100644 --- a/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs +++ b/Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs @@ -14,16 +14,24 @@ namespace MQTTnet.AspNetCore { if (options == null) throw new ArgumentNullException(nameof(options)); + var logger = new MqttNetLogger(); + var childLogger = logger.CreateChildLogger(); + services.AddSingleton(options); - services.AddSingleton(new MqttNetLogger()); + services.AddSingleton(logger); + services.AddSingleton(childLogger); services.AddSingleton(); services.AddSingleton(s => s.GetService()); services.AddSingleton(s => s.GetService()); - + services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(s => s.GetService()); - services.AddSingleton(s => s.GetService()); + services.AddSingleton(s => s.GetService()); + + if (options.DefaultEndpointOptions.IsEnabled) + { + services.AddSingleton(s => s.GetService()); + } return services; } diff --git a/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs b/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs index b342ddc..0a781bc 100644 --- a/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs +++ b/Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs @@ -504,20 +504,16 @@ namespace MQTTnet.Client private void StartReceivingPackets(CancellationToken cancellationToken) { - _packetReceiverTask = Task.Factory.StartNew( + _packetReceiverTask = Task.Run( () => ReceivePacketsAsync(cancellationToken), - cancellationToken, - TaskCreationOptions.LongRunning, - TaskScheduler.Current); + cancellationToken); } private void StartSendingKeepAliveMessages(CancellationToken cancellationToken) { - _keepAliveMessageSenderTask = Task.Factory.StartNew( + _keepAliveMessageSenderTask = Task.Run( () => SendKeepAliveMessagesAsync(cancellationToken), - cancellationToken, - TaskCreationOptions.LongRunning, - TaskScheduler.Current); + cancellationToken); } private void StartProcessReceivedPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken) diff --git a/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetChildLogger.cs b/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetChildLogger.cs index 55d99e5..df99f44 100644 --- a/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetChildLogger.cs +++ b/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetChildLogger.cs @@ -4,7 +4,7 @@ namespace MQTTnet.Diagnostics { public interface IMqttNetChildLogger { - IMqttNetChildLogger CreateChildLogger(string source); + IMqttNetChildLogger CreateChildLogger(string source = null); void Verbose(string message, params object[] parameters); diff --git a/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetLogger.cs b/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetLogger.cs index d2aacf4..73b6fcb 100644 --- a/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetLogger.cs +++ b/Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetLogger.cs @@ -6,7 +6,7 @@ namespace MQTTnet.Diagnostics { event EventHandler LogMessagePublished; - IMqttNetChildLogger CreateChildLogger(string source); + IMqttNetChildLogger CreateChildLogger(string source = null); void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception); } diff --git a/Frameworks/MQTTnet.NetStandard/Diagnostics/MqttNetLogger.cs b/Frameworks/MQTTnet.NetStandard/Diagnostics/MqttNetLogger.cs index 868e5d7..6afeba1 100644 --- a/Frameworks/MQTTnet.NetStandard/Diagnostics/MqttNetLogger.cs +++ b/Frameworks/MQTTnet.NetStandard/Diagnostics/MqttNetLogger.cs @@ -13,7 +13,7 @@ namespace MQTTnet.Diagnostics public event EventHandler LogMessagePublished; - public IMqttNetChildLogger CreateChildLogger(string source) + public IMqttNetChildLogger CreateChildLogger(string source = null) { return new MqttNetChildLogger(this, source); } diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs index 85c15ce..40f6b2e 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs @@ -45,11 +45,8 @@ namespace MQTTnet.Implementations _defaultEndpointSocket.Bind(new IPEndPoint(options.DefaultEndpointOptions.BoundIPAddress, options.GetDefaultEndpointPort())); _defaultEndpointSocket.Listen(options.ConnectionBacklog); - Task.Factory.StartNew( - () => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), - _cancellationTokenSource.Token, - TaskCreationOptions.LongRunning, - TaskScheduler.Current); + Task.Run(() => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), + _cancellationTokenSource.Token); } if (options.TlsEndpointOptions.IsEnabled) @@ -69,11 +66,9 @@ namespace MQTTnet.Implementations _tlsEndpointSocket.Bind(new IPEndPoint(options.TlsEndpointOptions.BoundIPAddress, options.GetTlsEndpointPort())); _tlsEndpointSocket.Listen(options.ConnectionBacklog); - Task.Factory.StartNew( - () => AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token), - _cancellationTokenSource.Token, - TaskCreationOptions.LongRunning, - TaskScheduler.Current); + Task.Run( + () => AcceptTlsEndpointConnectionsAsync(_cancellationTokenSource.Token), + _cancellationTokenSource.Token); } return Task.FromResult(0); diff --git a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs index d9aa944..0ba3d2f 100644 --- a/Frameworks/MQTTnet.NetStandard/MqttFactory.cs +++ b/Frameworks/MQTTnet.NetStandard/MqttFactory.cs @@ -25,14 +25,14 @@ namespace MQTTnet public IManagedMqttClient CreateManagedMqttClient() { - return new ManagedMqttClient(CreateMqttClient(), new MqttNetLogger().CreateChildLogger(string.Empty)); + return new ManagedMqttClient(CreateMqttClient(), new MqttNetLogger().CreateChildLogger()); } public IManagedMqttClient CreateManagedMqttClient(IMqttNetLogger logger) { if (logger == null) throw new ArgumentNullException(nameof(logger)); - return new ManagedMqttClient(CreateMqttClient(), logger.CreateChildLogger(string.Empty)); + return new ManagedMqttClient(CreateMqttClient(), logger.CreateChildLogger()); } public IMqttServer CreateMqttServer() @@ -45,7 +45,7 @@ namespace MQTTnet { if (logger == null) throw new ArgumentNullException(nameof(logger)); - return CreateMqttServer(new List { new MqttTcpServerAdapter(logger.CreateChildLogger(string.Empty)) }, logger); + return CreateMqttServer(new List { new MqttTcpServerAdapter(logger.CreateChildLogger()) }, logger); } public IMqttServer CreateMqttServer(IEnumerable adapters, IMqttNetLogger logger) @@ -53,7 +53,7 @@ namespace MQTTnet if (adapters == null) throw new ArgumentNullException(nameof(adapters)); if (logger == null) throw new ArgumentNullException(nameof(logger)); - return new MqttServer(adapters, logger.CreateChildLogger(string.Empty)); + return new MqttServer(adapters, logger.CreateChildLogger()); } } } \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs b/Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs index a10d19c..4323e2e 100644 --- a/Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs +++ b/Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs @@ -157,11 +157,9 @@ namespace MQTTnet.Server private void OnClientAccepted(object sender, MqttServerAdapterClientAcceptedEventArgs eventArgs) { - eventArgs.SessionTask = Task.Factory.StartNew( + eventArgs.SessionTask = Task.Run( () => _clientSessionsManager.RunSessionAsync(eventArgs.Client, _cancellationTokenSource.Token), - _cancellationTokenSource.Token, - TaskCreationOptions.LongRunning, - TaskScheduler.Current); + _cancellationTokenSource.Token); } } } diff --git a/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj b/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj index 29b7253..cd38d16 100644 --- a/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj +++ b/Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj @@ -10,7 +10,7 @@ - + diff --git a/Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs b/Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs index 878a788..27d8077 100644 --- a/Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs @@ -17,8 +17,8 @@ namespace MQTTnet.TestApp.NetCore Console.WriteLine("Press 'c' for concurrent sends. Otherwise in one batch."); var concurrent = Console.ReadKey(true).KeyChar == 'c'; - var server = Task.Factory.StartNew(async () => await RunServerAsync(), TaskCreationOptions.LongRunning); - var client = Task.Factory.StartNew(async () => await RunClientAsync(2000, TimeSpan.FromMilliseconds(10), concurrent), TaskCreationOptions.LongRunning); + var server = Task.Run(RunServerAsync); + var client = Task.Run(() => RunClientAsync(2000, TimeSpan.FromMilliseconds(10), concurrent)); await Task.WhenAll(server, client).ConfigureAwait(false); }