diff --git a/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetCoreApp/Implementations/MqttServerAdapter.cs new file mode 100644 index 0000000..335de78 --- /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 0000000..fabe7b9 --- /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 10ff3fa..ac6f611 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 7f3feb6..0000000 --- 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 1d3db70..0000000 --- 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 bc5ef2f..eb7441c 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 b55771c..0000000 --- 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 58b0a3b..0000000 --- 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 e32c27f..0000000 --- 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 0000000..f365efd --- /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 80c76cf..e272c97 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 314c158..7f4ab44 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 54e06c4..7b84239 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 7fe9ca3..0000000 --- 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 b7b1b30..0000000 --- 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 71e57af..eb7441c 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 699948d..0000000 --- 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 f02c74b..0000000 --- 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 0000000..f8fe07f --- /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 0000000..042fcb0 --- /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 2323cd0..9022957 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 4da24a1..7b84239 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 b9eeecd..0000000 --- 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 a725426..eb7441c 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 5763df4..0000000 --- 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 412563e..8529523 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 d306bfe..9eebdd5 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 0000000..404dd01 --- /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 0000000..3185d10 --- /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 eb73c30..9ef3dcd 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 2e707d7..e724007 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 95d72b8..a4724d8 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 c0f4972..795748d 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 418deb0..0000000 --- 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 1b4a9d5..382d702 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 005f999..c624bea 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 3d262dc..d8a5fd9 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 9adec4d..aadfe52 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 4e3659a..5be8407 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 9640e64..3df1c3a 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 d7a837c..753a357 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 2b182bf..b3e5d3e 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 47f06a6..cb6b485 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 5d5b375..a8ed333 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 2a556b7..c95d16b 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 4bef319..750a152 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 f46ebe0..3a7265f 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 dfc815c..a0949e8 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 389a5a7..5f33095 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 88df034..3e3f36f 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 569f9e6..e2ae422 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 a95f056..935c777 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 fbaf175..d6abec9 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 76071cf..ffac41b 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 0000000..203ed28 --- /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 ea62122..8f80272 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 63b4881..926e581 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 3d93e4b..8847c70 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 1dcbd44..2a9a0b2 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 7064117..8cb9563 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 0b689e2..e1cda96 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 0638700..c93870e 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 0000000..cad2a26 --- /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 0000000..5a6a46d --- /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 0000000..3839334 --- /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 9e71785..d373465 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 b7e540a..1053400 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 6ef5398..b44ec2f 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 00fbfb2..1ca8f82 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 d4cc365..5c91b3a 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 41d547a..6ec31cd 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 f90b75a..5fb5495 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 0000000..5f70fb2 --- /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 0000000..de5a9f8 --- /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 0000000..735f57a Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/LockScreenLogo.scale-200.png differ 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 0000000..023e7f1 Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/SplashScreen.scale-200.png differ 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 0000000..af49fec Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square150x150Logo.scale-200.png differ 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 0000000..ce342a2 Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.scale-200.png differ 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 0000000..f6c02ce Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Square44x44Logo.targetsize-24_altform-unplated.png differ diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/Assets/StoreLogo.png b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/StoreLogo.png new file mode 100644 index 0000000..7385b56 Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/StoreLogo.png differ 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 0000000..288995b Binary files /dev/null and b/Tests/MQTTnet.TestApp.UniversalWindows/Assets/Wide310x150Logo.scale-200.png differ diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj b/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj new file mode 100644 index 0000000..e2ea210 --- /dev/null +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj @@ -0,0 +1,149 @@ + + + + + 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 0000000..3beb053 --- /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 0000000..3b13a55 --- /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 0000000..3027fde --- /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 28a81c7..d125e02 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 0000000..7c40ffe --- /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 0000000..77bf149 --- /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 8227adb..0000000 --- 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 b9722ff..0000000 --- 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 7df076d..0000000 --- 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(); - } - } -}