From a7d0ad5f4360872b860299b404224f2e4caeb389 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Mon, 9 Oct 2017 19:42:31 +0200 Subject: [PATCH] Fix several bugs --- .../MQTTnet.NetFramework/MqttClientFactory.cs | 7 +- .../MQTTnet.NetStandard/MqttClientFactory.cs | 7 +- .../Implementations/MqttTcpChannel.cs | 2 +- .../MqttClientFactory.cs | 7 +- MQTTnet.Core/Client/IMqttClientFactory.cs | 2 +- Tests/MQTTnet.TestApp.NetCore/Program.cs | 2 +- .../PerformanceTest.cs | 2 +- Tests/MQTTnet.TestApp.NetFramework/Program.cs | 33 ++++-- .../MainPage.xaml.cs | 102 +++++++++++++++++- 9 files changed, 134 insertions(+), 30 deletions(-) diff --git a/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs b/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs index a64deea..15da033 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs @@ -1,15 +1,12 @@ -using System; -using MQTTnet.Core.Client; +using MQTTnet.Core.Client; using MQTTnet.Implementations; namespace MQTTnet { public class MqttClientFactory : IMqttClientFactory { - public IMqttClient CreateMqttClient(MqttClientOptions options) + public IMqttClient CreateMqttClient() { - if (options == null) throw new ArgumentNullException(nameof(options)); - return new MqttClient(new MqttCommunicationAdapterFactory()); } } diff --git a/Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs b/Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs index a64deea..15da033 100644 --- a/Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.NetStandard/MqttClientFactory.cs @@ -1,15 +1,12 @@ -using System; -using MQTTnet.Core.Client; +using MQTTnet.Core.Client; using MQTTnet.Implementations; namespace MQTTnet { public class MqttClientFactory : IMqttClientFactory { - public IMqttClient CreateMqttClient(MqttClientOptions options) + public IMqttClient CreateMqttClient() { - if (options == null) throw new ArgumentNullException(nameof(options)); - return new MqttClient(new MqttCommunicationAdapterFactory()); } } diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs index 591b739..03206f1 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs @@ -33,7 +33,7 @@ namespace MQTTnet.Implementations public Stream ReceiveStream { get; private set; } public Stream RawReceiveStream { get; private set; } - public Func> CustomIgnorableServerCertificateErrorsResolver { get; set; } + public static Func> CustomIgnorableServerCertificateErrorsResolver { get; set; } public async Task ConnectAsync() { diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs index a64deea..15da033 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs @@ -1,15 +1,12 @@ -using System; -using MQTTnet.Core.Client; +using MQTTnet.Core.Client; using MQTTnet.Implementations; namespace MQTTnet { public class MqttClientFactory : IMqttClientFactory { - public IMqttClient CreateMqttClient(MqttClientOptions options) + public IMqttClient CreateMqttClient() { - if (options == null) throw new ArgumentNullException(nameof(options)); - return new MqttClient(new MqttCommunicationAdapterFactory()); } } diff --git a/MQTTnet.Core/Client/IMqttClientFactory.cs b/MQTTnet.Core/Client/IMqttClientFactory.cs index cecc5ab..033eb99 100644 --- a/MQTTnet.Core/Client/IMqttClientFactory.cs +++ b/MQTTnet.Core/Client/IMqttClientFactory.cs @@ -2,6 +2,6 @@ { public interface IMqttClientFactory { - IMqttClient CreateMqttClient(MqttClientOptions options); + IMqttClient CreateMqttClient(); } } \ No newline at end of file diff --git a/Tests/MQTTnet.TestApp.NetCore/Program.cs b/Tests/MQTTnet.TestApp.NetCore/Program.cs index 0622f7e..e89c42a 100644 --- a/Tests/MQTTnet.TestApp.NetCore/Program.cs +++ b/Tests/MQTTnet.TestApp.NetCore/Program.cs @@ -53,7 +53,7 @@ namespace MQTTnet.TestApp.NetCore CleanSession = true }; - var client = new MqttClientFactory().CreateMqttClient(options); + var client = new MqttClientFactory().CreateMqttClient(); client.ApplicationMessageReceived += (s, e) => { Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###"); diff --git a/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs b/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs index e8890d5..9f44419 100644 --- a/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/PerformanceTest.cs @@ -40,7 +40,7 @@ namespace MQTTnet.TestApp.NetFramework DefaultCommunicationTimeout = TimeSpan.FromMinutes(10) }; - var client = new MqttClientFactory().CreateMqttClient(options); + var client = new MqttClientFactory().CreateMqttClient(); client.ApplicationMessageReceived += (s, e) => { }; diff --git a/Tests/MQTTnet.TestApp.NetFramework/Program.cs b/Tests/MQTTnet.TestApp.NetFramework/Program.cs index 50867f0..606aafa 100644 --- a/Tests/MQTTnet.TestApp.NetFramework/Program.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/Program.cs @@ -9,6 +9,7 @@ using MQTTnet.Core.Diagnostics; using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using MQTTnet.Core.Server; +using MQTTnet.Implementations; namespace MQTTnet.TestApp.NetFramework { @@ -54,7 +55,7 @@ namespace MQTTnet.TestApp.NetFramework { var options = new MqttClientWebSocketOptions { - Uri = "broker.hivemq.com:8000/mqtt" + Uri = "broker.hivemq.com:8000/mqtt" }; ////var options = new MqttClientOptions @@ -64,8 +65,8 @@ namespace MQTTnet.TestApp.NetFramework //// CleanSession = true ////}; - var client = new MqttClientFactory().CreateMqttClient(options); - client.ApplicationMessageReceived += (s, e) => + var mqttClient = new MqttClientFactory().CreateMqttClient(); + mqttClient.ApplicationMessageReceived += (s, e) => { Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###"); Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}"); @@ -75,24 +76,24 @@ namespace MQTTnet.TestApp.NetFramework Console.WriteLine(); }; - client.Connected += async (s, e) => + mqttClient.Connected += async (s, e) => { Console.WriteLine("### CONNECTED WITH SERVER ###"); - await client.SubscribeAsync(new List + await mqttClient.SubscribeAsync(new List { new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce) }); }; - client.Disconnected += async (s, e) => + mqttClient.Disconnected += async (s, e) => { Console.WriteLine("### DISCONNECTED FROM SERVER ###"); await Task.Delay(TimeSpan.FromSeconds(5)); try { - await client.ConnectAsync(options); + await mqttClient.ConnectAsync(options); } catch { @@ -102,7 +103,7 @@ namespace MQTTnet.TestApp.NetFramework try { - await client.ConnectAsync(options); + await mqttClient.ConnectAsync(options); } catch (Exception exception) { @@ -117,7 +118,7 @@ namespace MQTTnet.TestApp.NetFramework Console.ReadLine(); var applicationMessage = messageFactory.CreateApplicationMessage("myTopic", "Hello World", MqttQualityOfServiceLevel.AtLeastOnce); - await client.PublishAsync(applicationMessage); + await mqttClient.PublishAsync(applicationMessage); } } catch (Exception exception) @@ -170,5 +171,19 @@ namespace MQTTnet.TestApp.NetFramework Console.ReadLine(); } + + private static async Task WikiCode() + { + // For .NET Framwork & netstandard apps: + MqttTcpChannel.CustomCertificateValidationCallback = (x509Certificate, x509Chain, sslPolicyErrors, mqttClientTcpOptions) => + { + if (mqttClientTcpOptions.Server == "server_with_revoked_cert") + { + return true; + } + + return false; + }; + } } } \ No newline at end of file diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 1014e02..07d5955 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -1,5 +1,7 @@ using System; using System.Text; +using System.Threading.Tasks; +using Windows.Security.Cryptography.Certificates; using Windows.UI.Core; using Windows.UI.Xaml; using MQTTnet.Core; @@ -7,6 +9,8 @@ using MQTTnet.Core.Client; using MQTTnet.Core.Diagnostics; using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; +using MQTTnet.Core.Server; +using MQTTnet.Implementations; namespace MQTTnet.TestApp.UniversalWindows { @@ -66,7 +70,7 @@ namespace MQTTnet.TestApp.UniversalWindows options.TlsOptions.IgnoreCertificateChainErrors = true; options.TlsOptions.IgnoreCertificateRevocationErrors = true; options.TlsOptions.AllowUntrustedCertificates = true; - + try { if (_mqttClient != null) @@ -75,7 +79,7 @@ namespace MQTTnet.TestApp.UniversalWindows } var factory = new MqttClientFactory(); - _mqttClient = factory.CreateMqttClient(options); + _mqttClient = factory.CreateMqttClient(); await _mqttClient.ConnectAsync(options); } catch (Exception exception) @@ -190,5 +194,99 @@ namespace MQTTnet.TestApp.UniversalWindows Trace.Text += exception + Environment.NewLine; } } + + private async Task WikiCode() + { + var mqttClient = new MqttClientFactory().CreateMqttClient(); + + // ---------------------------------- + + var tcpOptions = new MqttClientTcpOptions + { + Server = "broker.hivemq.org", + ClientId = "TestClient" + }; + + await mqttClient.ConnectAsync(tcpOptions); + + // ---------------------------------- + + var secureTcpOptions = new MqttClientTcpOptions + { + Server = "broker.hivemq.org", + ClientId = "TestClient", + TlsOptions = new MqttClientTlsOptions + { + UseTls = true, + IgnoreCertificateChainErrors = true, + IgnoreCertificateRevocationErrors = true, + AllowUntrustedCertificates = true + } + }; + + // ---------------------------------- + + var wsOptions = new MqttClientWebSocketOptions + { + Uri = "broker.hivemq.com:8000/mqtt", + ClientId = "TestClient" + }; + + await mqttClient.ConnectAsync(wsOptions); + + // ---------------------------------- + { + var options = new MqttServerOptions(); + + var mqttServer = new MqttServerFactory().CreateMqttServer(options); + await mqttServer.StartAsync(); + + Console.WriteLine("Press any key to exit."); + Console.ReadLine(); + + await mqttServer.StopAsync(); + } + + // ---------------------------------- + + { + 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 => + { + if (o.Server == "server_with_revoked_cert") + { + return new[] { ChainValidationResult.Revoked }; + } + + return new ChainValidationResult[0]; + }; + + } } }