From 5c1eceb8a9d58c5ae01c1b3faae474c477b73a0c Mon Sep 17 00:00:00 2001 From: Zazzmatazz Date: Fri, 2 Jun 2017 16:20:48 -0400 Subject: [PATCH] Updated task methods. Instead of awaiting most tasks, changed to simply returning the tasks. --- .../MqttClientSslChannel.cs | 8 +++--- .../MqttServerSslChannel.cs | 26 +++++++++---------- .../MQTTnet.NetCoreApp/MqttTcpChannel.cs | 16 ++++++------ .../MQTTnet.NetFramework/MqttClientFactory.cs | 2 -- .../MqttClientSslChannel.cs | 13 +++++----- .../MqttServerSslChannel.cs | 26 +++++++++---------- .../MQTTnet.NetFramework/MqttTcpChannel.cs | 16 ++++++------ 7 files changed, 52 insertions(+), 55 deletions(-) diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs b/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs index 26b12fb..7f3feb6 100644 --- a/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs +++ b/Frameworks/MQTTnet.NetCoreApp/MqttClientSslChannel.cs @@ -77,14 +77,14 @@ namespace MQTTnet /// Asynchronously writes a sequence of bytes to the socket. /// /// The buffer to write data from. - public async Task WriteAsync(byte[] buffer) + public Task WriteAsync(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); try { - await _sslStream.WriteAsync(buffer, 0, buffer.Length); + return _sslStream.WriteAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) @@ -97,11 +97,11 @@ namespace MQTTnet /// Asynchronously reads a sequence of bytes from the socket. /// /// The buffer to write the data into. - public async Task ReadAsync(byte[] buffer) + public Task ReadAsync(byte[] buffer) { try { - await _sslStream.ReadAsync(buffer, 0, buffer.Length); + return _sslStream.ReadAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs b/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs index c4c5713..b55771c 100644 --- a/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs +++ b/Frameworks/MQTTnet.NetCoreApp/MqttServerSslChannel.cs @@ -30,27 +30,27 @@ namespace MQTTnet _socket = socket ?? throw new ArgumentNullException(nameof(socket)); _cert = cert ?? throw new ArgumentNullException(nameof(cert)); - if (_socket.Connected) - { - NetworkStream ns = new NetworkStream(_socket, true); - _sslStream = new SslStream(ns); - } + if (!_socket.Connected) + return; + + NetworkStream ns = new NetworkStream(_socket, true); + _sslStream = new SslStream(ns); } - public async Task Authenticate() + public Task Authenticate() { - await _sslStream.AuthenticateAsServerAsync(_cert, false, SslProtocols.Tls12, false); + return _sslStream.AuthenticateAsServerAsync(_cert, false, SslProtocols.Tls12, false); } /// /// Asynchronously connects to the client described in the . /// /// The describing the connection. - public async Task ConnectAsync(MqttClientOptions options) + public Task ConnectAsync(MqttClientOptions options) { try { - await _socket.ConnectAsync(options.Server, options.Port); + return _socket.ConnectAsync(options.Server, options.Port); } catch (SocketException exception) { @@ -78,14 +78,14 @@ namespace MQTTnet /// Asynchronously writes a sequence of bytes to the socket. /// /// The buffer to write data from. - public async Task WriteAsync(byte[] buffer) + public Task WriteAsync(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); try { - await _sslStream.WriteAsync(buffer, 0, buffer.Length); + return _sslStream.WriteAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) @@ -98,11 +98,11 @@ namespace MQTTnet /// Asynchronously reads a sequence of bytes from the socket. /// /// The buffer to write the data into. - public async Task ReadAsync(byte[] buffer) + public Task ReadAsync(byte[] buffer) { try { - await _sslStream.ReadAsync(buffer, 0, buffer.Length); + return _sslStream.ReadAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) diff --git a/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs index 139037c..e32c27f 100644 --- a/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.NetCoreApp/MqttTcpChannel.cs @@ -21,11 +21,11 @@ namespace MQTTnet _socket = socket ?? throw new ArgumentNullException(nameof(socket)); } - public async Task ConnectAsync(MqttClientOptions options) + public Task ConnectAsync(MqttClientOptions options) { try { - await _socket.ConnectAsync(options.Server, options.Port); + return _socket.ConnectAsync(options.Server, options.Port); } catch (SocketException exception) { @@ -33,12 +33,12 @@ namespace MQTTnet } } - public async Task DisconnectAsync() + public Task DisconnectAsync() { try { _socket.Dispose(); - await Task.FromResult(0); + return Task.FromResult(0); } catch (SocketException exception) { @@ -46,13 +46,13 @@ namespace MQTTnet } } - public async Task WriteAsync(byte[] buffer) + public Task WriteAsync(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); try { - await _socket.SendAsync(new ArraySegment(buffer), SocketFlags.None); + return _socket.SendAsync(new ArraySegment(buffer), SocketFlags.None); } catch (SocketException exception) { @@ -60,12 +60,12 @@ namespace MQTTnet } } - public async Task ReadAsync(byte[] buffer) + public Task ReadAsync(byte[] buffer) { try { var buffer2 = new ArraySegment(buffer); - await _socket.ReceiveAsync(buffer2, SocketFlags.None); + return _socket.ReceiveAsync(buffer2, SocketFlags.None); } catch (SocketException exception) { diff --git a/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs b/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs index 048112f..54e06c4 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttClientFactory.cs @@ -13,8 +13,6 @@ namespace MQTTnet if (options == null) throw new ArgumentNullException(nameof(options)); return new MqttClient(options, - // The cast to IMqttCommunicationChannel is required... Roslyn is too stupid to - // figure out how to cast back to the base type new MqttChannelCommunicationAdapter(options.UseSSL ? new MqttClientSslChannel() : (IMqttCommunicationChannel)new MqttTcpChannel(), new DefaultMqttV311PacketSerializer())); } diff --git a/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs b/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs index 8adf14b..7fe9ca3 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttClientSslChannel.cs @@ -50,7 +50,6 @@ namespace MQTTnet _sslStream = new SslStream(ns); await _sslStream.AuthenticateAsClientAsync(options.Server, null, SslProtocols.Tls12, false); - } catch (SocketException exception) { @@ -61,11 +60,11 @@ namespace MQTTnet /// /// Asynchronously disconnects the client from the server. /// - public async Task DisconnectAsync() + public Task DisconnectAsync() { try { - await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); + return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); } catch (SocketException exception) { @@ -77,14 +76,14 @@ namespace MQTTnet /// Asynchronously writes a sequence of bytes to the socket. /// /// The buffer to write data from. - public async Task WriteAsync(byte[] buffer) + public Task WriteAsync(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); try { - await _sslStream.WriteAsync(buffer, 0, buffer.Length); + return _sslStream.WriteAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) @@ -97,11 +96,11 @@ namespace MQTTnet /// Asynchronously reads a sequence of bytes from the socket. /// /// The buffer to write the data into. - public async Task ReadAsync(byte[] buffer) + public Task ReadAsync(byte[] buffer) { try { - await _sslStream.ReadAsync(buffer, 0, buffer.Length); + return _sslStream.ReadAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) diff --git a/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs b/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs index 2f01462..699948d 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttServerSslChannel.cs @@ -30,11 +30,11 @@ namespace MQTTnet _socket = socket ?? throw new ArgumentNullException(nameof(socket)); _cert = cert ?? throw new ArgumentNullException(nameof(cert)); - if (_socket.Connected) - { - NetworkStream ns = new NetworkStream(_socket, true); - _sslStream = new SslStream(ns); - } + if (!_socket.Connected) + return; + + NetworkStream ns = new NetworkStream(_socket, true); + _sslStream = new SslStream(ns); } public async Task Authenticate() @@ -46,11 +46,11 @@ namespace MQTTnet /// Asynchronously connects to the client described in the . /// /// The describing the connection. - public async Task ConnectAsync(MqttClientOptions options) + public Task ConnectAsync(MqttClientOptions options) { try { - await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, + return Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, null); } catch (SocketException exception) @@ -62,11 +62,11 @@ namespace MQTTnet /// /// Asynchronously disconnects the client from the server. /// - public async Task DisconnectAsync() + public Task DisconnectAsync() { try { - await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); + return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); } catch (SocketException exception) { @@ -78,14 +78,14 @@ namespace MQTTnet /// Asynchronously writes a sequence of bytes to the socket. /// /// The buffer to write data from. - public async Task WriteAsync(byte[] buffer) + public Task WriteAsync(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); try { - await _sslStream.WriteAsync(buffer, 0, buffer.Length); + return _sslStream.WriteAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) @@ -98,11 +98,11 @@ namespace MQTTnet /// Asynchronously reads a sequence of bytes from the socket. /// /// The buffer to write the data into. - public async Task ReadAsync(byte[] buffer) + public Task ReadAsync(byte[] buffer) { try { - await _sslStream.ReadAsync(buffer, 0, buffer.Length); + return _sslStream.ReadAsync(buffer, 0, buffer.Length); } catch (Exception ex) when (ex is SocketException || ex is IOException) diff --git a/Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs b/Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs index c690bcb..80c76cf 100644 --- a/Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.NetFramework/MqttTcpChannel.cs @@ -21,11 +21,11 @@ namespace MQTTnet _socket = socket ?? throw new ArgumentNullException(nameof(socket)); } - public async Task ConnectAsync(MqttClientOptions options) + public Task ConnectAsync(MqttClientOptions options) { try { - await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, null); + return Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.Port, null); } catch (SocketException exception) { @@ -33,11 +33,11 @@ namespace MQTTnet } } - public async Task DisconnectAsync() + public Task DisconnectAsync() { try { - await Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); + return Task.Factory.FromAsync(_socket.BeginDisconnect, _socket.EndDisconnect, true, null); } catch (SocketException exception) { @@ -45,13 +45,13 @@ namespace MQTTnet } } - public async Task WriteAsync(byte[] buffer) + public Task WriteAsync(byte[] buffer) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); try { - await Task.Factory.FromAsync( + return Task.Factory.FromAsync( // ReSharper disable once AssignNullToNotNullAttribute _socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, null, null), _socket.EndSend); @@ -62,11 +62,11 @@ namespace MQTTnet } } - public async Task ReadAsync(byte[] buffer) + public Task ReadAsync(byte[] buffer) { try { - await Task.Factory.FromAsync( + return Task.Factory.FromAsync( // ReSharper disable once AssignNullToNotNullAttribute _socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null), _socket.EndReceive);