diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttCommunicationAdapterFactory.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttCommunicationAdapterFactory.cs deleted file mode 100644 index 0654e27b..00000000 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttCommunicationAdapterFactory.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Client; -using MQTTnet.Core.Diagnostics; -using MQTTnet.Core.Serializer; - -namespace MQTTnet.Implementations -{ - public class MqttCommunicationAdapterFactory : IMqttCommunicationAdapterFactory - { - public IMqttCommunicationAdapter CreateMqttCommunicationAdapter(IMqttClientOptions options) - { - if (options == null) throw new ArgumentNullException(nameof(options)); - - if (options is MqttClientTcpOptions tcpOptions) - { - var trace = new MqttNetTrace(); - return new MqttChannelCommunicationAdapter(new MqttTcpChannel(tcpOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion }, trace); - } - - if (options is MqttClientWebSocketOptions webSocketOptions) - { - var trace = new MqttNetTrace(); - return new MqttChannelCommunicationAdapter(new MqttWebSocketChannel(webSocketOptions), new MqttPacketSerializer { ProtocolVersion = options.ProtocolVersion }, trace); - } - - throw new NotSupportedException(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs index f5963186..373cdac4 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs @@ -1,21 +1,22 @@ using System; using System.Threading.Tasks; using MQTTnet.Core.Adapter; -using MQTTnet.Core.Diagnostics; -using MQTTnet.Core.Serializer; using MQTTnet.Core.Server; using Windows.Networking.Sockets; +using Microsoft.Extensions.Logging; namespace MQTTnet.Implementations { public class MqttServerAdapter : IMqttServerAdapter, IDisposable { - private readonly MqttNetTrace _trace; + private readonly ILogger _logger; + private readonly IMqttCommunicationAdapterFactory _mqttCommunicationAdapterFactory; private StreamSocketListener _defaultEndpointSocket; - public MqttServerAdapter(MqttNetTrace trace) + public MqttServerAdapter(ILogger logger, IMqttCommunicationAdapterFactory mqttCommunicationAdapterFactory) { - _trace = trace ?? throw new ArgumentNullException(nameof(trace)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _mqttCommunicationAdapterFactory = mqttCommunicationAdapterFactory ?? throw new ArgumentNullException(nameof(mqttCommunicationAdapterFactory)); } public event EventHandler ClientAccepted; @@ -56,12 +57,12 @@ namespace MQTTnet.Implementations { try { - var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new MqttPacketSerializer(), _trace); + var clientAdapter = _mqttCommunicationAdapterFactory.CreateServerMqttCommunicationAdapter(new MqttTcpChannel(args.Socket)); ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter)); } catch (Exception exception) { - _trace.Error(nameof(MqttServerAdapter), exception, "Error while accepting connection at default endpoint."); + _logger.LogError(new EventId(), exception, "Error while accepting connection at default endpoint."); } } } diff --git a/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj b/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj index 71081e0a..d8051f81 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj +++ b/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj @@ -110,14 +110,13 @@ bin\x64\Release\MQTTnet.XML - - + - + @@ -127,6 +126,12 @@ + + 1.1.1 + + + 1.1.2 + 5.4.0 diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs deleted file mode 100644 index 9a254f20..00000000 --- a/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs +++ /dev/null @@ -1,20 +0,0 @@ -using MQTTnet.Core.Client; -using MQTTnet.Core.Diagnostics; -using MQTTnet.Core.ManagedClient; -using MQTTnet.Implementations; - -namespace MQTTnet -{ - public class MqttClientFactory : IMqttClientFactory - { - public IMqttClient CreateMqttClient(IMqttNetTraceHandler traceHandler = null) - { - return new MqttClient(new MqttCommunicationAdapterFactory(), new MqttNetTrace(traceHandler)); - } - - public ManagedMqttClient CreateManagedMqttClient(IMqttNetTraceHandler traceHandler = null) - { - return new ManagedMqttClient(new MqttCommunicationAdapterFactory(), new MqttNetTrace(traceHandler)); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs new file mode 100644 index 00000000..9761150e --- /dev/null +++ b/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs @@ -0,0 +1,108 @@ +using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using MQTTnet.Core.Adapter; +using MQTTnet.Core.Channel; +using MQTTnet.Core.Client; +using MQTTnet.Core.ManagedClient; +using MQTTnet.Core.Serializer; +using MQTTnet.Core.Server; +using MQTTnet.Implementations; + +namespace MQTTnet +{ + public class MqttFactory : IMqttCommunicationAdapterFactory, IMqttClientSesssionFactory, IMqttClientFactory, IMqttServerFactory + { + private readonly IServiceProvider _serviceProvider; + + private static IServiceProvider BuildServiceProvider() + { + var serviceProvider = new ServiceCollection() + .AddMqttClient() + .AddMqttServer() + .AddLogging() + .BuildServiceProvider(); + + serviceProvider.GetRequiredService() + .AddMqttTrace(); + + return serviceProvider; + } + + public MqttFactory() + : this(BuildServiceProvider()) + { + } + + public MqttFactory(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public IMqttCommunicationAdapter CreateClientMqttCommunicationAdapter(IMqttClientOptions options) + { + var logger = _serviceProvider.GetRequiredService>(); + return new MqttChannelCommunicationAdapter(CreateMqttCommunicationChannel(options), CreateSerializer(options.ProtocolVersion), logger); + } + + public IMqttCommunicationAdapter CreateServerMqttCommunicationAdapter(IMqttCommunicationChannel channel) + { + var serializer = _serviceProvider.GetRequiredService(); + var logger = _serviceProvider.GetRequiredService>(); + return new MqttChannelCommunicationAdapter(channel, serializer, logger); + } + + public IMqttCommunicationChannel CreateMqttCommunicationChannel(IMqttClientOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + + switch (options) + { + case MqttClientTcpOptions tcpOptions: + return CreateTcpChannel(tcpOptions); + case MqttClientWebSocketOptions webSocketOptions: + return CreateWebSocketChannel(webSocketOptions); + default: + throw new NotSupportedException(); + } + } + + public MqttTcpChannel CreateTcpChannel(MqttClientTcpOptions tcpOptions) + { + return new MqttTcpChannel(tcpOptions); + } + + public MqttWebSocketChannel CreateWebSocketChannel(MqttClientWebSocketOptions webSocketOptions) + { + return new MqttWebSocketChannel(webSocketOptions); + } + + public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion) + { + return new MqttPacketSerializer() + { + ProtocolVersion = protocolVersion + }; + } + + public MqttClientSession CreateClientSession(string sessionId, MqttClientSessionsManager mqttClientSessionsManager) + { + return new MqttClientSession(sessionId, mqttClientSessionsManager, _serviceProvider.GetRequiredService>(), _serviceProvider.GetRequiredService>()); + } + + public IMqttClient CreateMqttClient() + { + return _serviceProvider.GetRequiredService(); + } + + public ManagedMqttClient CreateManagedMqttClient() + { + return _serviceProvider.GetRequiredService(); + } + + public IMqttServer CreateMqttServer() + { + return _serviceProvider.GetRequiredService(); + } + } +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs deleted file mode 100644 index 468b8e08..00000000 --- a/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Diagnostics; -using MQTTnet.Core.Server; -using MQTTnet.Implementations; - -namespace MQTTnet -{ - public class MqttServerFactory : IMqttServerFactory - { - public IMqttServer CreateMqttServer(MqttServerOptions options, IMqttNetTraceHandler traceHandler = null) - { - if (options == null) throw new ArgumentNullException(nameof(options)); - - var trace = new MqttNetTrace(traceHandler); - return new MqttServer(options, new List { new MqttServerAdapter(trace) }, trace); - } - } -} diff --git a/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs b/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..f8f647a9 --- /dev/null +++ b/Frameworks/MQTTnet.UniversalWindows/ServiceCollectionExtensions.cs @@ -0,0 +1,62 @@ +using Microsoft.Extensions.DependencyInjection; +using MQTTnet.Core.Adapter; +using MQTTnet.Core.Client; +using MQTTnet.Core.ManagedClient; +using MQTTnet.Core.Serializer; +using MQTTnet.Core.Server; +using MQTTnet.Implementations; +using System; +using Microsoft.Extensions.Logging; +using MQTTnet.Core.Diagnostics; + +namespace MQTTnet +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddMqttServer(this IServiceCollection services) + { + services.AddOptions(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + + services.AddSingleton(); + services.AddSingleton(); + + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + return services; + } + + public static IServiceCollection AddMqttServer(this IServiceCollection services, Action configureOptions) + { + return services + .AddMqttServer() + .Configure(configureOptions); + } + + public static IServiceCollection AddMqttClient(this IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + + return services; + } + + public static ILoggerFactory AddMqttTrace(this ILoggerFactory factory) + { + factory.AddProvider(new MqttNetTrace()); + return factory; + } + } +} diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 981d4a01..993e227e 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Windows.Security.Cryptography.Certificates; using Windows.UI.Core; using Windows.UI.Xaml; +using Microsoft.Extensions.DependencyInjection; using MQTTnet.Core; using MQTTnet.Core.Client; using MQTTnet.Core.Diagnostics; @@ -78,7 +79,7 @@ namespace MQTTnet.TestApp.UniversalWindows await _mqttClient.DisconnectAsync(); } - var factory = new MqttClientFactory(); + var factory = new MqttFactory(); _mqttClient = factory.CreateMqttClient(); await _mqttClient.ConnectAsync(options); } @@ -200,7 +201,35 @@ namespace MQTTnet.TestApp.UniversalWindows // ReSharper disable once UnusedMember.Local private async Task WikiCode() { - var mqttClient = new MqttClientFactory().CreateMqttClient(); + var services = new ServiceCollection() + .AddMqttClient() + .AddMqttServer(options => + { + options.ConnectionValidator = c => + { + if (c.ClientId.Length < 10) + { + return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; + } + + if (c.Username != "mySecretUser") + { + return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; + } + + if (c.Password != "mySecretPassword") + { + return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; + } + + return MqttConnectReturnCode.ConnectionAccepted; + }; + }) + .BuildServiceProvider(); + + var factory = new MqttFactory(services); + + var mqttClient = factory.CreateMqttClient(); // ---------------------------------- @@ -239,9 +268,7 @@ namespace MQTTnet.TestApp.UniversalWindows // ---------------------------------- { - var options = new MqttServerOptions(); - - var mqttServer = new MqttServerFactory().CreateMqttServer(options); + var mqttServer = factory.CreateMqttServer(); await mqttServer.StartAsync(); Console.WriteLine("Press any key to exit."); @@ -251,34 +278,7 @@ namespace MQTTnet.TestApp.UniversalWindows } // ---------------------------------- - - { - var options = new MqttServerOptions - { - ConnectionValidator = c => - { - if (c.ClientId.Length < 10) - { - return MqttConnectReturnCode.ConnectionRefusedIdentifierRejected; - } - - if (c.Username != "mySecretUser") - { - return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; - } - - if (c.Password != "mySecretPassword") - { - return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; - } - - return MqttConnectReturnCode.ConnectionAccepted; - } - }; - } - - // ---------------------------------- - + // For UWP apps: MqttTcpChannel.CustomIgnorableServerCertificateErrorsResolver = o => {