mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 11:03:07 +08:00
Refactor package object model.
This commit is contained in:
@@ -10,6 +10,7 @@ using System.Runtime.ExceptionServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Exceptions;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
{
|
||||
@@ -75,7 +76,7 @@ namespace MQTTnet.Implementations
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var networkStream = socket.GetStream();
|
||||
|
||||
|
||||
if (_options.TlsOptions?.UseTls == true)
|
||||
{
|
||||
var sslStream = new SslStream(networkStream, false, InternalUserCertificateValidationCallback);
|
||||
@@ -144,7 +145,7 @@ namespace MQTTnet.Implementations
|
||||
|
||||
if (stream == null)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(stream));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return await stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
|
||||
@@ -185,6 +186,10 @@ namespace MQTTnet.Implementations
|
||||
}
|
||||
|
||||
await stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// This subsequent call is required to check whether the socket is still connected.
|
||||
// Without this call a broken connection is only recognized at the next call.
|
||||
await stream.WriteAsync(PlatformAbstractionLayer.EmptyByteArray, 0, 0, cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
throw new ArgumentException("TLS certificate is not set.");
|
||||
}
|
||||
|
||||
|
||||
var tlsCertificate = options.TlsEndpointOptions.CertificateProvider.GetCertificate();
|
||||
if (!tlsCertificate.HasPrivateKey)
|
||||
{
|
||||
@@ -73,16 +73,22 @@ namespace MQTTnet.Implementations
|
||||
|
||||
void Cleanup()
|
||||
{
|
||||
_cancellationTokenSource?.Cancel(false);
|
||||
_cancellationTokenSource?.Dispose();
|
||||
_cancellationTokenSource = null;
|
||||
|
||||
foreach (var listener in _listeners)
|
||||
try
|
||||
{
|
||||
listener.Dispose();
|
||||
_cancellationTokenSource?.Cancel(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_cancellationTokenSource?.Dispose();
|
||||
_cancellationTokenSource = null;
|
||||
|
||||
_listeners.Clear();
|
||||
foreach (var listener in _listeners)
|
||||
{
|
||||
listener.Dispose();
|
||||
}
|
||||
|
||||
_listeners.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
void RegisterListeners(MqttServerTcpEndpointBaseOptions options, X509Certificate2 tlsCertificate, CancellationToken cancellationToken)
|
||||
|
||||
@@ -77,7 +77,7 @@ namespace MQTTnet.Implementations
|
||||
|
||||
_socket.Bind(_localEndPoint);
|
||||
_socket.Listen(_options.ConnectionBacklog);
|
||||
|
||||
|
||||
Task.Run(() => AcceptClientConnectionsAsync(cancellationToken), cancellationToken).Forget(_logger);
|
||||
|
||||
return true;
|
||||
@@ -152,9 +152,7 @@ namespace MQTTnet.Implementations
|
||||
_addressFamily == AddressFamily.InterNetwork ? "ipv4" : "ipv6");
|
||||
|
||||
clientSocket.NoDelay = _options.NoDelay;
|
||||
|
||||
stream = clientSocket.GetStream();
|
||||
|
||||
X509Certificate2 clientCertificate = null;
|
||||
|
||||
if (_tlsCertificate != null)
|
||||
@@ -208,17 +206,17 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
stream?.Dispose();
|
||||
clientSocket?.Dispose();
|
||||
|
||||
_logger.Verbose("Client '{0}' disconnected at TCP listener '{1}, {2}'.",
|
||||
remoteEndPoint,
|
||||
_localEndPoint,
|
||||
_addressFamily == AddressFamily.InterNetwork ? "ipv4" : "ipv6");
|
||||
}
|
||||
catch (Exception disposeException)
|
||||
{
|
||||
_logger.Error(disposeException, "Error while cleaning up client connection");
|
||||
}
|
||||
}
|
||||
|
||||
_logger.Verbose("Client '{0}' disconnected at TCP listener '{1}, {2}'.",
|
||||
remoteEndPoint,
|
||||
_localEndPoint,
|
||||
_addressFamily == AddressFamily.InterNetwork ? "ipv4" : "ipv6");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,25 @@
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
{
|
||||
public static class PlatformAbstractionLayer
|
||||
{
|
||||
public static Task CompletedTask
|
||||
{
|
||||
get
|
||||
{
|
||||
#if NET452
|
||||
return Task.FromResult(0);
|
||||
#else
|
||||
return Task.CompletedTask;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
public static readonly byte[] EmptyByteArray = new byte[0];
|
||||
|
||||
#if NET452
|
||||
public static Task CompletedTask => Task.FromResult(0);
|
||||
#else
|
||||
public static Task CompletedTask => Task.CompletedTask;
|
||||
#endif
|
||||
|
||||
public static void Sleep(TimeSpan timeout)
|
||||
{
|
||||
#if NET452 || NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP3_1
|
||||
System.Threading.Thread.Sleep(timeout);
|
||||
#else
|
||||
Task.Delay(timeout).Wait();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user