mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-22 12:53:04 +08:00
Refactor code
This commit is contained in:
@@ -2,6 +2,12 @@
|
|||||||
// The .NET Foundation licenses this file to you under the MIT license.
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO.Pipelines;
|
||||||
|
using System.Net;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Connections;
|
using Microsoft.AspNetCore.Connections;
|
||||||
using Microsoft.AspNetCore.Http.Connections.Features;
|
using Microsoft.AspNetCore.Http.Connections.Features;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
@@ -11,19 +17,14 @@ using MQTTnet.Exceptions;
|
|||||||
using MQTTnet.Formatter;
|
using MQTTnet.Formatter;
|
||||||
using MQTTnet.Internal;
|
using MQTTnet.Internal;
|
||||||
using MQTTnet.Packets;
|
using MQTTnet.Packets;
|
||||||
using System;
|
|
||||||
using System.IO.Pipelines;
|
|
||||||
using System.Net;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MQTTnet.AspNetCore
|
namespace MQTTnet.AspNetCore
|
||||||
{
|
{
|
||||||
public sealed class MqttConnectionContext : IMqttChannelAdapter
|
public sealed class MqttConnectionContext : IMqttChannelAdapter
|
||||||
{
|
{
|
||||||
readonly AsyncLock _writerLock = new AsyncLock();
|
|
||||||
readonly ConnectionContext _connection;
|
readonly ConnectionContext _connection;
|
||||||
|
readonly AsyncLock _writerLock = new AsyncLock();
|
||||||
|
|
||||||
PipeReader _input;
|
PipeReader _input;
|
||||||
PipeWriter _output;
|
PipeWriter _output;
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ namespace MQTTnet.AspNetCore
|
|||||||
{
|
{
|
||||||
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
|
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
|
||||||
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
|
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
|
||||||
|
|
||||||
_input = connection.Transport.Input;
|
_input = connection.Transport.Input;
|
||||||
_output = connection.Transport.Output;
|
_output = connection.Transport.Output;
|
||||||
}
|
}
|
||||||
@@ -52,15 +54,10 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
// mqtt over websocket
|
// mqtt over websocket
|
||||||
var httpFeature = _connection.Features.Get<IHttpContextFeature>();
|
var httpFeature = _connection.Features.Get<IHttpContextFeature>();
|
||||||
if (httpFeature != null && httpFeature.HttpContext != null)
|
return httpFeature?.HttpContext?.Connection.ClientCertificate;
|
||||||
{
|
|
||||||
return httpFeature.HttpContext.Connection.ClientCertificate;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public string Endpoint
|
public string Endpoint
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -74,7 +71,7 @@ namespace MQTTnet.AspNetCore
|
|||||||
#endif
|
#endif
|
||||||
// mqtt over websocket
|
// mqtt over websocket
|
||||||
var httpFeature = _connection.Features.Get<IHttpConnectionFeature>();
|
var httpFeature = _connection.Features.Get<IHttpConnectionFeature>();
|
||||||
if (httpFeature != null && httpFeature.RemoteIpAddress != null)
|
if (httpFeature?.RemoteIpAddress != null)
|
||||||
{
|
{
|
||||||
return new IPEndPoint(httpFeature.RemoteIpAddress, httpFeature.RemotePort).ToString();
|
return new IPEndPoint(httpFeature.RemoteIpAddress, httpFeature.RemotePort).ToString();
|
||||||
}
|
}
|
||||||
@@ -83,6 +80,8 @@ namespace MQTTnet.AspNetCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsReadingPacket { get; private set; }
|
||||||
|
|
||||||
public bool IsSecureConnection
|
public bool IsSecureConnection
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -96,20 +95,17 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
// mqtt over websocket
|
// mqtt over websocket
|
||||||
var httpFeature = _connection.Features.Get<IHttpContextFeature>();
|
var httpFeature = _connection.Features.Get<IHttpContextFeature>();
|
||||||
if (httpFeature != null && httpFeature.HttpContext != null)
|
if (httpFeature?.HttpContext != null)
|
||||||
{
|
{
|
||||||
return httpFeature.HttpContext.Request.IsHttps;
|
return httpFeature.HttpContext.Request.IsHttps;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public bool IsReadingPacket { get; private set; }
|
|
||||||
|
|
||||||
public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
|
public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
|
||||||
|
|
||||||
|
|
||||||
public async Task ConnectAsync(CancellationToken cancellationToken)
|
public async Task ConnectAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (_connection is TcpConnection tcp && !tcp.IsConnected)
|
if (_connection is TcpConnection tcp && !tcp.IsConnected)
|
||||||
@@ -131,6 +127,7 @@ namespace MQTTnet.AspNetCore
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
_writerLock.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MqttPacket> ReceivePacketAsync(CancellationToken cancellationToken)
|
public async Task<MqttPacket> ReceivePacketAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
Reference in New Issue
Block a user