From 84dab18c2a11a3679b777c94f68a6486a8728460 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 25 Apr 2018 19:55:30 +0200 Subject: [PATCH] Fix broken UWP support. --- .../Implementations/MqttTcpChannel.Uwp.cs | 11 +++++++++-- .../Implementations/MqttTcpServerAdapter.Uwp.cs | 8 +++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpChannel.Uwp.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpChannel.Uwp.cs index 1487f0a..41e1d54 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpChannel.Uwp.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpChannel.Uwp.cs @@ -37,6 +37,8 @@ namespace MQTTnet.Implementations public MqttTcpChannel(StreamSocket socket) { _socket = socket ?? throw new ArgumentNullException(nameof(socket)); + + CreateStreams(); } public static Func> CustomIgnorableServerCertificateErrorsResolver { get; set; } @@ -65,8 +67,7 @@ namespace MQTTnet.Implementations await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), SocketProtectionLevel.Tls12); } - _readStream = _socket.InputStream.AsStreamForRead(_bufferSize); - _writeStream = _socket.OutputStream.AsStreamForWrite(_bufferSize); + CreateStreams(); } public Task DisconnectAsync() @@ -177,6 +178,12 @@ namespace MQTTnet.Implementations return result; } + + private void CreateStreams() + { + _readStream = _socket.InputStream.AsStreamForRead(_bufferSize); + _writeStream = _socket.OutputStream.AsStreamForWrite(_bufferSize); + } } } #endif \ No newline at end of file diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.Uwp.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.Uwp.cs index c6b0ff4..aba092a 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.Uwp.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttTcpServerAdapter.Uwp.cs @@ -26,10 +26,14 @@ namespace MQTTnet.Implementations if (options == null) throw new ArgumentNullException(nameof(options)); if (_defaultEndpointSocket != null) throw new InvalidOperationException("Server is already started."); - + if (options.DefaultEndpointOptions.IsEnabled) { _defaultEndpointSocket = new StreamSocketListener(); + + // This also affects the client sockets. + _defaultEndpointSocket.Control.NoDelay = true; + await _defaultEndpointSocket.BindServiceNameAsync(options.GetDefaultEndpointPort().ToString(), SocketProtectionLevel.PlainSocket); _defaultEndpointSocket.ConnectionReceived += AcceptDefaultEndpointConnectionsAsync; } @@ -62,8 +66,6 @@ namespace MQTTnet.Implementations { try { - args.Socket.Control.NoDelay = true; - var clientAdapter = new MqttChannelAdapter(new MqttTcpChannel(args.Socket), new MqttPacketSerializer(), _logger); ClientAccepted?.Invoke(this, new MqttServerAdapterClientAcceptedEventArgs(clientAdapter)); }