Add new factory overloads.

This commit is contained in:
Christian Kratky
2020-05-22 22:02:05 +02:00
parent ba68b1274b
commit 60941c04e5
12 changed files with 78 additions and 35 deletions
@@ -11,11 +11,18 @@ using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.AspNetCore.Extensions;
using MQTTnet.Internal;
namespace MQTTnet.AspNetCore
{
public class MqttConnectionContext : IMqttChannelAdapter
public sealed class MqttConnectionContext : IMqttChannelAdapter
{
readonly AsyncLock _writerLock = new AsyncLock();
readonly SpanBasedMqttPacketBodyReader _reader;
PipeReader _input;
PipeWriter _output;
public MqttConnectionContext(MqttPacketFormatterAdapter packetFormatterAdapter, ConnectionContext connection)
{
PacketFormatterAdapter = packetFormatterAdapter ?? throw new ArgumentNullException(nameof(packetFormatterAdapter));
@@ -30,10 +37,6 @@ namespace MQTTnet.AspNetCore
_reader = new SpanBasedMqttPacketBodyReader();
}
private PipeReader _input;
private PipeWriter _output;
private readonly SpanBasedMqttPacketBodyReader _reader;
public string Endpoint
{
get
@@ -58,9 +61,8 @@ namespace MQTTnet.AspNetCore
public X509Certificate2 ClientCertificate => Http?.HttpContext?.Connection?.ClientCertificate;
private IHttpContextFeature Http => Connection.Features.Get<IHttpContextFeature>();
public ConnectionContext Connection { get; }
public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
public long BytesSent { get; set; }
@@ -69,7 +71,7 @@ namespace MQTTnet.AspNetCore
public Action ReadingPacketStartedCallback { get; set; }
public Action ReadingPacketCompletedCallback { get; set; }
private readonly SemaphoreSlim _writerSemaphore = new SemaphoreSlim(1, 1);
IHttpContextFeature Http => Connection.Features.Get<IHttpContextFeature>();
public async Task ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
@@ -165,8 +167,7 @@ namespace MQTTnet.AspNetCore
var msg = buffer.AsMemory();
var output = _output;
await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try
using (await _writerLock.WaitAsync(cancellationToken).ConfigureAwait(false))
{
var result = await output.WriteAsync(msg, cancellationToken).ConfigureAwait(false);
if (result.IsCompleted)
@@ -176,10 +177,6 @@ namespace MQTTnet.AspNetCore
PacketFormatterAdapter.FreeBuffer();
}
finally
{
_writerSemaphore.Release();
}
}
public void Dispose()