Parcourir la source

Refactor logging. Remove TCP adapter from HTTP host if disabled.

release/3.x.x
Christian il y a 6 ans
Parent
révision
dc7b9414d9
13 fichiers modifiés avec 41 ajouts et 38 suppressions
  1. +1
    -0
      Build/MQTTnet.nuspec
  2. +5
    -0
      Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs
  3. +2
    -2
      Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs
  4. +12
    -4
      Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs
  5. +4
    -8
      Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs
  6. +1
    -1
      Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetChildLogger.cs
  7. +1
    -1
      Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetLogger.cs
  8. +1
    -1
      Frameworks/MQTTnet.NetStandard/Diagnostics/MqttNetLogger.cs
  9. +5
    -10
      Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs
  10. +4
    -4
      Frameworks/MQTTnet.NetStandard/MqttFactory.cs
  11. +2
    -4
      Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs
  12. +1
    -1
      Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj
  13. +2
    -2
      Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs

+ 1
- 0
Build/MQTTnet.nuspec Voir le fichier

@@ -11,6 +11,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker).</description>
<releaseNotes> * [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.


+ 5
- 0
Frameworks/MQTTnet.AspnetCore/ApplicationBuilderExtensions.cs Voir le fichier

@@ -25,6 +25,11 @@ namespace MQTTnet.AspNetCore
.FirstOrDefault(p => p.ToLower().StartsWith("mqtt"));
}

if (subprotocol == null)
{
return;
}

var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync(subprotocol))
{


+ 2
- 2
Frameworks/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs Voir le fichier

@@ -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();
}
}
}

+ 12
- 4
Frameworks/MQTTnet.AspnetCore/ServiceCollectionExtensions.cs Voir le fichier

@@ -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<IMqttNetLogger>(new MqttNetLogger());
services.AddSingleton<IMqttNetLogger>(logger);
services.AddSingleton(childLogger);
services.AddSingleton<MqttHostedServer>();
services.AddSingleton<IHostedService>(s => s.GetService<MqttHostedServer>());
services.AddSingleton<IMqttServer>(s => s.GetService<MqttHostedServer>());
services.AddSingleton<MqttWebSocketServerAdapter>();
services.AddSingleton<MqttTcpServerAdapter>();
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>());
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttWebSocketServerAdapter>());

if (options.DefaultEndpointOptions.IsEnabled)
{
services.AddSingleton<IMqttServerAdapter>(s => s.GetService<MqttTcpServerAdapter>());
}

return services;
}


+ 4
- 8
Frameworks/MQTTnet.NetStandard/Client/MqttClient.cs Voir le fichier

@@ -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)


+ 1
- 1
Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetChildLogger.cs Voir le fichier

@@ -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);



+ 1
- 1
Frameworks/MQTTnet.NetStandard/Diagnostics/IMqttNetLogger.cs Voir le fichier

@@ -6,7 +6,7 @@ namespace MQTTnet.Diagnostics
{
event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;

IMqttNetChildLogger CreateChildLogger(string source);
IMqttNetChildLogger CreateChildLogger(string source = null);

void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception);
}


+ 1
- 1
Frameworks/MQTTnet.NetStandard/Diagnostics/MqttNetLogger.cs Voir le fichier

@@ -13,7 +13,7 @@ namespace MQTTnet.Diagnostics

public event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;

public IMqttNetChildLogger CreateChildLogger(string source)
public IMqttNetChildLogger CreateChildLogger(string source = null)
{
return new MqttNetChildLogger(this, source);
}


+ 5
- 10
Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.cs Voir le fichier

@@ -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);


+ 4
- 4
Frameworks/MQTTnet.NetStandard/MqttFactory.cs Voir le fichier

@@ -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<IMqttServerAdapter> { new MqttTcpServerAdapter(logger.CreateChildLogger(string.Empty)) }, logger);
return CreateMqttServer(new List<IMqttServerAdapter> { new MqttTcpServerAdapter(logger.CreateChildLogger()) }, logger);
}

public IMqttServer CreateMqttServer(IEnumerable<IMqttServerAdapter> 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());
}
}
}

+ 2
- 4
Frameworks/MQTTnet.NetStandard/Server/MqttServer.cs Voir le fichier

@@ -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);
}
}
}

+ 1
- 1
Tests/MQTTnet.TestApp.AspNetCore2/MQTTnet.TestApp.AspNetCore2.csproj Voir le fichier

@@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
</ItemGroup>

<ItemGroup>


+ 2
- 2
Tests/MQTTnet.TestApp.NetCore/PerformanceTest.cs Voir le fichier

@@ -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);
}


Chargement…
Annuler
Enregistrer