From 929cdb971ac4627b66f10d2f149313e90bd4aa0e Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Sat, 3 Jun 2017 22:48:22 +0200 Subject: [PATCH] Fix breaking changes. --- .../Implementations/MqttServerAdapter.cs | 117 ++++++++++++++ .../Implementations/MqttTcpChannel.cs | 122 ++++++++++++++ .../MQTTnet.NetCoreApp/MqttClientFactory.cs | 6 +- .../MqttClientSslChannel.cs | 122 -------------- .../MQTTnet.NetCoreApp/MqttServerAdapter.cs | 56 ------- .../MQTTnet.NetCoreApp/MqttServerFactory.cs | 6 +- .../MqttServerSslChannel.cs | 123 --------------- .../MqttSslServerAdapter.cs | 64 -------- .../MQTTnet.NetCoreApp/MqttTcpChannel.cs | 81 ---------- .../Implementations/MqttServerAdapter.cs | 117 ++++++++++++++ .../{ => Implementations}/MqttTcpChannel.cs | 54 ++++++- .../MQTTnet.NetFramework.csproj | 7 +- .../MQTTnet.NetFramework/MqttClientFactory.cs | 10 +- .../MqttClientSslChannel.cs | 121 -------------- .../MQTTnet.NetFramework/MqttServerAdapter.cs | 56 ------- .../MQTTnet.NetFramework/MqttServerFactory.cs | 6 +- .../MqttServerSslChannel.cs | 123 --------------- .../MqttSslServerAdapter.cs | 64 -------- .../Implementations/MqttServerAdapter.cs | 90 +++++++++++ .../Implementations/MqttTcpChannel.cs | 121 ++++++++++++++ .../MQTTnet.UniversalWindows.csproj | 4 +- .../MqttClientFactory.cs | 3 +- .../MqttServerAdapter.cs | 54 ------- .../MqttServerFactory.cs | 5 +- .../MqttTcpChannel.cs | 62 -------- MQTTnet.Core/Client/MqttClient.cs | 12 +- MQTTnet.Core/Client/MqttClientOptions.cs | 25 +-- .../Client/MqttClientOptionsExtensions.cs | 19 +++ MQTTnet.Core/Client/MqttClientSslOptions.cs | 13 ++ .../MqttTraceMessagePublishedEventArgs.cs | 2 +- .../MqttCommunicationTimedOutException.cs | 2 +- .../MqttProtocolViolationException.cs | 2 +- MQTTnet.Core/Internal/AsyncAutoResetEvent.cs | 2 +- MQTTnet.Core/Internal/TaskExtensions.cs | 11 -- MQTTnet.Core/MqttApplicationMessage.cs | 2 +- ...MqttApplicationMessageReceivedEventArgs.cs | 2 +- MQTTnet.Core/Packets/MqttConnAckPacket.cs | 2 +- MQTTnet.Core/Packets/MqttConnectPacket.cs | 2 +- MQTTnet.Core/Packets/MqttDisconnectPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPingReqPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPingRespPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPubAckPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPubCompPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPubRecPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPubRelPacket.cs | 2 +- MQTTnet.Core/Packets/MqttPublishPacket.cs | 2 +- MQTTnet.Core/Packets/MqttSubAckPacket.cs | 2 +- MQTTnet.Core/Packets/MqttSubscribePacket.cs | 2 +- MQTTnet.Core/Packets/MqttUnsubAckPacket.cs | 2 +- MQTTnet.Core/Packets/MqttUnsubscribe.cs | 2 +- MQTTnet.Core/Packets/TopicFilter.cs | 2 +- MQTTnet.Core/Serializer/ByteReader.cs | 2 +- MQTTnet.Core/Serializer/ByteWriter.cs | 2 +- .../DefaultMqttV311PacketSerializer.cs | 2 +- MQTTnet.Core/Server/DefaultEndpointOptions.cs | 9 ++ .../Server/GetOrCreateClientSessionResult.cs | 2 +- MQTTnet.Core/Server/MqttClientMessageQueue.cs | 2 +- .../Server/MqttClientPublishPacketContext.cs | 2 +- .../Server/MqttClientSessionsManager.cs | 2 +- .../Server/MqttClientSubscriptionsManager.cs | 2 +- MQTTnet.Core/Server/MqttServer.cs | 24 +-- MQTTnet.Core/Server/MqttServerOptions.cs | 13 +- .../Server/MqttServerOptionsExtensions.cs | 31 ++++ MQTTnet.Core/Server/SslEndpointOptions.cs | 11 ++ .../Server/SslEndpointOptionsExtensions.cs | 19 +++ MQTTnet.nuspec | 4 +- MQTTnet.sln | 118 +++++++------- README.md | 14 +- Tests/MQTTnet.Core.Tests/MqttServerTests.cs | 10 +- .../App.config | 0 .../MQTTnet.TestApp.NetFramework.csproj} | 46 ++---- .../Program.cs | 75 ++++++++- .../Properties/AssemblyInfo.cs | 2 +- .../MQTTnet.TestApp.UniversalWindows/App.xaml | 8 + .../App.xaml.cs | 100 ++++++++++++ .../Assets/LockScreenLogo.scale-200.png | Bin 0 -> 1430 bytes .../Assets/SplashScreen.scale-200.png | Bin 0 -> 7700 bytes .../Assets/Square150x150Logo.scale-200.png | Bin 0 -> 2937 bytes .../Assets/Square44x44Logo.scale-200.png | Bin 0 -> 1647 bytes ...x44Logo.targetsize-24_altform-unplated.png | Bin 0 -> 1255 bytes .../Assets/StoreLogo.png | Bin 0 -> 1451 bytes .../Assets/Wide310x150Logo.scale-200.png | Bin 0 -> 3204 bytes .../MQTTnet.TestApp.UniversalWindows.csproj | 149 ++++++++++++++++++ .../MainPage.xaml | 13 ++ .../MainPage.xaml.cs | 30 ++++ .../Package.appxmanifest | 49 ++++++ .../Properties/AssemblyInfo.cs | 7 +- .../Properties/Default.rd.xml | 31 ++++ .../project.json | 16 ++ Tests/MQTTnet.TestMqttClient/App.config | 6 - .../MQTTnet.TestMqttClient.csproj | 100 ------------ Tests/MQTTnet.TestMqttServer/Program.cs | 55 ------- 92 files changed, 1363 insertions(+), 1309 deletions(-) create mode 100644 Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs create mode 100644 Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs delete mode 100644 Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs delete mode 100644 Frameworks/MQTTnet.NetCoreApp/MqttServerAdapter.cs delete mode 100644 Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs delete mode 100644 Frameworks/MQTTnet.NetCoreApp/MqttSslServerAdapter.cs delete mode 100644 Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs create mode 100644 Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs rename Frameworks/MQTTnet.NetFramework/{ => Implementations}/MqttTcpChannel.cs (52%) delete mode 100644 Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs delete mode 100644 Frameworks/MQTTnet.NetFramework/MqttServerAdapter.cs delete mode 100644 Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs delete mode 100644 Frameworks/MQTTnet.NetFramework/MqttSslServerAdapter.cs create mode 100644 Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs create mode 100644 Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs delete mode 100644 Frameworks/MQTTnet.UniversalWindows/MqttServerAdapter.cs delete mode 100644 Frameworks/MQTTnet.UniversalWindows/MqttTcpChannel.cs create mode 100644 MQTTnet.Core/Client/MqttClientOptionsExtensions.cs create mode 100644 MQTTnet.Core/Client/MqttClientSslOptions.cs delete mode 100644 MQTTnet.Core/Internal/TaskExtensions.cs create mode 100644 MQTTnet.Core/Server/DefaultEndpointOptions.cs create mode 100644 MQTTnet.Core/Server/MqttServerOptionsExtensions.cs create mode 100644 MQTTnet.Core/Server/SslEndpointOptions.cs create mode 100644 MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs rename Tests/{MQTTnet.TestMqttServer => MQTTnet.TestApp.NetFramework}/App.config (100%) rename Tests/{MQTTnet.TestMqttServer/MQTTnet.TestMqttServer.csproj => MQTTnet.TestApp.NetFramework/MQTTnet.TestApp.NetFramework.csproj} (56%) rename Tests/{MQTTnet.TestMqttClient => MQTTnet.TestApp.NetFramework}/Program.cs (58%) rename Tests/{MQTTnet.TestMqttClient => MQTTnet.TestApp.NetFramework}/Properties/AssemblyInfo.cs (83%) create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/App.xaml create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/App.xaml.cs create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/LockScreenLogo.scale-200.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/SplashScreen.scale-200.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square150x150Logo.scale-200.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.scale-200.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.targetsize-24_altform-unplated.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/StoreLogo.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Assets/Wide310x150Logo.scale-200.png create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest rename Tests/{MQTTnet.TestMqttServer => MQTTnet.TestApp.UniversalWindows}/Properties/AssemblyInfo.cs (69%) create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/Properties/Default.rd.xml create mode 100644 Tests/MQTTnet.TestApp.UniversalWindows/project.json delete mode 100644 Tests/MQTTnet.TestMqttClient/App.config delete mode 100644 Tests/MQTTnet.TestMqttClient/MQTTnet.TestMqttClient.csproj delete mode 100644 Tests/MQTTnet.TestMqttServer/Program.cs diff --git a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs new file mode 100644 index 00000000..335de784 --- /dev/null +++ b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs @@ -0,0 +1,117 @@ +using System; +using System.Net; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; +using MQTTnet.Core.Adapter; +using MQTTnet.Core.Diagnostics; +using MQTTnet.Core.Serializer; +using MQTTnet.Core.Server; + +namespace MQTTnet.Implementations +{ + public class MqttServerAdapter : IMqttServerAdapter, IDisposable + { + private CancellationTokenSource _cancellationTokenSource; + private Socket _defaultEndpointSocket; + private Socket _sslEndpointSocket; + private X509Certificate2 _sslCertificate; + + private bool _isRunning; + + public event EventHandler ClientConnected; + + public void Start(MqttServerOptions options) + { + if (_isRunning) throw new InvalidOperationException("Server is already started."); + _isRunning = true; + + _cancellationTokenSource = new CancellationTokenSource(); + + if (options.DefaultEndpointOptions.IsEnabled) + { + _defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); + _defaultEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetDefaultEndpointPort())); + _defaultEndpointSocket.Listen(options.ConnectionBacklog); + + Task.Run(() => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); + } + + if (options.SslEndpointOptions.IsEnabled) + { + if (options.SslEndpointOptions.Certificate == null) + { + throw new ArgumentException("SSL certificate is not set."); + } + + _sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate); + + _sslEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); + _sslEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetSslEndpointPort())); + _sslEndpointSocket.Listen(options.ConnectionBacklog); + + Task.Run(() => AcceptSslEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); + } + } + + public void Stop() + { + _isRunning = false; + + _cancellationTokenSource?.Dispose(); + _cancellationTokenSource = null; + + _defaultEndpointSocket?.Dispose(); + _defaultEndpointSocket = null; + + _sslEndpointSocket?.Dispose(); + _sslEndpointSocket = null; + } + + public void Dispose() + { + Stop(); + } + + private async Task AcceptDefaultEndpointConnectionsAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + var clientSocket = await _defaultEndpointSocket.AcceptAsync(); + var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, null), new DefaultMqttV311PacketSerializer()); + ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); + } + catch (Exception exception) + { + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at default endpoint."); + } + } + } + + private async Task AcceptSslEndpointConnectionsAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + var clientSocket = await _defaultEndpointSocket.AcceptAsync(); + + var sslStream = new SslStream(new NetworkStream(clientSocket)); + await sslStream.AuthenticateAsServerAsync(_sslCertificate, false, SslProtocols.Tls12, false); + + var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream), new DefaultMqttV311PacketSerializer()); + ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); + } + catch (Exception exception) + { + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint."); + } + } + } + } +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs new file mode 100644 index 00000000..fabe7b97 --- /dev/null +++ b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttTcpChannel.cs @@ -0,0 +1,122 @@ +using System; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading.Tasks; +using MQTTnet.Core.Channel; +using MQTTnet.Core.Client; +using MQTTnet.Core.Exceptions; + +namespace MQTTnet.Implementations +{ + public class MqttTcpChannel : IMqttCommunicationChannel, IDisposable + { + private readonly Socket _socket; + private SslStream _sslStream; + + public MqttTcpChannel() + { + _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); + } + + public MqttTcpChannel(Socket socket, SslStream sslStream) + { + _socket = socket ?? throw new ArgumentNullException(nameof(socket)); + _sslStream = sslStream; + } + + public async Task ConnectAsync(MqttClientOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + try + { + await _socket.ConnectAsync(options.Server, options.GetPort()); + + if (options.SslOptions.UseSsl) + { + _sslStream = new SslStream(new NetworkStream(_socket, true)); + await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.SslOptions.CheckCertificateRevocation); + } + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public Task DisconnectAsync() + { + try + { + _sslStream.Dispose(); + _socket.Dispose(); + return Task.FromResult(0); + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public Task WriteAsync(byte[] buffer) + { + if (buffer == null) throw new ArgumentNullException(nameof(buffer)); + + try + { + if (_sslStream != null) + { + return _sslStream.WriteAsync(buffer, 0, buffer.Length); + } + + return _socket.SendAsync(new ArraySegment(buffer), SocketFlags.None); + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public Task ReadAsync(byte[] buffer) + { + if (buffer == null) throw new ArgumentNullException(nameof(buffer)); + + try + { + if (_sslStream != null) + { + return _sslStream.ReadAsync(buffer, 0, buffer.Length); + } + + return _socket.ReceiveAsync(new ArraySegment(buffer), SocketFlags.None); + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public void Dispose() + { + _socket?.Dispose(); + _sslStream?.Dispose(); + } + + private static X509CertificateCollection LoadCertificates(MqttClientOptions options) + { + var certificates = new X509CertificateCollection(); + if (options.SslOptions.Certificates == null) + { + return certificates; + } + + foreach (var certificate in options.SslOptions.Certificates) + { + certificates.Add(new X509Certificate(certificate)); + } + + return certificates; + } + } +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttClientFactory.cs b/Frameworks/MQTTnet.NetCoreApp/MqttClientFactory.cs index 10ff3fab..ac6f611a 100644 --- a/Frameworks/MQTTnet.NetCoreApp/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.NetCoreApp/MqttClientFactory.cs @@ -1,8 +1,8 @@ using System; using MQTTnet.Core.Adapter; -using MQTTnet.Core.Channel; using MQTTnet.Core.Client; using MQTTnet.Core.Serializer; +using MQTTnet.Implementations; namespace MQTTnet { @@ -12,9 +12,7 @@ namespace MQTTnet { if (options == null) throw new ArgumentNullException(nameof(options)); - return new MqttClient(options, - new MqttChannelCommunicationAdapter(options.UseSSL ? new MqttClientSslChannel() : (IMqttCommunicationChannel) new MqttTcpChannel(), - new DefaultMqttV311PacketSerializer())); + return new MqttClient(options, new MqttChannelCommunicationAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer())); } } } diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs b/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs deleted file mode 100644 index 7f3feb6e..00000000 --- a/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs +++ /dev/null @@ -1,122 +0,0 @@ -using System; -using System.IO; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Threading.Tasks; -using MQTTnet.Core.Channel; -using MQTTnet.Core.Client; -using MQTTnet.Core.Exceptions; - -namespace MQTTnet -{ - /// - /// Describes an SSL channel to an MQTT server. - /// - public class MqttClientSslChannel : IMqttCommunicationChannel, IDisposable - { - private readonly Socket _socket; - private SslStream _sslStream; - - /// - /// Creates a new . - /// - public MqttClientSslChannel() - { - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - } - - /// - /// Creates a new with a predefined . - /// - /// - public MqttClientSslChannel(Socket socket) - { - _socket = socket ?? throw new ArgumentNullException(nameof(socket)); - } - - /// - /// Asynchronously connects to the host described in the . - /// - /// The describing the connection. - public async Task ConnectAsync(MqttClientOptions options) - { - try - { - await _socket.ConnectAsync(options.Server, options.Port); - - NetworkStream ns = new NetworkStream(_socket, true); - _sslStream = new SslStream(ns); - - await _sslStream.AuthenticateAsClientAsync(options.Server, null, SslProtocols.Tls12, false); - - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously disconnects the client from the server. - /// - public Task DisconnectAsync() - { - try - { - _socket.Dispose(); - return Task.FromResult(0); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously writes a sequence of bytes to the socket. - /// - /// The buffer to write data from. - public Task WriteAsync(byte[] buffer) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer)); - - try - { - return _sslStream.WriteAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Asynchronously reads a sequence of bytes from the socket. - /// - /// The buffer to write the data into. - public Task ReadAsync(byte[] buffer) - { - try - { - return _sslStream.ReadAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Releases all resources used by the . - /// - public void Dispose() - { - _sslStream?.Dispose(); - _socket?.Dispose(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetCoreApp/MqttServerAdapter.cs deleted file mode 100644 index 1d3db70d..00000000 --- a/Frameworks/MQTTnet.NetCoreApp/MqttServerAdapter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Threading; -using System.Threading.Tasks; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Serializer; -using MQTTnet.Core.Server; - -namespace MQTTnet -{ - public sealed class MqttServerAdapter : IMqttServerAdapter, IDisposable - { - private CancellationTokenSource _cancellationTokenSource; - private Socket _socket; - - public event EventHandler ClientConnected; - - public void Start(MqttServerOptions options) - { - if (_socket != null) throw new InvalidOperationException("Server is already started."); - - _cancellationTokenSource = new CancellationTokenSource(); - - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _socket.Bind(new IPEndPoint(IPAddress.Any, options.Port)); - _socket.Listen(options.ConnectionBacklog); - - Task.Run(async () => await AcceptConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); - } - - public void Stop() - { - _cancellationTokenSource?.Dispose(); - _cancellationTokenSource = null; - - _socket?.Dispose(); - _socket = null; - } - - public void Dispose() - { - Stop(); - } - - private async Task AcceptConnectionsAsync(CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - var clientSocket = await _socket.AcceptAsync(); - var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket), new DefaultMqttV311PacketSerializer()); - ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); - } - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttServerFactory.cs b/Frameworks/MQTTnet.NetCoreApp/MqttServerFactory.cs index bc5ef2f5..eb7441c3 100644 --- a/Frameworks/MQTTnet.NetCoreApp/MqttServerFactory.cs +++ b/Frameworks/MQTTnet.NetCoreApp/MqttServerFactory.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using MQTTnet.Core.Adapter; using MQTTnet.Core.Server; +using MQTTnet.Implementations; namespace MQTTnet { @@ -9,8 +11,8 @@ namespace MQTTnet public MqttServer CreateMqttServer(MqttServerOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); - - return new MqttServer(options, options.UseSSL ? (IMqttServerAdapter)new MqttSslServerAdapter() : new MqttServerAdapter()); + + return new MqttServer(options, new List { new MqttServerAdapter() }); } } } diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs b/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs deleted file mode 100644 index b55771c0..00000000 --- a/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System; -using System.IO; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using System.Threading.Tasks; -using MQTTnet.Core.Channel; -using MQTTnet.Core.Client; -using MQTTnet.Core.Exceptions; - -namespace MQTTnet -{ - /// - /// Describes an SSL channel to a client. - /// - public class MqttServerSslChannel : IMqttCommunicationChannel, IDisposable - { - private readonly Socket _socket; - private readonly SslStream _sslStream; - private readonly X509Certificate2 _cert; - - /// - /// Creates a new with a predefined . - /// - /// The client socket. - /// The X509 certificate used to authenticate as a server. - public MqttServerSslChannel(Socket socket, X509Certificate2 cert) - { - _socket = socket ?? throw new ArgumentNullException(nameof(socket)); - _cert = cert ?? throw new ArgumentNullException(nameof(cert)); - - if (!_socket.Connected) - return; - - NetworkStream ns = new NetworkStream(_socket, true); - _sslStream = new SslStream(ns); - } - - public Task Authenticate() - { - return _sslStream.AuthenticateAsServerAsync(_cert, false, SslProtocols.Tls12, false); - } - - /// - /// Asynchronously connects to the client described in the . - /// - /// The describing the connection. - public Task ConnectAsync(MqttClientOptions options) - { - try - { - return _socket.ConnectAsync(options.Server, options.Port); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously disconnects the client from the server. - /// - public Task DisconnectAsync() - { - try - { - _socket.Dispose(); - return Task.FromResult(0); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously writes a sequence of bytes to the socket. - /// - /// The buffer to write data from. - public Task WriteAsync(byte[] buffer) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer)); - - try - { - return _sslStream.WriteAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Asynchronously reads a sequence of bytes from the socket. - /// - /// The buffer to write the data into. - public Task ReadAsync(byte[] buffer) - { - try - { - return _sslStream.ReadAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Releases all resources used by the . - /// - public void Dispose() - { - _sslStream?.Dispose(); - _socket?.Dispose(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttSslServerAdapter.cs b/Frameworks/MQTTnet.NetCoreApp/MqttSslServerAdapter.cs deleted file mode 100644 index 58b0a3b1..00000000 --- a/Frameworks/MQTTnet.NetCoreApp/MqttSslServerAdapter.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using System.Threading.Tasks; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Serializer; -using MQTTnet.Core.Server; - -namespace MQTTnet -{ - public class MqttSslServerAdapter : IMqttServerAdapter, IDisposable - { - private CancellationTokenSource _cancellationTokenSource; - private Socket _socket; - private X509Certificate2 _x590Certificate2; - - public event EventHandler ClientConnected; - - public void Start(MqttServerOptions options) - { - if (_socket != null) throw new InvalidOperationException("Server is already started."); - - _cancellationTokenSource = new CancellationTokenSource(); - - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _socket.Bind(new IPEndPoint(IPAddress.Any, options.Port)); - _socket.Listen(options.ConnectionBacklog); - - Task.Run(async () => await AcceptConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); - - _x590Certificate2 = new X509Certificate2(options.CertificatePath); - } - - public void Stop() - { - _cancellationTokenSource?.Dispose(); - _cancellationTokenSource = null; - - _socket?.Dispose(); - _socket = null; - } - - public void Dispose() - { - Stop(); - } - - private async Task AcceptConnectionsAsync(CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - var clientSocket = await _socket.AcceptAsync(); - - MqttServerSslChannel mssc = new MqttServerSslChannel(clientSocket, _x590Certificate2); - await mssc.Authenticate(); - - var clientAdapter = new MqttChannelCommunicationAdapter(mssc, new DefaultMqttV311PacketSerializer()); - ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); - } - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs deleted file mode 100644 index e32c27f8..00000000 --- a/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Net.Sockets; -using System.Threading.Tasks; -using MQTTnet.Core.Channel; -using MQTTnet.Core.Client; -using MQTTnet.Core.Exceptions; - -namespace MQTTnet -{ - public class MqttTcpChannel : IMqttCommunicationChannel, IDisposable - { - private readonly Socket _socket; - - public MqttTcpChannel() - { - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - } - - public MqttTcpChannel(Socket socket) - { - _socket = socket ?? throw new ArgumentNullException(nameof(socket)); - } - - public Task ConnectAsync(MqttClientOptions options) - { - try - { - return _socket.ConnectAsync(options.Server, options.Port); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - public Task DisconnectAsync() - { - try - { - _socket.Dispose(); - return Task.FromResult(0); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - public Task WriteAsync(byte[] buffer) - { - if (buffer == null) throw new ArgumentNullException(nameof(buffer)); - - try - { - return _socket.SendAsync(new ArraySegment(buffer), SocketFlags.None); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - public Task ReadAsync(byte[] buffer) - { - try - { - var buffer2 = new ArraySegment(buffer); - return _socket.ReceiveAsync(buffer2, SocketFlags.None); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - public void Dispose() - { - _socket?.Dispose(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs new file mode 100644 index 00000000..f365efde --- /dev/null +++ b/Frameworks/MQTTnet.NetFramework/Implementations/MqttServerAdapter.cs @@ -0,0 +1,117 @@ +using System; +using System.Net; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; +using MQTTnet.Core.Adapter; +using MQTTnet.Core.Diagnostics; +using MQTTnet.Core.Serializer; +using MQTTnet.Core.Server; + +namespace MQTTnet.Implementations +{ + public class MqttServerAdapter : IMqttServerAdapter, IDisposable + { + private CancellationTokenSource _cancellationTokenSource; + private Socket _defaultEndpointSocket; + private Socket _sslEndpointSocket; + private X509Certificate2 _sslCertificate; + + private bool _isRunning; + + public event EventHandler ClientConnected; + + public void Start(MqttServerOptions options) + { + if (_isRunning) throw new InvalidOperationException("Server is already started."); + _isRunning = true; + + _cancellationTokenSource = new CancellationTokenSource(); + + if (options.DefaultEndpointOptions.IsEnabled) + { + _defaultEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); + _defaultEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetDefaultEndpointPort())); + _defaultEndpointSocket.Listen(options.ConnectionBacklog); + + Task.Run(() => AcceptDefaultEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); + } + + if (options.SslEndpointOptions.IsEnabled) + { + if (options.SslEndpointOptions.Certificate == null) + { + throw new ArgumentException("SSL certificate is not set."); + } + + _sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate); + + _sslEndpointSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); + _sslEndpointSocket.Bind(new IPEndPoint(IPAddress.Any, options.GetSslEndpointPort())); + _sslEndpointSocket.Listen(options.ConnectionBacklog); + + Task.Run(() => AcceptSslEndpointConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); + } + } + + public void Stop() + { + _isRunning = false; + + _cancellationTokenSource?.Dispose(); + _cancellationTokenSource = null; + + _defaultEndpointSocket?.Dispose(); + _defaultEndpointSocket = null; + + _sslEndpointSocket?.Dispose(); + _sslEndpointSocket = null; + } + + public void Dispose() + { + Stop(); + } + + private async Task AcceptDefaultEndpointConnectionsAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + var clientSocket = await Task.Factory.FromAsync(_defaultEndpointSocket.BeginAccept, _defaultEndpointSocket.EndAccept, null); + var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, null), new DefaultMqttV311PacketSerializer()); + ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); + } + catch (Exception exception) + { + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at default endpoint."); + } + } + } + + private async Task AcceptSslEndpointConnectionsAsync(CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + try + { + var clientSocket = await Task.Factory.FromAsync(_defaultEndpointSocket.BeginAccept, _defaultEndpointSocket.EndAccept, null); + + var sslStream = new SslStream(new NetworkStream(clientSocket)); + await sslStream.AuthenticateAsServerAsync(_sslCertificate, false, SslProtocols.Tls12, false); + + var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket, sslStream), new DefaultMqttV311PacketSerializer()); + ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); + } + catch (Exception exception) + { + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint."); + } + } + } + } +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs similarity index 52% rename from Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs rename to Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs index 80c76cff..e272c976 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.NetFramework/Implementations/MqttTcpChannel.cs @@ -1,31 +1,43 @@ using System; +using System.Net.Security; using System.Net.Sockets; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using MQTTnet.Core.Channel; using MQTTnet.Core.Client; using MQTTnet.Core.Exceptions; -namespace MQTTnet +namespace MQTTnet.Implementations { public class MqttTcpChannel : IMqttCommunicationChannel, IDisposable { private readonly Socket _socket; + private SslStream _sslStream; public MqttTcpChannel() { _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); } - public MqttTcpChannel(Socket socket) + public MqttTcpChannel(Socket socket, SslStream sslStream) { _socket = socket ?? throw new ArgumentNullException(nameof(socket)); + _sslStream = sslStream; } - public Task ConnectAsync(MqttClientOptions options) + public async Task ConnectAsync(MqttClientOptions options) { + if (options == null) throw new ArgumentNullException(nameof(options)); try { - return Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, null); + await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.GetPort(), null); + + if (options.SslOptions.UseSsl) + { + _sslStream = new SslStream(new NetworkStream(_socket, true)); + await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.SslOptions.CheckCertificateRevocation); + } } catch (SocketException exception) { @@ -37,7 +49,9 @@ namespace MQTTnet { try { - return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); + _sslStream.Dispose(); + _socket.Dispose(); + return Task.FromResult(0); } catch (SocketException exception) { @@ -51,10 +65,16 @@ namespace MQTTnet try { + if (_sslStream != null) + { + return _sslStream.WriteAsync(buffer, 0, buffer.Length); + } + return Task.Factory.FromAsync( // ReSharper disable once AssignNullToNotNullAttribute _socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null), _socket.EndSend); + } catch (SocketException exception) { @@ -64,8 +84,15 @@ namespace MQTTnet public Task ReadAsync(byte[] buffer) { + if (buffer == null) throw new ArgumentNullException(nameof(buffer)); + try { + if (_sslStream != null) + { + return _sslStream.ReadAsync(buffer, 0, buffer.Length); + } + return Task.Factory.FromAsync( // ReSharper disable once AssignNullToNotNullAttribute _socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null), @@ -80,6 +107,23 @@ namespace MQTTnet public void Dispose() { _socket?.Dispose(); + _sslStream?.Dispose(); + } + + private static X509CertificateCollection LoadCertificates(MqttClientOptions options) + { + var certificates = new X509CertificateCollection(); + if (options.SslOptions.Certificates == null) + { + return certificates; + } + + foreach (var certificate in options.SslOptions.Certificates) + { + certificates.Add(new X509Certificate(certificate)); + } + + return certificates; } } } \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/MQTTnet.NetFramework.csproj b/Frameworks/MQTTnet.NetFramework/MQTTnet.NetFramework.csproj index 314c158e..7f4ab44b 100644 --- a/Frameworks/MQTTnet.NetFramework/MQTTnet.NetFramework.csproj +++ b/Frameworks/MQTTnet.NetFramework/MQTTnet.NetFramework.csproj @@ -101,13 +101,10 @@ - - - - + - + diff --git a/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs b/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs index 54e06c4b..7b842393 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs @@ -1,8 +1,8 @@ using System; using MQTTnet.Core.Adapter; -using MQTTnet.Core.Channel; using MQTTnet.Core.Client; using MQTTnet.Core.Serializer; +using MQTTnet.Implementations; namespace MQTTnet { @@ -11,10 +11,8 @@ namespace MQTTnet public MqttClient CreateMqttClient(MqttClientOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); - - return new MqttClient(options, - new MqttChannelCommunicationAdapter(options.UseSSL ? new MqttClientSslChannel() : (IMqttCommunicationChannel)new MqttTcpChannel(), - new DefaultMqttV311PacketSerializer())); + + return new MqttClient(options, new MqttChannelCommunicationAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer())); } } -} +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs b/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs deleted file mode 100644 index 7fe9ca31..00000000 --- a/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.IO; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Threading.Tasks; -using MQTTnet.Core.Channel; -using MQTTnet.Core.Client; -using MQTTnet.Core.Exceptions; - -namespace MQTTnet -{ - /// - /// Describes an SSL channel to an MQTT server. - /// - public class MqttClientSslChannel : IMqttCommunicationChannel, IDisposable - { - private readonly Socket _socket; - private SslStream _sslStream; - - /// - /// Creates a new . - /// - public MqttClientSslChannel() - { - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - } - - /// - /// Creates a new with a predefined . - /// - /// - public MqttClientSslChannel(Socket socket) - { - _socket = socket ?? throw new ArgumentNullException(nameof(socket)); - } - - /// - /// Asynchronously connects to the host described in the . - /// - /// The describing the connection. - public async Task ConnectAsync(MqttClientOptions options) - { - try - { - await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, - null); - - NetworkStream ns = new NetworkStream(_socket, true); - _sslStream = new SslStream(ns); - - await _sslStream.AuthenticateAsClientAsync(options.Server, null, SslProtocols.Tls12, false); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously disconnects the client from the server. - /// - public Task DisconnectAsync() - { - try - { - return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously writes a sequence of bytes to the socket. - /// - /// The buffer to write data from. - public Task WriteAsync(byte[] buffer) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer)); - - try - { - return _sslStream.WriteAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Asynchronously reads a sequence of bytes from the socket. - /// - /// The buffer to write the data into. - public Task ReadAsync(byte[] buffer) - { - try - { - return _sslStream.ReadAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Releases all resources used by the . - /// - public void Dispose() - { - _sslStream?.Dispose(); - _socket?.Dispose(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetFramework/MqttServerAdapter.cs deleted file mode 100644 index b7b1b30a..00000000 --- a/Frameworks/MQTTnet.NetFramework/MqttServerAdapter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Threading; -using System.Threading.Tasks; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Serializer; -using MQTTnet.Core.Server; - -namespace MQTTnet -{ - public sealed class MqttServerAdapter : IMqttServerAdapter, IDisposable - { - private CancellationTokenSource _cancellationTokenSource; - private Socket _socket; - - public event EventHandler ClientConnected; - - public void Start(MqttServerOptions options) - { - if (_socket != null) throw new InvalidOperationException("Server is already started."); - - _cancellationTokenSource = new CancellationTokenSource(); - - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _socket.Bind(new IPEndPoint(IPAddress.Any, options.Port)); - _socket.Listen(options.ConnectionBacklog); - - Task.Run(async () => await AcceptConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); - } - - public void Stop() - { - _cancellationTokenSource?.Dispose(); - _cancellationTokenSource = null; - - _socket?.Dispose(); - _socket = null; - } - - public void Dispose() - { - Stop(); - } - - private async Task AcceptConnectionsAsync(CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - var clientSocket = await Task.Factory.FromAsync(_socket.BeginAccept, _socket.EndAccept, null); - var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(clientSocket), new DefaultMqttV311PacketSerializer()); - ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); - } - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/MqttServerFactory.cs b/Frameworks/MQTTnet.NetFramework/MqttServerFactory.cs index 71e57afe..eb7441c3 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttServerFactory.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttServerFactory.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using MQTTnet.Core.Adapter; using MQTTnet.Core.Server; +using MQTTnet.Implementations; namespace MQTTnet { @@ -9,8 +11,8 @@ namespace MQTTnet public MqttServer CreateMqttServer(MqttServerOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); - - return new MqttServer(options, options.UseSSL ? (IMqttServerAdapter) new MqttSslServerAdapter() : new MqttServerAdapter()); + + return new MqttServer(options, new List { new MqttServerAdapter() }); } } } diff --git a/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs b/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs deleted file mode 100644 index 699948de..00000000 --- a/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System; -using System.IO; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using System.Threading.Tasks; -using MQTTnet.Core.Channel; -using MQTTnet.Core.Client; -using MQTTnet.Core.Exceptions; - -namespace MQTTnet -{ - /// - /// Describes an SSL channel to a client. - /// - public class MqttServerSslChannel : IMqttCommunicationChannel, IDisposable - { - private readonly Socket _socket; - private SslStream _sslStream; - private X509Certificate2 _cert; - - /// - /// Creates a new with a predefined . - /// - /// The client socket. - /// The X509 certificate used to authenticate as a server. - public MqttServerSslChannel(Socket socket, X509Certificate2 cert) - { - _socket = socket ?? throw new ArgumentNullException(nameof(socket)); - _cert = cert ?? throw new ArgumentNullException(nameof(cert)); - - if (!_socket.Connected) - return; - - NetworkStream ns = new NetworkStream(_socket, true); - _sslStream = new SslStream(ns); - } - - public async Task Authenticate() - { - await _sslStream.AuthenticateAsServerAsync(_cert, false, SslProtocols.Tls12, false); - } - - /// - /// Asynchronously connects to the client described in the . - /// - /// The describing the connection. - public Task ConnectAsync(MqttClientOptions options) - { - try - { - return Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, - null); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously disconnects the client from the server. - /// - public Task DisconnectAsync() - { - try - { - return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); - } - catch (SocketException exception) - { - throw new MqttCommunicationException(exception); - } - } - - /// - /// Asynchronously writes a sequence of bytes to the socket. - /// - /// The buffer to write data from. - public Task WriteAsync(byte[] buffer) - { - if (buffer == null) - throw new ArgumentNullException(nameof(buffer)); - - try - { - return _sslStream.WriteAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Asynchronously reads a sequence of bytes from the socket. - /// - /// The buffer to write the data into. - public Task ReadAsync(byte[] buffer) - { - try - { - return _sslStream.ReadAsync(buffer, 0, buffer.Length); - } - catch (Exception ex) - when (ex is SocketException || ex is IOException) - { - throw new MqttCommunicationException(ex); - } - } - - /// - /// Releases all resources used by the . - /// - public void Dispose() - { - _sslStream?.Dispose(); - _socket?.Dispose(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetFramework/MqttSslServerAdapter.cs b/Frameworks/MQTTnet.NetFramework/MqttSslServerAdapter.cs deleted file mode 100644 index f02c74b8..00000000 --- a/Frameworks/MQTTnet.NetFramework/MqttSslServerAdapter.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using System.Threading.Tasks; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Serializer; -using MQTTnet.Core.Server; - -namespace MQTTnet -{ - public class MqttSslServerAdapter : IMqttServerAdapter, IDisposable - { - private CancellationTokenSource _cancellationTokenSource; - private Socket _socket; - private X509Certificate2 _x590Certificate2; - - public event EventHandler ClientConnected; - - public void Start(MqttServerOptions options) - { - if (_socket != null) throw new InvalidOperationException("Server is already started."); - - _cancellationTokenSource = new CancellationTokenSource(); - - _socket = new Socket(SocketType.Stream, ProtocolType.Tcp); - _socket.Bind(new IPEndPoint(IPAddress.Any, options.Port)); - _socket.Listen(options.ConnectionBacklog); - - Task.Run(async () => await AcceptConnectionsAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); - - _x590Certificate2 = new X509Certificate2(options.CertificatePath); - } - - public void Stop() - { - _cancellationTokenSource?.Dispose(); - _cancellationTokenSource = null; - - _socket?.Dispose(); - _socket = null; - } - - public void Dispose() - { - Stop(); - } - - private async Task AcceptConnectionsAsync(CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - var clientSocket = await Task.Factory.FromAsync(_socket.BeginAccept, _socket.EndAccept, null); - - MqttServerSslChannel mssc = new MqttServerSslChannel(clientSocket, _x590Certificate2); - await mssc.Authenticate(); - - var clientAdapter = new MqttChannelCommunicationAdapter(mssc, new DefaultMqttV311PacketSerializer()); - ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(clientSocket.RemoteEndPoint.ToString(), clientAdapter)); - } - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs new file mode 100644 index 00000000..f8fe07f9 --- /dev/null +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttServerAdapter.cs @@ -0,0 +1,90 @@ +using System; +using System.Security.Cryptography.X509Certificates; +using MQTTnet.Core.Adapter; +using MQTTnet.Core.Diagnostics; +using MQTTnet.Core.Serializer; +using MQTTnet.Core.Server; +using Windows.Networking.Sockets; + +namespace MQTTnet.Implementations +{ + public class MqttServerAdapter : IMqttServerAdapter, IDisposable + { + private StreamSocketListener _defaultEndpointSocket; + private StreamSocketListener _sslEndpointSocket; + private X509Certificate2 _sslCertificate; + + private bool _isRunning; + + public event EventHandler ClientConnected; + + public void Start(MqttServerOptions options) + { + if (_isRunning) throw new InvalidOperationException("Server is already started."); + _isRunning = true; + + if (options.DefaultEndpointOptions.IsEnabled) + { + _defaultEndpointSocket = new StreamSocketListener(); + _defaultEndpointSocket.BindServiceNameAsync(options.GetDefaultEndpointPort().ToString(), SocketProtectionLevel.PlainSocket).GetAwaiter().GetResult(); + _defaultEndpointSocket.ConnectionReceived += AcceptDefaultEndpointConnectionsAsync; + } + + if (options.SslEndpointOptions.IsEnabled) + { + if (options.SslEndpointOptions.Certificate == null) + { + throw new ArgumentException("SSL certificate is not set."); + } + + _sslCertificate = new X509Certificate2(options.SslEndpointOptions.Certificate); + + _sslEndpointSocket = new StreamSocketListener(); + _sslEndpointSocket.BindServiceNameAsync(options.GetSslEndpointPort().ToString(), SocketProtectionLevel.Tls12).GetAwaiter().GetResult(); + _sslEndpointSocket.ConnectionReceived += AcceptSslEndpointConnectionsAsync; + } + } + + public void Stop() + { + _isRunning = false; + + _defaultEndpointSocket?.Dispose(); + _defaultEndpointSocket = null; + + _sslEndpointSocket?.Dispose(); + _sslEndpointSocket = null; + } + + public void Dispose() + { + Stop(); + } + + private void AcceptDefaultEndpointConnectionsAsync(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) + { + try + { + var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer()); + ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(args.Socket.Information.RemoteAddress.ToString(), clientAdapter)); + } + catch (Exception exception) + { + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at default endpoint."); + } + } + + private void AcceptSslEndpointConnectionsAsync(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) + { + try + { + var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer()); + ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(args.Socket.Information.RemoteAddress.ToString(), clientAdapter)); + } + catch (Exception exception) + { + MqttTrace.Error(nameof(MqttServerAdapter), exception, "Error while acceping connection at SSL endpoint."); + } + } + } +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs new file mode 100644 index 00000000..042fcb0d --- /dev/null +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs @@ -0,0 +1,121 @@ +using System; +using System.Linq; +using System.Net.Sockets; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; +using Windows.Networking; +using Windows.Networking.Sockets; +using Windows.Security.Cryptography.Certificates; +using Windows.Storage.Streams; +using MQTTnet.Core.Channel; +using MQTTnet.Core.Client; +using MQTTnet.Core.Exceptions; + +namespace MQTTnet.Implementations +{ + public class MqttTcpChannel : IMqttCommunicationChannel, IDisposable + { + private readonly StreamSocket _socket; + + public MqttTcpChannel() + { + _socket = new StreamSocket(); + } + + public MqttTcpChannel(StreamSocket socket) + { + _socket = socket ?? throw new ArgumentNullException(nameof(socket)); + } + + public async Task ConnectAsync(MqttClientOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + try + { + if (!options.SslOptions.UseSsl) + { + await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString()); + } + else + { + _socket.Control.ClientCertificate = LoadCertificate(options); + + if (!options.SslOptions.CheckCertificateRevocation) + { + _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked); + _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain); + _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing); + } + + await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString(), SocketProtectionLevel.Tls12); + } + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public Task DisconnectAsync() + { + try + { + _socket.Dispose(); + return Task.FromResult(0); + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public async Task WriteAsync(byte[] buffer) + { + if (buffer == null) throw new ArgumentNullException(nameof(buffer)); + + try + { + await _socket.OutputStream.WriteAsync(buffer.AsBuffer()); + await _socket.OutputStream.FlushAsync(); + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public async Task ReadAsync(byte[] buffer) + { + if (buffer == null) throw new ArgumentNullException(nameof(buffer)); + + try + { + await _socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length, InputStreamOptions.Partial); + } + catch (SocketException exception) + { + throw new MqttCommunicationException(exception); + } + } + + public void Dispose() + { + _socket?.Dispose(); + } + + private static Certificate LoadCertificate(MqttClientOptions options) + { + if (options.SslOptions.Certificates == null || !options.SslOptions.Certificates.Any()) + { + return null; + } + + if (options.SslOptions.Certificates.Count > 1) + { + throw new NotSupportedException("Only one client certificate is supported for UWP."); + } + + return new Certificate(options.SslOptions.Certificates.First().AsBuffer()); + } + } +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj b/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj index 2323cd08..9022957f 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj +++ b/Frameworks/MQTTnet.UniversalWindows/MQTTnet.UniversalWindows.csproj @@ -112,9 +112,9 @@ - + - + diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs index 4da24a17..7b842393 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.UniversalWindows/MqttClientFactory.cs @@ -2,6 +2,7 @@ using MQTTnet.Core.Adapter; using MQTTnet.Core.Client; using MQTTnet.Core.Serializer; +using MQTTnet.Implementations; namespace MQTTnet { @@ -14,4 +15,4 @@ namespace MQTTnet return new MqttClient(options, new MqttChannelCommunicationAdapter(new MqttTcpChannel(), new DefaultMqttV311PacketSerializer())); } } -} +} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttServerAdapter.cs b/Frameworks/MQTTnet.UniversalWindows/MqttServerAdapter.cs deleted file mode 100644 index b9eeecd4..00000000 --- a/Frameworks/MQTTnet.UniversalWindows/MqttServerAdapter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Threading; -using Windows.Networking.Sockets; -using MQTTnet.Core.Adapter; -using MQTTnet.Core.Serializer; -using MQTTnet.Core.Server; - -namespace MQTTnet -{ - public sealed class MqttServerAdapter : IMqttServerAdapter, IDisposable - { - private CancellationTokenSource _cancellationTokenSource; - private StreamSocketListener _socket; - - public event EventHandler ClientConnected; - - public void Start(MqttServerOptions options) - { - if (_socket != null) throw new InvalidOperationException("Server is already started."); - - _cancellationTokenSource = new CancellationTokenSource(); - - _socket = new StreamSocketListener(); - _socket.BindServiceNameAsync(options.Port.ToString()).AsTask().Wait(); - _socket.ConnectionReceived += ConnectionReceived; - } - - private void ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args) - { - var clientAdapter = new MqttChannelCommunicationAdapter(new MqttTcpChannel(args.Socket), new DefaultMqttV311PacketSerializer()); - - var identifier = $"{args.Socket.Information.RemoteAddress}:{args.Socket.Information.RemotePort}"; - ClientConnected?.Invoke(this, new MqttClientConnectedEventArgs(identifier, clientAdapter)); - } - - public void Stop() - { - _cancellationTokenSource?.Dispose(); - - if (_socket != null) - { - _socket.ConnectionReceived -= ConnectionReceived; - } - - _socket?.Dispose(); - _socket = null; - } - - public void Dispose() - { - Stop(); - } - } -} \ No newline at end of file diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs index a7254265..eb7441c3 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs +++ b/Frameworks/MQTTnet.UniversalWindows/MqttServerFactory.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using MQTTnet.Core.Adapter; using MQTTnet.Core.Server; +using MQTTnet.Implementations; namespace MQTTnet { @@ -9,7 +12,7 @@ namespace MQTTnet { if (options == null) throw new ArgumentNullException(nameof(options)); - return new MqttServer(options, new MqttServerAdapter()); + return new MqttServer(options, new List { new MqttServerAdapter() }); } } } diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttTcpChannel.cs b/Frameworks/MQTTnet.UniversalWindows/MqttTcpChannel.cs deleted file mode 100644 index 5763df42..00000000 --- a/Frameworks/MQTTnet.UniversalWindows/MqttTcpChannel.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Runtime.InteropServices.WindowsRuntime; -using System.Threading.Tasks; -using Windows.Networking; -using Windows.Networking.Sockets; -using Windows.Storage.Streams; -using MQTTnet.Core.Channel; -using MQTTnet.Core.Client; -using Buffer = Windows.Storage.Streams.Buffer; - -namespace MQTTnet -{ - public sealed class MqttTcpChannel : IMqttCommunicationChannel, IDisposable - { - private readonly StreamSocket _socket; - - public MqttTcpChannel() - { - _socket = new StreamSocket(); - } - - public MqttTcpChannel(StreamSocket socket) - { - _socket = socket ?? throw new ArgumentNullException(nameof(socket)); - } - - public async Task ConnectAsync(MqttClientOptions options) - { - if (options == null) throw new ArgumentNullException(nameof(options)); - - await _socket.ConnectAsync(new HostName(options.Server), options.Port.ToString()); - } - - public async Task DisconnectAsync() - { - await _socket.CancelIOAsync(); - _socket.Dispose(); - } - - public async Task WriteAsync(byte[] buffer) - { - if (buffer == null) throw new ArgumentNullException(nameof(buffer)); - - await _socket.OutputStream.WriteAsync(buffer.AsBuffer()); - await _socket.OutputStream.FlushAsync(); - } - - public async Task ReadAsync(byte[] buffer) - { - var buffer2 = new Buffer((uint)buffer.Length); - await _socket.InputStream.ReadAsync(buffer2, (uint)buffer.Length, InputStreamOptions.None); - - var array2 = buffer2.ToArray(); - Array.Copy(array2, buffer, array2.Length); - } - - public void Dispose() - { - _socket?.Dispose(); - } - } -} \ No newline at end of file diff --git a/MQTTnet.Core/Client/MqttClient.cs b/MQTTnet.Core/Client/MqttClient.cs index 412563e5..85295236 100644 --- a/MQTTnet.Core/Client/MqttClient.cs +++ b/MQTTnet.Core/Client/MqttClient.cs @@ -67,7 +67,9 @@ namespace MQTTnet.Core.Client _packetDispatcher.Reset(); IsConnected = true; - Task.Run(() => ReceivePackets(_cancellationTokenSource.Token), _cancellationTokenSource.Token).Forget(); +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + Task.Run(() => ReceivePackets(_cancellationTokenSource.Token), _cancellationTokenSource.Token); +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed var response = await SendAndReceiveAsync(connectPacket); if (response.ConnectReturnCode != MqttConnectReturnCode.ConnectionAccepted) @@ -77,7 +79,9 @@ namespace MQTTnet.Core.Client if (_options.KeepAlivePeriod != TimeSpan.Zero) { - Task.Run(() => SendKeepAliveMessagesAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token).Forget(); +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + Task.Run(() => SendKeepAliveMessagesAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token); +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } Connected?.Invoke(this, EventArgs.Empty); @@ -350,7 +354,9 @@ namespace MQTTnet.Core.Client var mqttPacket = await _adapter.ReceivePacketAsync(TimeSpan.Zero); MqttTrace.Information(nameof(MqttClient), $"Received <<< {mqttPacket}"); - Task.Run(() => ProcessReceivedPacket(mqttPacket), cancellationToken).Forget(); +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed + Task.Run(() => ProcessReceivedPacket(mqttPacket), cancellationToken); +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } } catch (MqttCommunicationException exception) diff --git a/MQTTnet.Core/Client/MqttClientOptions.cs b/MQTTnet.Core/Client/MqttClientOptions.cs index d306bfee..9eebdd5f 100644 --- a/MQTTnet.Core/Client/MqttClientOptions.cs +++ b/MQTTnet.Core/Client/MqttClientOptions.cs @@ -2,11 +2,13 @@ namespace MQTTnet.Core.Client { - public class MqttClientOptions + public sealed class MqttClientOptions { public string Server { get; set; } - public int Port { get; set; } = 1883; + public int? Port { get; set; } + + public MqttClientSslOptions SslOptions { get; } = new MqttClientSslOptions(); public string UserName { get; set; } @@ -19,24 +21,5 @@ namespace MQTTnet.Core.Client public TimeSpan KeepAlivePeriod { get; set; } = TimeSpan.FromSeconds(5); public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(10); - - /// - /// Use SSL to communicate with the MQTT server. - /// - /// Setting this value to true will also set to 8883 if its value was 1883 (not set). - public bool UseSSL - { - get => _useSSL; - set - { - // Automatically set the port to the MQTT SSL port (8883) if it wasn't set already - if (value && Port == 1883) - Port = 8883; - - _useSSL = value; - } - } - - private bool _useSSL; } } diff --git a/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs b/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs new file mode 100644 index 00000000..404dd01f --- /dev/null +++ b/MQTTnet.Core/Client/MqttClientOptionsExtensions.cs @@ -0,0 +1,19 @@ +using System; + +namespace MQTTnet.Core.Client +{ + public static class MqttClientOptionsExtensions + { + public static int GetPort(this MqttClientOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + + if (options.Port.HasValue) + { + return options.Port.Value; + } + + return !options.SslOptions.UseSsl ? 1883 : 8883; + } + } +} diff --git a/MQTTnet.Core/Client/MqttClientSslOptions.cs b/MQTTnet.Core/Client/MqttClientSslOptions.cs new file mode 100644 index 00000000..3185d10d --- /dev/null +++ b/MQTTnet.Core/Client/MqttClientSslOptions.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace MQTTnet.Core.Client +{ + public sealed class MqttClientSslOptions + { + public bool UseSsl { get; set; } + + public bool CheckCertificateRevocation { get; set; } + + public List Certificates { get; set; } + } +} diff --git a/MQTTnet.Core/Diagnostics/MqttTraceMessagePublishedEventArgs.cs b/MQTTnet.Core/Diagnostics/MqttTraceMessagePublishedEventArgs.cs index eb73c309..9ef3dcd6 100644 --- a/MQTTnet.Core/Diagnostics/MqttTraceMessagePublishedEventArgs.cs +++ b/MQTTnet.Core/Diagnostics/MqttTraceMessagePublishedEventArgs.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Diagnostics { - public class MqttTraceMessagePublishedEventArgs : EventArgs + public sealed class MqttTraceMessagePublishedEventArgs : EventArgs { public MqttTraceMessagePublishedEventArgs(int threadId, string source, MqttTraceLevel level, string message, Exception exception) { diff --git a/MQTTnet.Core/Exceptions/MqttCommunicationTimedOutException.cs b/MQTTnet.Core/Exceptions/MqttCommunicationTimedOutException.cs index 2e707d73..e7240072 100644 --- a/MQTTnet.Core/Exceptions/MqttCommunicationTimedOutException.cs +++ b/MQTTnet.Core/Exceptions/MqttCommunicationTimedOutException.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Exceptions { - public class MqttCommunicationTimedOutException : MqttCommunicationException + public sealed class MqttCommunicationTimedOutException : MqttCommunicationException { } } diff --git a/MQTTnet.Core/Exceptions/MqttProtocolViolationException.cs b/MQTTnet.Core/Exceptions/MqttProtocolViolationException.cs index 95d72b8c..a4724d8a 100644 --- a/MQTTnet.Core/Exceptions/MqttProtocolViolationException.cs +++ b/MQTTnet.Core/Exceptions/MqttProtocolViolationException.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Exceptions { - public class MqttProtocolViolationException : Exception + public sealed class MqttProtocolViolationException : Exception { public MqttProtocolViolationException(string message) : base(message) diff --git a/MQTTnet.Core/Internal/AsyncAutoResetEvent.cs b/MQTTnet.Core/Internal/AsyncAutoResetEvent.cs index c0f4972e..795748d6 100644 --- a/MQTTnet.Core/Internal/AsyncAutoResetEvent.cs +++ b/MQTTnet.Core/Internal/AsyncAutoResetEvent.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; namespace MQTTnet.Core.Internal { - public class AsyncGate + public sealed class AsyncGate { private readonly Queue> _waitingTasks = new Queue>(); diff --git a/MQTTnet.Core/Internal/TaskExtensions.cs b/MQTTnet.Core/Internal/TaskExtensions.cs deleted file mode 100644 index 418deb09..00000000 --- a/MQTTnet.Core/Internal/TaskExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Threading.Tasks; - -namespace MQTTnet.Core.Internal -{ - internal static class TaskExtensions - { - public static void Forget(this Task task) - { - } - } -} diff --git a/MQTTnet.Core/MqttApplicationMessage.cs b/MQTTnet.Core/MqttApplicationMessage.cs index 1b4a9d5f..382d702c 100644 --- a/MQTTnet.Core/MqttApplicationMessage.cs +++ b/MQTTnet.Core/MqttApplicationMessage.cs @@ -3,7 +3,7 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core { - public class MqttApplicationMessage + public sealed class MqttApplicationMessage { public MqttApplicationMessage(string topic, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel, bool retain) { diff --git a/MQTTnet.Core/MqttApplicationMessageReceivedEventArgs.cs b/MQTTnet.Core/MqttApplicationMessageReceivedEventArgs.cs index 005f9993..c624beac 100644 --- a/MQTTnet.Core/MqttApplicationMessageReceivedEventArgs.cs +++ b/MQTTnet.Core/MqttApplicationMessageReceivedEventArgs.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core { - public class MqttApplicationMessageReceivedEventArgs : EventArgs + public sealed class MqttApplicationMessageReceivedEventArgs : EventArgs { public MqttApplicationMessageReceivedEventArgs(MqttApplicationMessage applicationMessage) { diff --git a/MQTTnet.Core/Packets/MqttConnAckPacket.cs b/MQTTnet.Core/Packets/MqttConnAckPacket.cs index 3d262dc3..d8a5fd93 100644 --- a/MQTTnet.Core/Packets/MqttConnAckPacket.cs +++ b/MQTTnet.Core/Packets/MqttConnAckPacket.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Packets { - public class MqttConnAckPacket : MqttBasePacket + public sealed class MqttConnAckPacket : MqttBasePacket { public bool IsSessionPresent { get; set; } diff --git a/MQTTnet.Core/Packets/MqttConnectPacket.cs b/MQTTnet.Core/Packets/MqttConnectPacket.cs index 9adec4d9..aadfe52d 100644 --- a/MQTTnet.Core/Packets/MqttConnectPacket.cs +++ b/MQTTnet.Core/Packets/MqttConnectPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttConnectPacket: MqttBasePacket + public sealed class MqttConnectPacket: MqttBasePacket { public string ClientId { get; set; } diff --git a/MQTTnet.Core/Packets/MqttDisconnectPacket.cs b/MQTTnet.Core/Packets/MqttDisconnectPacket.cs index 4e3659a0..5be8407f 100644 --- a/MQTTnet.Core/Packets/MqttDisconnectPacket.cs +++ b/MQTTnet.Core/Packets/MqttDisconnectPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttDisconnectPacket : MqttBasePacket + public sealed class MqttDisconnectPacket : MqttBasePacket { } } diff --git a/MQTTnet.Core/Packets/MqttPingReqPacket.cs b/MQTTnet.Core/Packets/MqttPingReqPacket.cs index 9640e641..3df1c3ae 100644 --- a/MQTTnet.Core/Packets/MqttPingReqPacket.cs +++ b/MQTTnet.Core/Packets/MqttPingReqPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttPingReqPacket : MqttBasePacket + public sealed class MqttPingReqPacket : MqttBasePacket { public override string ToString() { diff --git a/MQTTnet.Core/Packets/MqttPingRespPacket.cs b/MQTTnet.Core/Packets/MqttPingRespPacket.cs index d7a837ca..753a357d 100644 --- a/MQTTnet.Core/Packets/MqttPingRespPacket.cs +++ b/MQTTnet.Core/Packets/MqttPingRespPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttPingRespPacket : MqttBasePacket + public sealed class MqttPingRespPacket : MqttBasePacket { public override string ToString() { diff --git a/MQTTnet.Core/Packets/MqttPubAckPacket.cs b/MQTTnet.Core/Packets/MqttPubAckPacket.cs index 2b182bff..b3e5d3e3 100644 --- a/MQTTnet.Core/Packets/MqttPubAckPacket.cs +++ b/MQTTnet.Core/Packets/MqttPubAckPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttPubAckPacket : MqttBasePublishPacket + public sealed class MqttPubAckPacket : MqttBasePublishPacket { } } diff --git a/MQTTnet.Core/Packets/MqttPubCompPacket.cs b/MQTTnet.Core/Packets/MqttPubCompPacket.cs index 47f06a6c..cb6b485f 100644 --- a/MQTTnet.Core/Packets/MqttPubCompPacket.cs +++ b/MQTTnet.Core/Packets/MqttPubCompPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttPubCompPacket : MqttBasePublishPacket + public sealed class MqttPubCompPacket : MqttBasePublishPacket { } } diff --git a/MQTTnet.Core/Packets/MqttPubRecPacket.cs b/MQTTnet.Core/Packets/MqttPubRecPacket.cs index 5d5b3751..a8ed3337 100644 --- a/MQTTnet.Core/Packets/MqttPubRecPacket.cs +++ b/MQTTnet.Core/Packets/MqttPubRecPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttPubRecPacket : MqttBasePublishPacket + public sealed class MqttPubRecPacket : MqttBasePublishPacket { } } diff --git a/MQTTnet.Core/Packets/MqttPubRelPacket.cs b/MQTTnet.Core/Packets/MqttPubRelPacket.cs index 2a556b7b..c95d16bf 100644 --- a/MQTTnet.Core/Packets/MqttPubRelPacket.cs +++ b/MQTTnet.Core/Packets/MqttPubRelPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttPubRelPacket : MqttBasePublishPacket + public sealed class MqttPubRelPacket : MqttBasePublishPacket { } } diff --git a/MQTTnet.Core/Packets/MqttPublishPacket.cs b/MQTTnet.Core/Packets/MqttPublishPacket.cs index 4bef319a..750a1521 100644 --- a/MQTTnet.Core/Packets/MqttPublishPacket.cs +++ b/MQTTnet.Core/Packets/MqttPublishPacket.cs @@ -3,7 +3,7 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core.Packets { - public class MqttPublishPacket : MqttBasePublishPacket + public sealed class MqttPublishPacket : MqttBasePublishPacket { public bool Retain { get; set; } diff --git a/MQTTnet.Core/Packets/MqttSubAckPacket.cs b/MQTTnet.Core/Packets/MqttSubAckPacket.cs index f46ebe03..3a7265f4 100644 --- a/MQTTnet.Core/Packets/MqttSubAckPacket.cs +++ b/MQTTnet.Core/Packets/MqttSubAckPacket.cs @@ -4,7 +4,7 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core.Packets { - public class MqttSubAckPacket : MqttBasePacket, IPacketWithIdentifier + public sealed class MqttSubAckPacket : MqttBasePacket, IPacketWithIdentifier { public ushort PacketIdentifier { get; set; } diff --git a/MQTTnet.Core/Packets/MqttSubscribePacket.cs b/MQTTnet.Core/Packets/MqttSubscribePacket.cs index dfc815c0..a0949e8b 100644 --- a/MQTTnet.Core/Packets/MqttSubscribePacket.cs +++ b/MQTTnet.Core/Packets/MqttSubscribePacket.cs @@ -3,7 +3,7 @@ using System.Linq; namespace MQTTnet.Core.Packets { - public class MqttSubscribePacket : MqttBasePacket, IPacketWithIdentifier + public sealed class MqttSubscribePacket : MqttBasePacket, IPacketWithIdentifier { public ushort PacketIdentifier { get; set; } diff --git a/MQTTnet.Core/Packets/MqttUnsubAckPacket.cs b/MQTTnet.Core/Packets/MqttUnsubAckPacket.cs index 389a5a79..5f330955 100644 --- a/MQTTnet.Core/Packets/MqttUnsubAckPacket.cs +++ b/MQTTnet.Core/Packets/MqttUnsubAckPacket.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Packets { - public class MqttUnsubAckPacket : MqttBasePacket, IPacketWithIdentifier + public sealed class MqttUnsubAckPacket : MqttBasePacket, IPacketWithIdentifier { public ushort PacketIdentifier { get; set; } } diff --git a/MQTTnet.Core/Packets/MqttUnsubscribe.cs b/MQTTnet.Core/Packets/MqttUnsubscribe.cs index 88df0347..3e3f36f8 100644 --- a/MQTTnet.Core/Packets/MqttUnsubscribe.cs +++ b/MQTTnet.Core/Packets/MqttUnsubscribe.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Packets { - public class MqttUnsubscribePacket : MqttBasePacket, IPacketWithIdentifier + public sealed class MqttUnsubscribePacket : MqttBasePacket, IPacketWithIdentifier { public ushort PacketIdentifier { get; set; } diff --git a/MQTTnet.Core/Packets/TopicFilter.cs b/MQTTnet.Core/Packets/TopicFilter.cs index 569f9e6c..e2ae4223 100644 --- a/MQTTnet.Core/Packets/TopicFilter.cs +++ b/MQTTnet.Core/Packets/TopicFilter.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Packets { - public class TopicFilter + public sealed class TopicFilter { public TopicFilter(string topic, MqttQualityOfServiceLevel qualityOfServiceLevel) { diff --git a/MQTTnet.Core/Serializer/ByteReader.cs b/MQTTnet.Core/Serializer/ByteReader.cs index a95f056c..935c777a 100644 --- a/MQTTnet.Core/Serializer/ByteReader.cs +++ b/MQTTnet.Core/Serializer/ByteReader.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Serializer { - public class ByteReader + public sealed class ByteReader { private readonly int _source; private int _index; diff --git a/MQTTnet.Core/Serializer/ByteWriter.cs b/MQTTnet.Core/Serializer/ByteWriter.cs index fbaf1755..d6abec90 100644 --- a/MQTTnet.Core/Serializer/ByteWriter.cs +++ b/MQTTnet.Core/Serializer/ByteWriter.cs @@ -2,7 +2,7 @@ namespace MQTTnet.Core.Serializer { - public class ByteWriter + public sealed class ByteWriter { private int _index; private int _byte; diff --git a/MQTTnet.Core/Serializer/DefaultMqttV311PacketSerializer.cs b/MQTTnet.Core/Serializer/DefaultMqttV311PacketSerializer.cs index 76071cfc..ffac41b9 100644 --- a/MQTTnet.Core/Serializer/DefaultMqttV311PacketSerializer.cs +++ b/MQTTnet.Core/Serializer/DefaultMqttV311PacketSerializer.cs @@ -9,7 +9,7 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core.Serializer { - public class DefaultMqttV311PacketSerializer : IMqttPacketSerializer + public sealed class DefaultMqttV311PacketSerializer : IMqttPacketSerializer { public Task SerializeAsync(MqttBasePacket packet, IMqttCommunicationChannel destination) { diff --git a/MQTTnet.Core/Server/DefaultEndpointOptions.cs b/MQTTnet.Core/Server/DefaultEndpointOptions.cs new file mode 100644 index 00000000..203ed289 --- /dev/null +++ b/MQTTnet.Core/Server/DefaultEndpointOptions.cs @@ -0,0 +1,9 @@ +namespace MQTTnet.Core.Server +{ + public sealed class DefaultEndpointOptions + { + public bool IsEnabled { get; set; } = true; + + public int? Port { get; set; } + } +} diff --git a/MQTTnet.Core/Server/GetOrCreateClientSessionResult.cs b/MQTTnet.Core/Server/GetOrCreateClientSessionResult.cs index ea62122e..8f802720 100644 --- a/MQTTnet.Core/Server/GetOrCreateClientSessionResult.cs +++ b/MQTTnet.Core/Server/GetOrCreateClientSessionResult.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Server { - public class GetOrCreateClientSessionResult + public sealed class GetOrCreateClientSessionResult { public bool IsExistingSession { get; set; } diff --git a/MQTTnet.Core/Server/MqttClientMessageQueue.cs b/MQTTnet.Core/Server/MqttClientMessageQueue.cs index 63b4881e..926e5817 100644 --- a/MQTTnet.Core/Server/MqttClientMessageQueue.cs +++ b/MQTTnet.Core/Server/MqttClientMessageQueue.cs @@ -11,7 +11,7 @@ using MQTTnet.Core.Packets; namespace MQTTnet.Core.Server { - public class MqttClientMessageQueue + public sealed class MqttClientMessageQueue { private readonly List _pendingPublishPackets = new List(); private readonly AsyncGate _gate = new AsyncGate(); diff --git a/MQTTnet.Core/Server/MqttClientPublishPacketContext.cs b/MQTTnet.Core/Server/MqttClientPublishPacketContext.cs index 3d93e4b3..8847c708 100644 --- a/MQTTnet.Core/Server/MqttClientPublishPacketContext.cs +++ b/MQTTnet.Core/Server/MqttClientPublishPacketContext.cs @@ -3,7 +3,7 @@ using MQTTnet.Core.Packets; namespace MQTTnet.Core.Server { - public class MqttClientPublishPacketContext + public sealed class MqttClientPublishPacketContext { public MqttClientPublishPacketContext(MqttClientSession senderClientSession, MqttPublishPacket publishPacket) { diff --git a/MQTTnet.Core/Server/MqttClientSessionsManager.cs b/MQTTnet.Core/Server/MqttClientSessionsManager.cs index 1dcbd442..2a9a0b21 100644 --- a/MQTTnet.Core/Server/MqttClientSessionsManager.cs +++ b/MQTTnet.Core/Server/MqttClientSessionsManager.cs @@ -10,7 +10,7 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core.Server { - public class MqttClientSessionsManager + public sealed class MqttClientSessionsManager { private readonly object _syncRoot = new object(); private readonly Dictionary _clientSessions = new Dictionary(); diff --git a/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs b/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs index 7064117d..8cb95634 100644 --- a/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs +++ b/MQTTnet.Core/Server/MqttClientSubscriptionsManager.cs @@ -5,7 +5,7 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core.Server { - public class MqttClientSubscriptionsManager + public sealed class MqttClientSubscriptionsManager { private readonly ConcurrentDictionary _subscribedTopics = new ConcurrentDictionary(); diff --git a/MQTTnet.Core/Server/MqttServer.cs b/MQTTnet.Core/Server/MqttServer.cs index 0b689e21..e1cda967 100644 --- a/MQTTnet.Core/Server/MqttServer.cs +++ b/MQTTnet.Core/Server/MqttServer.cs @@ -7,18 +7,18 @@ using MQTTnet.Core.Diagnostics; namespace MQTTnet.Core.Server { - public class MqttServer + public sealed class MqttServer { private readonly MqttClientSessionsManager _clientSessionsManager; - private readonly IMqttServerAdapter _adapter; + private readonly ICollection _adapters; private readonly MqttServerOptions _options; private CancellationTokenSource _cancellationTokenSource; - public MqttServer(MqttServerOptions options, IMqttServerAdapter adapter) + public MqttServer(MqttServerOptions options, ICollection adapters) { _options = options ?? throw new ArgumentNullException(nameof(options)); - _adapter = adapter ?? throw new ArgumentNullException(nameof(adapter)); + _adapters = adapters ?? throw new ArgumentNullException(nameof(adapters)); _clientSessionsManager = new MqttClientSessionsManager(options); } @@ -45,9 +45,12 @@ namespace MQTTnet.Core.Server _cancellationTokenSource = new CancellationTokenSource(); - _adapter.ClientConnected += OnClientConnected; - _adapter.Start(_options); - + foreach (var adapter in _adapters) + { + adapter.ClientConnected += OnClientConnected; + adapter.Start(_options); + } + MqttTrace.Information(nameof(MqttServer), "Started."); } @@ -56,8 +59,11 @@ namespace MQTTnet.Core.Server _cancellationTokenSource?.Cancel(); _cancellationTokenSource = null; - _adapter.ClientConnected -= OnClientConnected; - _adapter.Stop(); + foreach (var adapter in _adapters) + { + adapter.ClientConnected -= OnClientConnected; + adapter.Stop(); + } _clientSessionsManager.Clear(); diff --git a/MQTTnet.Core/Server/MqttServerOptions.cs b/MQTTnet.Core/Server/MqttServerOptions.cs index 0638700e..c93870e3 100644 --- a/MQTTnet.Core/Server/MqttServerOptions.cs +++ b/MQTTnet.Core/Server/MqttServerOptions.cs @@ -4,21 +4,16 @@ using MQTTnet.Core.Protocol; namespace MQTTnet.Core.Server { - public class MqttServerOptions + public sealed class MqttServerOptions { - public int Port { get; set; } = 1883; + public DefaultEndpointOptions DefaultEndpointOptions { get; } = new DefaultEndpointOptions(); + public SslEndpointOptions SslEndpointOptions { get; } = new SslEndpointOptions(); + public int ConnectionBacklog { get; set; } = 10; public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(10); public Func ConnectionValidator { get; set; } - - public bool UseSSL = false; - - /// - /// The path to the X509 SSL certificate. - /// - public string CertificatePath = string.Empty; } } diff --git a/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs b/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs new file mode 100644 index 00000000..cad2a26f --- /dev/null +++ b/MQTTnet.Core/Server/MqttServerOptionsExtensions.cs @@ -0,0 +1,31 @@ +using System; + +namespace MQTTnet.Core.Server +{ + public static class MqttServerOptionsExtensions + { + public static int GetSslEndpointPort(this MqttServerOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + + if (!options.SslEndpointOptions.Port.HasValue) + { + return 8883; + } + + return options.SslEndpointOptions.Port.Value; + } + + public static int GetDefaultEndpointPort(this MqttServerOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + + if (!options.DefaultEndpointOptions.Port.HasValue) + { + return 1883; + } + + return options.DefaultEndpointOptions.Port.Value; + } + } +} diff --git a/MQTTnet.Core/Server/SslEndpointOptions.cs b/MQTTnet.Core/Server/SslEndpointOptions.cs new file mode 100644 index 00000000..5a6a46d0 --- /dev/null +++ b/MQTTnet.Core/Server/SslEndpointOptions.cs @@ -0,0 +1,11 @@ +namespace MQTTnet.Core.Server +{ + public sealed class SslEndpointOptions + { + public bool IsEnabled { get; set; } + + public int? Port { get; set; } + + public byte[] Certificate { get; set; } + } +} diff --git a/MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs b/MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs new file mode 100644 index 00000000..38393343 --- /dev/null +++ b/MQTTnet.Core/Server/SslEndpointOptionsExtensions.cs @@ -0,0 +1,19 @@ +using System; + +namespace MQTTnet.Core.Server +{ + public static class SslEndpointOptionsExtensions + { + public static int GetPort(this DefaultEndpointOptions options) + { + if (options == null) throw new ArgumentNullException(nameof(options)); + + if (!options.Port.HasValue) + { + return 1883; + } + + return options.Port.Value; + } + } +} diff --git a/MQTTnet.nuspec b/MQTTnet.nuspec index 9e71785e..d373465a 100644 --- a/MQTTnet.nuspec +++ b/MQTTnet.nuspec @@ -11,7 +11,9 @@ false MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). * [Core] Any exception while accessing the underlying data source is now rethrown as "MqttCommunicationException". -* [Core] Extended exception information when an invalid packet type is received. +* [Core] Extended exception information when an invalid packet type is received. +* [Server] Added SSL (TLS1.2) support (thanks to Zazzmatazz) +* [Client] Added SSL (TLS1.2) support (thanks to Zazzmatazz) Copyright Christian Kratky 2016-2017 MQTT MQTTClient MQTTServer MQTTBroker Broker diff --git a/MQTTnet.sln b/MQTTnet.sln index b7e540a7..1053400e 100644 --- a/MQTTnet.sln +++ b/MQTTnet.sln @@ -1,10 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.26430.12 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestMqttClient", "Tests\MQTTnet.TestMqttClient\MQTTnet.TestMqttClient.csproj", "{7B19B139-2E9D-4F1D-88B4-6180B4CF872A}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.Core.Tests", "Tests\MQTTnet.Core.Tests\MQTTnet.Core.Tests.csproj", "{A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.NetFramework", "Frameworks\MQTTnet.NetFramework\MQTTnet.NetFramework.csproj", "{A480EF90-0EAA-4D9A-B271-47A9C47F6F7D}" @@ -15,8 +13,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{9248C2E1 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Frameworks", "Frameworks", "{32A630A7-2598-41D7-B625-204CD906F5FB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestMqttServer", "Tests\MQTTnet.TestMqttServer\MQTTnet.TestMqttServer.csproj", "{6F8C0C0C-59EC-4921-9267-370AE113C34F}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.Core", "MQTTnet.Core\MQTTnet.Core.csproj", "{2ECB99E4-72D0-4C23-99BA-93D511D3967D}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{002203AF-2565-4C0D-95ED-027FDEFE0C35}" @@ -26,6 +22,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MQTTnet.NetCoreApp", "Frameworks\MQTTnet.NetCoreApp\MQTTnet.NetCoreApp.csproj", "{1A1B7F51-5328-4395-9D9C-07D70965825E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestApp.NetFramework", "Tests\MQTTnet.TestApp.NetFramework\MQTTnet.TestApp.NetFramework.csproj", "{D9D74F33-6943-49B2-B765-7BD589082098}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MQTTnet.TestApp.UniversalWindows", "Tests\MQTTnet.TestApp.UniversalWindows\MQTTnet.TestApp.UniversalWindows.csproj", "{FF1F72D6-9524-4422-9497-3CC0002216ED}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Any CPU|Any CPU = Any CPU|Any CPU @@ -42,30 +42,6 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|Any CPU.ActiveCfg = Any CPU|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|Any CPU.Build.0 = Any CPU|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|ARM.ActiveCfg = Any CPU|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|ARM.Build.0 = Any CPU|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|x64.ActiveCfg = Any CPU|x64 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|x64.Build.0 = Any CPU|x64 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|x86.ActiveCfg = Any CPU|x86 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Any CPU|x86.Build.0 = Any CPU|x86 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|ARM.ActiveCfg = Debug|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|ARM.Build.0 = Debug|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|x64.ActiveCfg = Debug|x64 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|x64.Build.0 = Debug|x64 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|x86.ActiveCfg = Debug|x86 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Debug|x86.Build.0 = Debug|x86 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|Any CPU.Build.0 = Release|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|ARM.ActiveCfg = Release|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|ARM.Build.0 = Release|Any CPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|x64.ActiveCfg = Release|x64 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|x64.Build.0 = Release|x64 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|x86.ActiveCfg = Release|x86 - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A}.Release|x86.Build.0 = Release|x86 {A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC}.Any CPU|Any CPU.ActiveCfg = Any CPU|Any CPU {A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC}.Any CPU|Any CPU.Build.0 = Any CPU|Any CPU {A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC}.Any CPU|ARM.ActiveCfg = Any CPU|Any CPU @@ -138,30 +114,6 @@ Global {BD60C727-D8E8-40C3-B8E3-C95A864AE611}.Release|x64.Build.0 = Release|x64 {BD60C727-D8E8-40C3-B8E3-C95A864AE611}.Release|x86.ActiveCfg = Release|x86 {BD60C727-D8E8-40C3-B8E3-C95A864AE611}.Release|x86.Build.0 = Release|x86 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|Any CPU.ActiveCfg = Any CPU|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|Any CPU.Build.0 = Any CPU|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|ARM.ActiveCfg = Any CPU|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|ARM.Build.0 = Any CPU|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|x64.ActiveCfg = Any CPU|x64 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|x64.Build.0 = Any CPU|x64 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|x86.ActiveCfg = Any CPU|x86 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Any CPU|x86.Build.0 = Any CPU|x86 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|ARM.ActiveCfg = Debug|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|ARM.Build.0 = Debug|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|x64.ActiveCfg = Debug|x64 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|x64.Build.0 = Debug|x64 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|x86.ActiveCfg = Debug|x86 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Debug|x86.Build.0 = Debug|x86 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|Any CPU.Build.0 = Release|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|ARM.ActiveCfg = Release|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|ARM.Build.0 = Release|Any CPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|x64.ActiveCfg = Release|x64 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|x64.Build.0 = Release|x64 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|x86.ActiveCfg = Release|x86 - {6F8C0C0C-59EC-4921-9267-370AE113C34F}.Release|x86.Build.0 = Release|x86 {2ECB99E4-72D0-4C23-99BA-93D511D3967D}.Any CPU|Any CPU.ActiveCfg = Any CPU|Any CPU {2ECB99E4-72D0-4C23-99BA-93D511D3967D}.Any CPU|Any CPU.Build.0 = Any CPU|Any CPU {2ECB99E4-72D0-4C23-99BA-93D511D3967D}.Any CPU|ARM.ActiveCfg = Any CPU|Any CPU @@ -210,16 +162,72 @@ Global {1A1B7F51-5328-4395-9D9C-07D70965825E}.Release|x64.Build.0 = Release|x64 {1A1B7F51-5328-4395-9D9C-07D70965825E}.Release|x86.ActiveCfg = Release|x86 {1A1B7F51-5328-4395-9D9C-07D70965825E}.Release|x86.Build.0 = Release|x86 + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|Any CPU.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|Any CPU.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|ARM.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|ARM.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|x64.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|x64.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|x86.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Any CPU|x86.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|ARM.ActiveCfg = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|ARM.Build.0 = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|x64.ActiveCfg = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|x64.Build.0 = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|x86.ActiveCfg = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Debug|x86.Build.0 = Debug|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|Any CPU.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|ARM.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|ARM.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|x64.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|x64.Build.0 = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|x86.ActiveCfg = Release|Any CPU + {D9D74F33-6943-49B2-B765-7BD589082098}.Release|x86.Build.0 = Release|Any CPU + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|Any CPU.ActiveCfg = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|Any CPU.Build.0 = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|Any CPU.Deploy.0 = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|ARM.ActiveCfg = Release|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|ARM.Build.0 = Release|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|ARM.Deploy.0 = Release|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|x64.ActiveCfg = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|x64.Build.0 = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|x64.Deploy.0 = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|x86.ActiveCfg = Release|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|x86.Build.0 = Release|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Any CPU|x86.Deploy.0 = Release|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|Any CPU.ActiveCfg = Debug|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|ARM.ActiveCfg = Debug|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|ARM.Build.0 = Debug|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|ARM.Deploy.0 = Debug|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|x64.ActiveCfg = Debug|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|x64.Build.0 = Debug|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|x64.Deploy.0 = Debug|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|x86.ActiveCfg = Debug|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|x86.Build.0 = Debug|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Debug|x86.Deploy.0 = Debug|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|Any CPU.ActiveCfg = Release|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|ARM.ActiveCfg = Release|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|ARM.Build.0 = Release|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|ARM.Deploy.0 = Release|ARM + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|x64.ActiveCfg = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|x64.Build.0 = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|x64.Deploy.0 = Release|x64 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|x86.ActiveCfg = Release|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|x86.Build.0 = Release|x86 + {FF1F72D6-9524-4422-9497-3CC0002216ED}.Release|x86.Deploy.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} {A7FF0C91-25DE-4BA6-B39E-F54E8DADF1CC} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} {A480EF90-0EAA-4D9A-B271-47A9C47F6F7D} = {32A630A7-2598-41D7-B625-204CD906F5FB} {BD60C727-D8E8-40C3-B8E3-C95A864AE611} = {32A630A7-2598-41D7-B625-204CD906F5FB} - {6F8C0C0C-59EC-4921-9267-370AE113C34F} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} {1A1B7F51-5328-4395-9D9C-07D70965825E} = {32A630A7-2598-41D7-B625-204CD906F5FB} + {D9D74F33-6943-49B2-B765-7BD589082098} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} + {FF1F72D6-9524-4422-9497-3CC0002216ED} = {9248C2E1-B9D6-40BF-81EC-86004D7765B4} EndGlobalSection EndGlobal diff --git a/README.md b/README.md index 6ef53984..b44ec2f4 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,19 @@ # MQTTnet MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server. The implementation is based on the documentation from http://mqtt.org/. -## Supported frameworks +## Features +* MQTT client included +* MQTT server (broker) included +* SSL (TLS 1.2) support for Client and Server +* Async support +* List of connected clients available (server only) +* Extensible communication channels (i.e. In-Memory, TCP, TCP+SSL, WebSockets (not included in this project)) +* Access to internal trace messages +* Extensible client credential validation (server only) +* Unit tested (48+ tests) +* Lightweight (only the low level implementation of MQTT) +## Supported frameworks * .NET Standard 1.1+ * .NET Core 1.1+ * .NET Core App 1.1+ @@ -16,7 +27,6 @@ MQTTnet is a .NET library for MQTT based communication. It provides a MQTT clien * Universal Windows (UWP) 10.0.10240+ (x86, x64, ARM, AnyCPU) ## Supported MQTT versions - * 3.1.1 ## Nuget diff --git a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs index 00fbfb22..1ca8f82a 100644 --- a/Tests/MQTTnet.Core.Tests/MqttServerTests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttServerTests.cs @@ -1,5 +1,7 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Core.Adapter; using MQTTnet.Core.Client; using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; @@ -46,11 +48,11 @@ namespace MQTTnet.Core.Tests [TestMethod] public async Task MqttServer_WillMessage() { - var s = new MqttServer(new MqttServerOptions(), new TestMqttServerAdapter()); + var s = new MqttServer(new MqttServerOptions(), new List { new TestMqttServerAdapter() }); s.Start(); var willMessage = new MqttApplicationMessage("My/last/will", new byte[0], MqttQualityOfServiceLevel.AtMostOnce, false); - var c1 = ConnectTestClient("c1", null, s); + var c1 = ConnectTestClient("c1", null, s); var c2 = ConnectTestClient("c2", willMessage, s); var receivedMessagesCount = 0; @@ -86,7 +88,7 @@ namespace MQTTnet.Core.Tests MqttQualityOfServiceLevel filterQualityOfServiceLevel, int expectedReceivedMessagesCount) { - var s = new MqttServer(new MqttServerOptions(), new TestMqttServerAdapter()); + var s = new MqttServer(new MqttServerOptions(), new List { new TestMqttServerAdapter() }); s.Start(); var c1 = ConnectTestClient("c1", null, s); diff --git a/Tests/MQTTnet.TestMqttServer/App.config b/Tests/MQTTnet.TestApp.NetFramework/App.config similarity index 100% rename from Tests/MQTTnet.TestMqttServer/App.config rename to Tests/MQTTnet.TestApp.NetFramework/App.config diff --git a/Tests/MQTTnet.TestMqttServer/MQTTnet.TestMqttServer.csproj b/Tests/MQTTnet.TestApp.NetFramework/MQTTnet.TestApp.NetFramework.csproj similarity index 56% rename from Tests/MQTTnet.TestMqttServer/MQTTnet.TestMqttServer.csproj rename to Tests/MQTTnet.TestApp.NetFramework/MQTTnet.TestApp.NetFramework.csproj index d4cc365c..5c91b3ac 100644 --- a/Tests/MQTTnet.TestMqttServer/MQTTnet.TestMqttServer.csproj +++ b/Tests/MQTTnet.TestApp.NetFramework/MQTTnet.TestApp.NetFramework.csproj @@ -4,10 +4,10 @@ Debug AnyCPU - {6F8C0C0C-59EC-4921-9267-370AE113C34F} + {D9D74F33-6943-49B2-B765-7BD589082098} Exe - MQTTnet.TestMqttServer - MQTTnet.TestMqttServer + MQTTnet.TestApp.NetFramework + MQTTnet.TestApp.NetFramework v4.5.2 512 true @@ -31,43 +31,15 @@ prompt 4 - - bin\Any CPU\ - TRACE - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - true - - - x86 - bin\x86\Debug\ - - - x86 - bin\x86\Release\ - - - x86 - bin\x86\Any CPU\ - - - x64 - bin\x64\Debug\ - - - x64 - bin\x64\Release\ - - - x64 - bin\x64\Any CPU\ - + + + + + + diff --git a/Tests/MQTTnet.TestMqttClient/Program.cs b/Tests/MQTTnet.TestApp.NetFramework/Program.cs similarity index 58% rename from Tests/MQTTnet.TestMqttClient/Program.cs rename to Tests/MQTTnet.TestApp.NetFramework/Program.cs index 41d547a0..6ec31cdb 100644 --- a/Tests/MQTTnet.TestMqttClient/Program.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading; using System.Threading.Tasks; using MQTTnet.Core; using MQTTnet.Core.Client; @@ -9,16 +10,29 @@ using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using MQTTnet.Core.Server; -namespace MQTTnet.TestMqttClient +namespace MQTTnet.TestApp.NetFramework { public static class Program { - public static void Main(string[] arguments) + public static void Main(string[] args) { - Task.Run(() => Run(arguments)).Wait(); + Console.WriteLine("MQTTnet - TestApp.NetFramework"); + Console.WriteLine("1 = Start client"); + Console.WriteLine("2 = Start server"); + var pressedKey = Console.ReadKey(true); + if (pressedKey.Key == ConsoleKey.D1) + { + Task.Run(() => RunClientAsync(args)); + Thread.Sleep(Timeout.Infinite); + } + else if (pressedKey.Key == ConsoleKey.D2) + { + Task.Run(() => RunServerAsync(args)); + Thread.Sleep(Timeout.Infinite); + } } - private static async Task Run(string[] arguments) + private static async Task RunClientAsync(string[] arguments) { MqttTrace.TraceMessagePublished += (s, e) => { @@ -28,7 +42,7 @@ namespace MQTTnet.TestMqttClient Console.WriteLine(e.Exception); } }; - + try { var options = new MqttClientOptions @@ -61,7 +75,7 @@ namespace MQTTnet.TestMqttClient Console.WriteLine("### SUBSCRIBED ###"); }; - client.Disconnected += async (s, e) => + client.Disconnected += async (s, e) => { Console.WriteLine("### DISCONNECTED FROM SERVER ###"); await Task.Delay(TimeSpan.FromSeconds(5)); @@ -80,9 +94,9 @@ namespace MQTTnet.TestMqttClient { await client.ConnectAsync(); } - catch + catch (Exception exception) { - Console.WriteLine("### CONNECTING FAILED ###"); + Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception); } Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###"); @@ -106,5 +120,50 @@ namespace MQTTnet.TestMqttClient Console.WriteLine(exception); } } + + private static async Task RunServerAsync(string[] arguments) + { + MqttTrace.TraceMessagePublished += (s, e) => + { + Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}"); + if (e.Exception != null) + { + Console.WriteLine(e.Exception); + } + }; + + try + { + var options = new MqttServerOptions + { + ConnectionValidator = p => + { + if (p.ClientId == "SpecialClient") + { + if (p.Username != "USER" || p.Password != "PASS") + { + return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; + } + } + + return MqttConnectReturnCode.ConnectionAccepted; + } + }; + + var mqttServer = new MqttServerFactory().CreateMqttServer(options); + mqttServer.Start(); + + Console.WriteLine("Press any key to exit."); + Console.ReadLine(); + + mqttServer.Stop(); + } + catch (Exception e) + { + Console.WriteLine(e); + } + + Console.ReadLine(); + } } } diff --git a/Tests/MQTTnet.TestMqttClient/Properties/AssemblyInfo.cs b/Tests/MQTTnet.TestApp.NetFramework/Properties/AssemblyInfo.cs similarity index 83% rename from Tests/MQTTnet.TestMqttClient/Properties/AssemblyInfo.cs rename to Tests/MQTTnet.TestApp.NetFramework/Properties/AssemblyInfo.cs index f90b75ac..5fb54951 100644 --- a/Tests/MQTTnet.TestMqttClient/Properties/AssemblyInfo.cs +++ b/Tests/MQTTnet.TestApp.NetFramework/Properties/AssemblyInfo.cs @@ -1,7 +1,7 @@ using System.Reflection; using System.Runtime.InteropServices; -[assembly: AssemblyTitle("MQTTnet.TestMqttClient")] +[assembly: AssemblyTitle("MQTTnet.TestApp.NetFramework")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Christian Kratky")] diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml b/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml new file mode 100644 index 00000000..5f70fb29 --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml @@ -0,0 +1,8 @@ + + + diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml.cs new file mode 100644 index 00000000..de5a9f87 --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/App.xaml.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.ApplicationModel; +using Windows.ApplicationModel.Activation; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +namespace MQTTnet.TestApp.UniversalWindows +{ + /// + /// Provides application-specific behavior to supplement the default Application class. + /// + sealed partial class App : Application + { + /// + /// Initializes the singleton application object. This is the first line of authored code + /// executed, and as such is the logical equivalent of main() or WinMain(). + /// + public App() + { + this.InitializeComponent(); + this.Suspending += OnSuspending; + } + + /// + /// Invoked when the application is launched normally by the end user. Other entry points + /// will be used such as when the application is launched to open a specific file. + /// + /// Details about the launch request and process. + protected override void OnLaunched(LaunchActivatedEventArgs e) + { + Frame rootFrame = Window.Current.Content as Frame; + + // Do not repeat app initialization when the Window already has content, + // just ensure that the window is active + if (rootFrame == null) + { + // Create a Frame to act as the navigation context and navigate to the first page + rootFrame = new Frame(); + + rootFrame.NavigationFailed += OnNavigationFailed; + + if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) + { + //TODO: Load state from previously suspended application + } + + // Place the frame in the current Window + Window.Current.Content = rootFrame; + } + + if (e.PrelaunchActivated == false) + { + if (rootFrame.Content == null) + { + // When the navigation stack isn't restored navigate to the first page, + // configuring the new page by passing required information as a navigation + // parameter + rootFrame.Navigate(typeof(MainPage), e.Arguments); + } + // Ensure the current window is active + Window.Current.Activate(); + } + } + + /// + /// Invoked when Navigation to a certain page fails + /// + /// The Frame which failed navigation + /// Details about the navigation failure + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) + { + throw new Exception("Failed to load Page " + e.SourcePageType.FullName); + } + + /// + /// Invoked when application execution is being suspended. Application state is saved + /// without knowing whether the application will be terminated or resumed with the contents + /// of memory still intact. + /// + /// The source of the suspend request. + /// Details about the suspend request. + private void OnSuspending(object sender, SuspendingEventArgs e) + { + var deferral = e.SuspendingOperation.GetDeferral(); + //TODO: Save application state and stop any background activity + deferral.Complete(); + } + } +} diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/LockScreenLogo.scale-200.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/LockScreenLogo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..735f57adb5dfc01886d137b4e493d7e97cf13af3 GIT binary patch literal 1430 zcmaJ>TTC2P7~aKltDttVHYH6u8Io4i*}3fO&d$gd*bA_<3j~&e7%8(eXJLfhS!M@! zKrliY>>6yT4+Kr95$!DoD(Qn-5TP|{V_KS`k~E6(LGS@#`v$hQo&^^BKsw3HIsZBT z_y6C2n`lK@apunKojRQ^(_P}Mgewt$(^BBKCTZ;*xa?J3wQ7~@S0lUvbcLeq1Bg4o zH-bvQi|wt~L7q$~a-gDFP!{&TQfc3fX*6=uHv* zT&1&U(-)L%Xp^djI2?~eBF2cxC@YOP$+9d?P&h?lPy-9M2UT9fg5jKm1t$m#iWE{M zIf%q9@;fyT?0UP>tcw-bLkz;s2LlKl2qeP0w zECS7Ate+Awk|KQ+DOk;fl}Xsy4o^CY=pwq%QAAKKl628_yNPsK>?A>%D8fQG6IgdJ ztnxttBz#NI_a@fk7SU`WtrpsfZsNs9^0(2a z@C3#YO3>k~w7?2hipBf{#b6`}Xw1hlG$yi?;1dDs7k~xDAw@jiI*+tc;t2Lflg&bM)0!Y;0_@=w%`LW^8DsYpS#-bLOklX9r?Ei}TScw|4DbpW%+7 zFgAI)f51s}{y-eWb|vrU-Ya!GuYKP)J7z#*V_k^Xo>4!1Yqj*m)x&0L^tg3GJbVAJ zJ-Pl$R=NAabouV=^z_t;^K*0AvFs!vYU>_<|I^#c?>>CR<(T?=%{;U=aI*SbZADLH z&(f2wz_Y0??Tf|g;?|1Znw6}6U43Q#qNRwv1vp9uFn1)V#*4p&%$mP9x&15^OaBiDS(XppT|z^>;B{PLVEbS3IFYV yGvCsSX*m literal 0 HcmV?d00001 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/SplashScreen.scale-200.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/SplashScreen.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..023e7f1feda78d5100569825acedfd213a0d84e9 GIT binary patch literal 7700 zcmeHLYj~4Yw%(;oxoEH#Kxq-eR|+VkP17b#Vk;?4QwkI+A{L04G+#<<(x#Un1#+h5>eArRq zTw$)ZvTWW_Y?bDho0nPVTh08+s`sp!j74rJTTtXIDww0SILedFv?sZ?yb@@}GN;#8 znk_b~Q(A0YR#uV4ef!osoV1M3;vQ8N$O|fStfgf$S5;ddUNv`tWtGjM;koG#N;7M< zP*84lnx(bn_KF&9Z5Ai$)#Cs3a|$OFw>WKCT$of*L7_CqQEinflT|W{JT+aKp-E0v zsxmYg)1(T>DROm+LN1eQw8}KCTp=C!$H7`PU!t9_Hw@TsTI2`udRZv*!a5`#A9hK6Y95L(CDUX&_@QxKV z_feX{UhA#ZWlvgpL$#w^D#lq`_A4AzDqd|Zv6y9PX&DNcN|l}_D^{q@GG&H^Pg583 z8FI6N8^H7b5WjGp;urW)d7F+_lcp%KsLX0viCmE(OHH+=%ZfD_=`voUuoUxFO^L;- z;!;2{g-YiiO6m4bs89OuF9!p{FGtH-f%8<2gY!h9s)4ciN%{Kh1+`}{^}M~+TDH9N z^Z5PlgVXMC&2&k*Hw^Lb9gny#ro$MOIxIt{+r)EA10$VR3 zanN8D{TUkl+v0CQ_>ZoHP<M-x#8@8ZiT#$Kh`(uRaX1g$Bg|qy$<#7 zSSAi{Nb8Y=lvNVeio+UGLCAtoLBfL`iOv`)yoJMDJBN>4IH@(l7YRF;61@>qq1iM9 zr@b#OC~SAxSle?5Pp8Z78{VO0YFr1x7kZU64Z23eLf2T2#6J_t;-E}DkB?NufZ0Ug zi?J&byXeaB-uTNVhuiM!UVQw}bZrJ3GtAETYp->!{q#zfN7D3AS9@Q7*V^85jGx#R z(QxYV(wW#F0XF9^^s>>H8pPlVJ>)3Oz z&_X8Sf@~?cH_O*cgi$U#`v`RRfv#y3m(ZpKk^5uLup+lVs$~}FZU$r_+}#hl%?g5m z-u-}-666ssp-xWQak~>PPy$mRc|~?pVSs1_@mBEXpPVfLF6(Ktf1S* zPPh@QZ=tFMs?LM2(5P3L2;l_6XX6s&cYsP1ip#eg0`ZEP0HGYh{UmS@o`MihLLvkU zgyAG0G`b1|qjxxh1(ODKFE%AP}Dq=3vK$P7TXP4GrM1kQ72!GUVMDl`rDC&2;TA}*nF z8$nQD&6ys_nc1*E7$*1S@R8$ymy(sQV}imGSedB@{!QR5P&N_H=-^o!?LsWs+2|mH z-e=)T^SvI)=_JIm7}j4;@*Z17=(#}m=~YF~z~CLI+vdAGlJDcdF$TM?CVI1%LhUrN zaa6DJ=Yh$)$k&Oz{-~8yw^GM^8prYxSxo zvI4k#ibryMa%%*8oI-5m61Koa_A_xg=(fwp0aBX{;X4Q;NXUhtaoJDo1>TqhWtn=_ zd5~chq#&6~c%8JZK#t_&J(9EVUU&upYeIovLt1>vaHe}UUq>#RGQj!EN#5+0@T`(@ z^g~>*c`VGRiSt;!$_4+0hk^I!@O3``5=sZ8IwlxWW7km1B&_t&E*u0_9UBa#VqwY* zz>nxv?FAsVnRaD(Bui=6i==BFUw0k4n$>`umU`F2l?7CYTD^)c2X+d9X&ddS9|gj? zM?knGkGCX&W8offw8aLC2$D{PjC3nVZwd4k?eZH8*mZ)U@3Qk8RDFOz_#WUA#vnzy zyP>KrCfKwSXea7}jgJjBc}PGY+4#6%lbZyjhy`5sZd_Vy6Wz;ixa?czkN}J9It1K6 zY!eu>|AwF^fwZlLAYyQI*lM@^>O>Iu6Vf6i>Q$?v!SeUS<{>UYMwz$*%Aq?w^`j{h z!$GZbhu=^D{&ET8;))LL%ZBDZkQqRd2;u~!d9bHGmLRhLDctNgYyjsuvoSZ#iVdoB z2!f--UUA#U;<{je#?cYt^{PIyKa%hW>}uepWMyAI{{Zo7?2>?$c9;whJae%oN|I-kpTQSx_C$Z&;f zi2i)qmEn=y4U0uvk)$m;zKfjPK@oc?I`}1Jzl$Q~aoKBd3kt7L#7gyt|A_qgz6ai< z=X%D1i!d2h?rHR^R8SUj&G||dkC?DT>{o#Yau<@uqVT{Xef&XG}5*E4aPk{}~ zplx&XhaV)&1EfI3Em;Bw#O5SV^c;{twb-1Rw)+=0!e_BLbd7tYmXCH0wrlOSS+~`7He8Iqx0{CN+DVit9;*6L~JAN zD&cyT)2?h}xnYmL?^)<7YyzZ3$FHU^Eg;DLqAV{#wv#Wj7S`Jdl1pX&{3(uZ?!uh} zDc$ZTNV*7le_W6}Hju~GMTxZQ1aWCeUc%!jv3MHAzt>Y-nQK%zfT*3ebDQA5b?iGn; zBjv3B+GhLTexd_(CzZDP4|#n5^~scvB6#Pk%Ho!kQ>yYw((Dv{6=$g3jT1!u6gORW zx5#`7Wy-ZHRa~IxGHdrp(bm%lf>2%J660nj$fCqN(epv@y!l9s7@k6EvxS{AMP>WY zX4$@F8^kayphIx-RGO$+LYl9YdoI5d|4#q9##`_F5Xnx`&GPzp2fB{-{P@ATw=X@~ z_|&^UMWAKD;jjBKTK(~o?cUFRK8EX=6>cXpfzg4ZpMB>*w_^8GSiT-Jp|xBOnzM+j z*09-@-~qJ(eqWq5@R4i^u4^{McCP(!3}C|v_WsTR*bIUxN(Nx`u##3B4{sE`Z`v8w zAwIG`?1~PkID~W{uDzmqH98Pew_1(;x2%8r^vY{)_&J2K)cN{W+h5+g)ZcjP&Ci#O zgy|8K@4kyMfwilHd&6TDlhb%++Pk!>9HRld6HT7gwyZGrxS$}CsD6`>6!!2K1@Mjf z(P0WYB7V_OFZyeWrbOFb>O54BNXf~K&?}3=^v;v_wT{DKr?jN^DtN&DXwX%u?s*c6`%8>WFz z7}YW^tp0bp^NriE)AB6M2l<7rn7fzePtR*omOevpfm9n?}2V*+0iW;S)C zhg`NAjL?D=W#k*$aR{>pGf~lD-rVtD;5jW1_*Jn1j1=es@Kcx4ySM_bwcQCT=d+DV z>Sz~L=Hj@(X%31nK$mWI@7d>}ORB`K(p=+`UD)+99YUGQc7y^bHZ1F(8|tL0 zdK*DT0kSXG_{BKTpP2*2PecdKV9;dq$^ZZDP;Nyq1kp-&GI5eAyZsK!e3V zK@rPy*{(`KIfo+lc878mDKk^V#`VT05}64kBtk%DgwLrOvLMj5-;*GNKv6c6pzMuL z6EP%ob|_0IW}lLRXCP2!9wWhEw3LA7iF#1O1mIZ@Z=6&bz41F;@S_GvYAG-#CW3z{ zP3+6vHhvP&A3$##Vo9$dT^#MoGg^|MDm=Bt1d2RRwSZ<;ZHICpLBv5Xs!D?BH^(9_ z7`H=N&^v|Z-%mP}wNzG{aiFCsRgwzwq!N6obW9+7(R; z(SZ=23`|`>qil!LMGG{_Heq!BD>(Y-zV9wD)}hz25JA37YR%39;kI4y9pgtcUass6 zP24}ZY$vvYeI`zy&)A_X#nY3017ap*0&jx|mVwyGhg3;!keU53a}Uhm3BZI$N$6Se zLWlAmy1S0xKJm4G_U@sN_Tm=`$xWJSEwKU98rZ&)1R^*$$1vA3oG#&*%SMxY_~oGP zP&PFJatFLM-Ps%84IV-+Ow)T{C7cqUAvauy4C z(FRz&?6$Rypj{xO!`y=*J5o4@U8Q-(y5(*=YoKeZ+-1YdljXxkA#B)zo=FeQH#?Le zycNUmEEHWO9a=X^pb#&cOq7-`7UA87#|S22)<7RUtZo|(zibX=w;K3qur9vy#`MNV z6UUcf9ZwEnKCCp+OoBnF@OdbvH)ANXO0o~Pi9l8=x3))}L<#vO0-~O4!~--Ket?d} zJaqsj<@CD1%S2cTW%rOP{Vto%0sGW~1RMa_j^)5nil0Yw- z0EE#bP+l4#P^%PQ+N*oxu1Zq05xZ!bXfYTg>9c{(Iw*lnjR^>kz%lAN^zFce7rppy zY8zA~3GD=A6d*hze&l4D_wA~+O!56)BZTe_rEu}Ezi<4!kG|W#amBZ5{&XS2@6R~H z{9o^y*BkH4$~yX9U&@CgbOzX1bn9xqF|zh$Dh0Y5y*E0e90*$!ObrHY3Ok0`2=O~r zCuke6KrP9KOf?V(YDsM<6pX2nVoN%M$LT^q#FmtaF?1^27F*IcNX~XRB(|hCFvdcc zc)$=S-)acdk$g4?_>jRqxpI6M3vHZk?0c^3=byamYDNf;uB{3NlKW5IhnOS3DNkMV z?tK8?kJ}pmvp%&&eTVOVjHP`q34hN1@!aK}H(K!vI`~gf|Gv+FNEQD5Yd<~yX7k_l h&G-K)@HZb3BABY{)U1?^%I#E6`MGoTtustd{~yM6srvu` literal 0 HcmV?d00001 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square150x150Logo.scale-200.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square150x150Logo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..af49fec1a5484db1d52a7f9b5ec90a27c7030186 GIT binary patch literal 2937 zcma)84OCO-8BSud5)jwMLRVKgX(S?$n?Ld|vrsm<$CF7)&zTbyy1FE5bU`Q17MRv`9ue$;R(@8kR;#vJ*IM0>cJIAOte!d7oRgdH zd%ySjdB6L9=gX^A6)VzH7p2l@v~3zJAMw|DFy#^)F@@F*`mqUn=Il>l)8_+ab;nOW{%+iPx z+s{Eu|&pIs)Z7{La9~?xKfyl z#43?gjEL15d4WbOZo#SiP%>DB^+BcnJ=7dHEe;r#G=tuw|ka z%q@}##Uh7;tc%L_64m(kHtw74ty%BJMb)_1)#S0j`)F8_1jF7vScpsnH=0V19bO8y zR`0SjIdCUo&=>JwMQF8KHA<{ODHTiQh}0^@5QRmCA?gOH6_H3K^-_sNB^RrdNuK-R zOO*vOrKCVvDwgUck`kF(E7j{I#iiN;b*ZdCt4m@HPA`EuEqGGf4%!K<;(=I=&Vyrw z%TwcWtxa}8mCZ%Cyf&ActJ6_$ox5z6-D!0-dvnRx6t7y3d+h6QYpKWO;8OdnvERo7 zuEf>ih5`wqY)~o@OeVt-wM?Q!>QzdGRj!bz6fzYrfw$hZfAKzr2-M+D+R>}~oT574c;_3zquHcElqKIsryILt3g8n3jcMb+j?i?-L3FpZJ z2WRVBRdDPc+G5aaYg#5hpE+6nQ|(VSoxT3|biF;BUq#==-27Xi=gihDPYP$7?=9cP zYKE$jeQ|3~_L0VG-(F~2ZPyD0=k{J4Q~h(t__{-mz_w8{JDY9{`1ouzz!Vr5!ECdE z6U~O1k8c}24V7~zzXWTV-Pe4)y}wQJS&q%H5`Fo_f_JvIU489aCX$;P`u#!I-=^4ijC2{&9!O&h>mi?9oYD=GC#%)6{GzN6nQYw+Fal50!#x^asjBBR50i`+mho*ttoqV)ubM2KD9S~k7+FR4>{29?6 z{!l6kDdyTN0YJ9LgkPWeXm|gyi@zM3?0@{&pXT12w|78&W-q!RRF)&iLCEZVH<|fR zN0fr2^t8H(>L?>K#>^+jWROLral(Qy-xoBq1U7A&DV||wClb)Otd9?(gZ|8znMF}D zf<1haWz^s0qgecz;RFGt0C-B4g`jNGHsFU+;{<%t65v^sjk^h$lmWn#B0#_)9ij&d z-~lc`A)YYExi^7sBuPM^Y|wA2g*5?`K?#7tzELQYNxGo$UB$4J8RJp1k(8Jj+~hMT zlN~>M@KTTh^--8y3PK_NZ@AC!{PT=CziBzGd+wTJ^@icH!Bd}%)g8V)%K?|c&WTUk zy}qv1C%(fjRoZ4ozC3{O%@5?)XzH35zHns$pgU*Q?fj4v?fp1Qbm+j;3l;9jam9Da zXVcKjPlQ73x78QPu|Ffm6x?`~e3oD=gl=4kYK?={kD5j~QCXU)`HSdduNNENzA*2$ zOm3PzF!lN5e*06-f1Uot67wY#{o-S1!KZ7E=!~7ynnk9_iJR#kFoNbAOT#^2Gd17F zMmvU6>lndZQGd|ax9kUoXXO+$N?|j@6qpsF&_j7YXvwo_C{JpmLw5&#e6k>atv%es z5)7r*Wvv_JkUpT}M!_o!nVlEk1Zbl=a*2hQ*<|%*K1Glj^FcF`6kTzGQ3lz~2tCc@ z&x|tj;aH&1&9HwcJBcT`;{?a+pnej;M1HO(6Z{#J!cZA04hnFl;NXA+&`=7bjW_^o zfC40u3LMG?NdPtwGl>Tq6u}*QG)}-y;)lu-_>ee3kibW(69n0$0Zy!}9rQz%*v1iO zT9_H>99yIrSPYVy6^);rR}7Yo=J_T@hi+qhTZXnVWyf;JDYm5#eYLTxr*?kiNn!+Y zQ+LUkBafNJ#rH#C(?d5^;gw9o#%daEI{mA*LHPIHPU`#|H$hD zwm>0&+kahQ)E#%~k>&5@&#Vg82H?s%71=)(soi@174pi9--2{w{1$}Sz4zGn3Du&x bht0Iza^2ykEt4(epJ78uh5nDlX8(TxzDYwP literal 0 HcmV?d00001 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.scale-200.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..ce342a2ec8a61291ba76c54604aea7e9d20af11b GIT binary patch literal 1647 zcmaJ?eM}Q)7(e+G1Q(|`V9JhTI2>MkceK4;p;PR&$Pi?ejk3YQ_3o`S&|W_dsOZ8# zWPTt69g`t$ab`0cj-Y0yiBSOqmd)tG7G(}M5aP0_%&9TijB#&)I{zSE^4@#z^FF`l z`8{8`o%wlL(UI|y2!cdsuVamHH~H86F!*-15em4)NqUpCQM5?aoC_eCf@lV4wvF2a zjDQn1JBL69f&@2M3rvzJcfE!eZ8FZUBlFlC5RD)it33{mF9#B82AiyQE%w)`vlwa> zv{<1sm&kSKK$&%2jSFn7$t&P%%6Ue>R=EAnG8N7fqynWG8L3p!4801a;8{+nliO(qd(jNJ_?+9W3#hLIDLoT6~3fx9=`CC-D}-AMrpEO7HK zt3$GicGPc?GmDjy7K2P@La;eu4!$zWCZ`ym{Z$b zu-O6RM&K4JT|BIZB`E-gxqG%FzanI#+2FFmqHqXG7yxWB=w55RGOM)$xMb(>kSNR z2w=1AZi%z=AmG~yea~XaXJR!v7vLn(RUnELfiB1|6D84ICOS}^Zo2AdN}<&*h}G_u z{xZ!(%>tLT3J3<5XhWy-tg+6)0nmUUENLW8TWA{R6bgVd3X;anYFZ^IRis*_P-C-r z;i>%1^eL3UI2-{w8nuFFcs0e~7J{O2k^~Ce%+Ly4U?|=!0LH=t6()xi<^I-rs+9sF z*q{E-CxZbGPeu#a;XJwE;9S1?#R&uns>^0G3p`hEUF*v`M?@h%T%J%RChmD|EVydq zmHWh*_=S%emRC*mhxaVLzT@>Z2SX0u9v*DIJ@WC^kLVdlGV6LpK$KIrlJqc zpJ921)+3JJdTx|<`G&kXpKkjGJv=76R`yYIQ{#c-`%+`#V(7}Q;&@6U8!Td1`d;?N z_9mnI#?AA}4J!r)LN4!E-@H5eXauuB7TOawS>Y|{-P?NNx-lq+z1W-+y(;39P&&LP zL{N80?&=C*qKmdA^moMZRuPcD!B<*mq$ch=0Cnlitw#txRWhb3%TQvPqjkC`F69G4b! ze7z9MZ#+;_#l?H37UqUhDFb^l&s2{oM$3I0o^Q!yx;;V)QmCMo)Tb_ui|mit8MS?U zm##6$sZZ1$@|s%?l@>4Z<*Q}sRBSKMhb4I{e5LdEhsHIHTe8Bod5c>6QtT>$XgUBz z6MK`kO$=jmt@FqggOhJ5j~e@ygRbG;<{Vu)*+nn9aQeo0;$#j;|MS=S$&L?BeV25z xs3B`@=#`5TF{^6(A1rvdY@|-RtQ|iS5{tyX+wH?;n8E)G$kykv-D^wh{{!TZT%7;_ literal 0 HcmV?d00001 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.targetsize-24_altform-unplated.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.targetsize-24_altform-unplated.png new file mode 100644 index 0000000000000000000000000000000000000000..f6c02ce97e0a802b85f6021e822c89f8bf57d5cd GIT binary patch literal 1255 zcmaJ>TWs4@7*5+{G#S+&C!qC#> zf>5N3P6jO*Cz>ug*(_DmW=)kea&m$gZ^+nyiF`;j%w@}y8)>p*SH}C`m?DXeieF2U zyQHecc_L%Gh!7GMt+hG06y;+|p4>m~}PjA}rKViGiEnn7G0ZO<>G|7q;2?NwGCM3s?eued6%hd$B+ z*kQJ{#~$S=DFE(%=E+UkmlEI*%3llUf~8Ja9YU1Vui0IbGBkW_gHB%Rd&!!ioX zs40O?i9I{};kle7GMvE7(rk`la=gTI)47=>%?q@^iL-nUo3}h4S}N-KHn8t5mVP8w z&bSErwp+37 zNJJ8?a|{r5Q3R0Z5s-LB1WHOwYC@7pCHWND#cL1cZ?{kJ368_*(UDWUDyb<}0y@o# zfMF016iMWPCb6obAxT$JlB6(2DrlXDTB&!0`!m??4F(qWMhjVZo?JXQmz`1*58Z=& zcDmB|S-E@j?BoFGix0flckqdS4jsPNzhfWyWIM98GxcLs89C(~dw%$_t;JjX-SD}E zfiGV;{8Q%8r}w9x>EEigW81>`kvnU@pK)4+xk9@+bNj9L!AAZ@SZ@q|)&BmY3+HZx zul~BeG4|}-;L%cHViQGQX?^zFfO0&#cHwel=d`lH9sJ-@Sl@n*(8J2>%Ac`IxyY?Q z{=GhWvC#gu-~Ia7*n{=+;qM?Ul_wy1+u7ho;=`>EwP^g~R@{unBds`!#@}tluZQpS zm)M~nYEifJWJGx?_6DcTy>#uh%>!H9=hb^(v`=m3F1{L>db=<5_tm+_&knAQ2EU$s Mu9UqpbNZeC0BbUo^Z)<= literal 0 HcmV?d00001 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/StoreLogo.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/StoreLogo.png new file mode 100644 index 0000000000000000000000000000000000000000..7385b56c0e4d3c6b0efe3324aa1194157d837826 GIT binary patch literal 1451 zcmaJ>eN5D57_Z|bH;{0+1#mbl)eTU3{h)Wf7EZV?;HD@XL@{B`Ui%(2aMxQ~xdXSv z5nzWi(LW)U2=Vc-cY@s7nPt{i0hc6!7xN4NNHI#EQl>YNBy8l4%x9gr_W-j zEZMQmmTIy(>;lblRfh`dIyTgc9W5d!VP$L4(kKrN1c5G~(O_#xG zAJCNTstD^5SeXFB+&$h=ToJP2H>xr$iqPs-#O*;4(!Fjw25-!gEb*)mU}=)J;Iu>w zxK(5XoD0wrPSKQ~rbL^Cw6O_03*l*}i=ydbu7adJ6y;%@tjFeXIXT+ms30pmbOP%Q zX}S;+LBh8Tea~TSkHzvX6$rYb)+n&{kSbIqh|c7hmlxmwSiq5iVhU#iEQ<>a18|O^Sln-8t&+t`*{qBWo5M?wFM(JuimAOb5!K#D}XbslM@#1ZVz_;!9U zpfEpLAOz=0g@bd6Xj_ILi-x^!M}73h^o@}hM$1jflTs|Yuj9AL@A3<-?MV4!^4q`e z)fO@A;{9K^?W?DbnesnPr6kK>$zaKo&;FhFd(GYFCIU^T+OIMb%Tqo+P%oq(IdX7S zf6+HLO?7o0m+p>~Tp5UrXWh!UH!wZ5kv!E`_w)PTpI(#Iw{AS`gH4^b(bm^ZCq^FZ zY9DD7bH}rq9mg88+KgA$Zp!iWncuU2n1AuIa@=sWvUR-s`Qb{R*kk(SPU^`$6BXz8 zn#7yaFOIK%qGxyi`dYtm#&qqox0$h=pNi#u=M8zUG@bpiZ=3sT=1}Trr}39cC)H|v zbL?W)=&s4zrh)7>L(|cc%$1#!zfL?HjpeP%T+x_a+jZ16b^iKOHxFEX$7d|8${H-* zIrOJ5w&i$>*D>AKaIoYg`;{L@jM((Kt?$N$5OnuPqVvq**Nm}(f0wwOF%iX_Pba;V z;m@wxX&NcV3?<1+u?A{y_DIj7#m3Af1rCE)o`D&Y3}0%7E;iX1yMDiS)sh0wKi!36 zL!Wmq?P^Ku&rK~HJd97KkLTRl>ScGFYZNlYytWnhmuu|)L&ND8_PmkayQb{HOY640 bno1(wj@u8DCVuFR|31B*4ek@pZJqxCDDe1x literal 0 HcmV?d00001 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Wide310x150Logo.scale-200.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Wide310x150Logo.scale-200.png new file mode 100644 index 0000000000000000000000000000000000000000..288995b397fdbef1fb7e85afd71445d5de1952c5 GIT binary patch literal 3204 zcmbVPeQXow8NYmBd90>}0NP?GhXW~VaeThm=a0tV#EwJMI!)6M3}|c4_Bl3=Kd>G0 z(GHx1wl<7(tP?FsOQkTilSo*iIvF%uArExJ73~P zSv1xEy!U(Wd4A9D`FQV@W3@F^qJ@PEF$@z`Z!*BbFsS(^?B zyiAzJ+q})bkgiQHWqEb*jJD-coHYr1^iocg)l!Qa{Xqs-l~6J}p-|##ZHYofskQ3$ zI0;xzXyhazBeXhIsg5A=%ufo@f)1yy&ScKS0;HF^!r_2UE^lpZEom(+@duma3awTv zCrCL-%D_SvYWIcdHkmI}#50(fkUi)Qgx!80ju>g1za^}ff>JI8Z@^-iCiaCgg@TgF z+vtE?Q9{VQUX&MW9SYYmGcxA14%N2@7FwBTD4N<(2{nWgV8$e3?-F=L^&FrtWn~(U_Q~~^uYiyeY6-KoTnfh9AWz@ zIKje0)u!_Lw)E}G!#kEfwKVdNt(UAf9*f>tEL_(=xco-T%jTi@7YlC3hs2ik%Le0H ztj}RTeCF(5mwvi3_56>-yB?l;J>-1%!9~=fs|QcNG3J~a@JCu`4SB460s0ZO+##4fFUSGLcj_ja^fL4&BKALfb#$6$O?>P@qx2Agl^x0i&ugt zsy5Pyu=()`7HRMG3IB7F1@`_ z+-!J%#i6e^U$e#+C%Q>_qVRzWRsG^W_n+@OcX@vzI&z;mzHNb!GQ?LWA(wtpqHqTM z1OFw_{Zn?fD)p)`c`kOgv{de=v@suGRqY{N^U7gI1VF3*F=obwaXI6ob5__Yn zVTguS!%(NI09J8x#AO_aW!9W7k*UvB;IWDFC3srwftr{kHj%g)fvnAm;&h_dnl~

MY- zf+K}sCe8qU6Ujs`3ua{U0Of$R_gVQBuUA za0v=mu#vIOqiiAZOr&h*$WyOw&k-xr$;G4Ixa!#TJNr>95(h>l%)PUy4p+^SgR(uR zta%k*?ny-+nAr8spEk1fo{J4i!b^Fia`N{_F6@zidA2ZTTrjl#^5Z-2KfB@Cu}l9s z(*|Z2jc?p~vn2f)3y9i*7zJV1L{$?|&q)4oaT;uXi6>1GkRXVTOzAz(RHEmr=eFIi z`}<>-Q?K0GN8!IYxeP1XKXO+jsJbp~o^);Bc;%b7Flpe7;1`Ny@3r7ZR;?R)aJt8C ziNlEC<@3f_lIV4TwV}&e;D!Ee5_|e#g0LUh=5vmYWYm7&2h*M>QPKvGh9-)wfMMW3 z8J9b%1k7dzPzO0_NGQy92BZ^FR6R~6;^6?lqO;-QUP4BY%cG%3vEhbm#>4vIhPBh3 z-+pZGjh$x%Hp{?=FHsMp0&wNPlj00us{&`1ZOZTqs8%4X&xH=UDr*xyBW(Zp&Em94 zf)ZSfn#yg0N)>!1kWdkqJ^S*z0FF5|fj&qcE#Na|%OY0$uO>!&hP+1ywfD_WXk@4J(?MBftK7>$Nvqh@tDuarN%PrTLQ2Uzysx>UV=V zk^RrDSvdQ?0;=hY67EgII-f4`t=+i*yS=Y~!XlqIy_4x&%+OdfbKOFPXS2X5%4R{N z$SQMX^AK6(fA + + + + Debug + x86 + {ff1f72d6-9524-4422-9497-3cc0002216ed} + AppContainerExe + Properties + MQTTnet.TestApp.UniversalWindows + MQTTnet.TestApp.UniversalWindows + en-US + UAP + 10.0.14393.0 + 10.0.10240.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + true + + MQTTnet.TestApp.UniversalWindows_TemporaryKey.pfx + + + + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x86 + false + prompt + true + + + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x86 + false + prompt + true + true + + + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + ARM + false + prompt + true + + + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + ARM + false + prompt + true + true + + + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + x64 + false + prompt + true + + + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + x64 + false + prompt + true + true + + + + + + + + + + App.xaml + + + MainPage.xaml + + + + + + Designer + + + + + + + + + + + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + + + 14.0 + + + + + diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml new file mode 100644 index 00000000..3beb0537 --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml @@ -0,0 +1,13 @@ + + + + + + diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs new file mode 100644 index 00000000..3b13a557 --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 + +namespace MQTTnet.TestApp.UniversalWindows +{ + ///

+ /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class MainPage : Page + { + public MainPage() + { + this.InitializeComponent(); + } + } +} diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest b/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest new file mode 100644 index 00000000..3027fdea --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/Package.appxmanifest @@ -0,0 +1,49 @@ + + + + + + + + + + MQTTnet.TestApp.UniversalWindows + chris + Assets\StoreLogo.png + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/MQTTnet.TestMqttServer/Properties/AssemblyInfo.cs b/Tests/MQTTnet.TestApp.UniversalWindows/Properties/AssemblyInfo.cs similarity index 69% rename from Tests/MQTTnet.TestMqttServer/Properties/AssemblyInfo.cs rename to Tests/MQTTnet.TestApp.UniversalWindows/Properties/AssemblyInfo.cs index 28a81c78..d125e02e 100644 --- a/Tests/MQTTnet.TestMqttServer/Properties/AssemblyInfo.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/Properties/AssemblyInfo.cs @@ -1,7 +1,7 @@ using System.Reflection; using System.Runtime.InteropServices; -[assembly: AssemblyTitle("MQTTnet.TestMqttServer")] +[assembly: AssemblyTitle("MQTTnet.TestApp.UniversalWindows")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Christian Kratky")] @@ -9,7 +9,6 @@ using System.Runtime.InteropServices; [assembly: AssemblyCopyright("Copyright © Christian Kratky 2016-2017")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: ComVisible(false)] -[assembly: Guid("59c1090b-1734-4185-bc2f-53b9edd30079")] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: ComVisible(false)] \ No newline at end of file diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Properties/Default.rd.xml b/Tests/MQTTnet.TestApp.UniversalWindows/Properties/Default.rd.xml new file mode 100644 index 00000000..7c40ffeb --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/Properties/Default.rd.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/project.json b/Tests/MQTTnet.TestApp.UniversalWindows/project.json new file mode 100644 index 00000000..77bf149e --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/project.json @@ -0,0 +1,16 @@ +{ + "dependencies": { + "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.3" + }, + "frameworks": { + "uap10.0": {} + }, + "runtimes": { + "win10-arm": {}, + "win10-arm-aot": {}, + "win10-x86": {}, + "win10-x86-aot": {}, + "win10-x64": {}, + "win10-x64-aot": {} + } +} \ No newline at end of file diff --git a/Tests/MQTTnet.TestMqttClient/App.config b/Tests/MQTTnet.TestMqttClient/App.config deleted file mode 100644 index 8227adb9..00000000 --- a/Tests/MQTTnet.TestMqttClient/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/Tests/MQTTnet.TestMqttClient/MQTTnet.TestMqttClient.csproj b/Tests/MQTTnet.TestMqttClient/MQTTnet.TestMqttClient.csproj deleted file mode 100644 index b9722ffa..00000000 --- a/Tests/MQTTnet.TestMqttClient/MQTTnet.TestMqttClient.csproj +++ /dev/null @@ -1,100 +0,0 @@ - - - - - Debug - AnyCPU - {7B19B139-2E9D-4F1D-88B4-6180B4CF872A} - Exe - Properties - MQTTnet.TestMqttClient - MQTTnet.TestMqttClient - v4.5.2 - 512 - true - - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - false - - - bin\Any CPU\ - TRACE - true - pdbonly - AnyCPU - prompt - MinimumRecommendedRules.ruleset - - - x86 - bin\x86\Debug\ - - - x86 - bin\x86\Release\ - - - x86 - bin\x86\Any CPU\ - - - x64 - bin\x64\Debug\ - - - x64 - bin\x64\Release\ - - - x64 - bin\x64\Any CPU\ - - - - - - - - - - - - - - - {a480ef90-0eaa-4d9a-b271-47a9c47f6f7d} - MQTTnet.NetFramework - - - {2ecb99e4-72d0-4c23-99ba-93d511d3967d} - MQTTnet.Core - - - - - \ No newline at end of file diff --git a/Tests/MQTTnet.TestMqttServer/Program.cs b/Tests/MQTTnet.TestMqttServer/Program.cs deleted file mode 100644 index 7df076d1..00000000 --- a/Tests/MQTTnet.TestMqttServer/Program.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using MQTTnet.Core.Diagnostics; -using MQTTnet.Core.Protocol; -using MQTTnet.Core.Server; - -namespace MQTTnet.TestMqttServer -{ - public static class Program - { - public static void Main(string[] arguments) - { - MqttTrace.TraceMessagePublished += (s, e) => - { - Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}"); - if (e.Exception != null) - { - Console.WriteLine(e.Exception); - } - }; - - try - { - var options = new MqttServerOptions - { - ConnectionValidator = p => - { - if (p.ClientId == "SpecialClient") - { - if (p.Username != "USER" || p.Password != "PASS") - { - return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; - } - } - - return MqttConnectReturnCode.ConnectionAccepted; - } - }; - - var mqttServer = new MqttServerFactory().CreateMqttServer(options); - mqttServer.Start(); - - Console.WriteLine("Press any key to exit."); - Console.ReadLine(); - - mqttServer.Stop(); - } - catch (Exception e) - { - Console.WriteLine(e); - } - - Console.ReadLine(); - } - } -}