Refactor code

This commit is contained in:
Christian
2022-11-28 21:10:42 +01:00
parent 41df2ec5da
commit 9b20e0a430
2 changed files with 29 additions and 30 deletions
@@ -33,6 +33,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
@@ -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<IHttpContextFeature>();
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<IHttpContextFeature>();
public async Task ConnectAsync(CancellationToken cancellationToken)
@@ -98,6 +95,10 @@ namespace MQTTnet.AspNetCore
return Task.CompletedTask;
}
public void Dispose()
{
}
public async Task<MqttPacket> 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()
{
}
}
}
}