From 9b20e0a430f0494350230833b007099f82061c62 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Mon, 28 Nov 2022 21:10:42 +0100 Subject: [PATCH] Refactor code --- .../MQTTnet.AspNetCore.csproj | 1 + .../MqttConnectionContext.cs | 58 +++++++++---------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/Source/MQTTnet.AspnetCore/MQTTnet.AspNetCore.csproj b/Source/MQTTnet.AspnetCore/MQTTnet.AspNetCore.csproj index 953a7949..7fff4ab4 100644 --- a/Source/MQTTnet.AspnetCore/MQTTnet.AspNetCore.csproj +++ b/Source/MQTTnet.AspnetCore/MQTTnet.AspNetCore.csproj @@ -33,6 +33,7 @@ true true 1591 + 7.3 diff --git a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs index 3844ff57..8dd380f8 100644 --- a/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs +++ b/Source/MQTTnet.AspnetCore/MqttConnectionContext.cs @@ -2,6 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.IO.Pipelines; +using System.Security.Cryptography.X509Certificates; +using System.Threading; +using System.Threading.Tasks; using Microsoft.AspNetCore.Connections; using Microsoft.AspNetCore.Http.Connections.Features; using MQTTnet.Adapter; @@ -10,18 +15,13 @@ using MQTTnet.Exceptions; using MQTTnet.Formatter; using MQTTnet.Internal; using MQTTnet.Packets; -using System; -using System.IO.Pipelines; -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using System.Threading.Tasks; namespace MQTTnet.AspNetCore { public sealed class MqttConnectionContext : IMqttChannelAdapter { - readonly AsyncLock _writerLock = new AsyncLock(); readonly bool? _isOverWebSocket; + readonly AsyncLock _writerLock = new AsyncLock(); PipeReader _input; PipeWriter _output; @@ -31,18 +31,23 @@ namespace MQTTnet.AspNetCore Connection = connection ?? throw new ArgumentNullException(nameof(connection)); var feature = connection.Features.Get(); - if (feature != null && feature.HttpContext != null) + if (feature?.HttpContext != null) { _isOverWebSocket = feature.HttpContext.WebSockets.IsWebSocketRequest; } - if (Connection.Transport != null) - { - _input = Connection.Transport.Input; - _output = Connection.Transport.Output; - } + _input = Connection.Transport.Input; + _output = Connection.Transport.Output; } + public long BytesReceived { get; private set; } + + public long BytesSent { get; private set; } + + public X509Certificate2 ClientCertificate => Http?.HttpContext?.Connection?.ClientCertificate; + + public ConnectionContext Connection { get; } + public string Endpoint { get @@ -63,20 +68,12 @@ namespace MQTTnet.AspNetCore } } - public bool IsSecureConnection => Http?.HttpContext?.Request?.IsHttps ?? false; + public bool IsReadingPacket { get; private set; } - public X509Certificate2 ClientCertificate => Http?.HttpContext?.Connection?.ClientCertificate; - - public ConnectionContext Connection { get; } + public bool IsSecureConnection => Http?.HttpContext?.Request.IsHttps ?? false; public MqttPacketFormatterAdapter PacketFormatterAdapter { get; } - public long BytesSent { get; set; } - - public long BytesReceived { get; set; } - - public bool IsReadingPacket { get; private set; } - IHttpContextFeature Http => Connection.Features.Get(); public async Task ConnectAsync(CancellationToken cancellationToken) @@ -98,6 +95,10 @@ namespace MQTTnet.AspNetCore return Task.CompletedTask; } + public void Dispose() + { + } + public async Task ReceivePacketAsync(CancellationToken cancellationToken) { var input = Connection.Transport.Input; @@ -151,11 +152,11 @@ namespace MQTTnet.AspNetCore } } } - catch (Exception e) + catch (Exception exception) { // completing the channel makes sure that there is no more data read after a protocol error - _input?.Complete(e); - _output?.Complete(e); + _input?.Complete(exception); + _output?.Complete(exception); throw; } finally @@ -180,6 +181,7 @@ namespace MQTTnet.AspNetCore try { var buffer = PacketFormatterAdapter.Encode(packet); + if (_isOverWebSocket == false) { await _output.WriteAsync(buffer.Packet, cancellationToken).ConfigureAwait(false); @@ -202,9 +204,5 @@ namespace MQTTnet.AspNetCore } } } - - public void Dispose() - { - } } -} +} \ No newline at end of file