mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Add missing overloads for client disconnect.
Add timeout for disconnect in managed client.
This commit is contained in:
@@ -53,11 +53,11 @@ namespace MQTTnet.Extensions.ManagedClient
|
|||||||
public bool IsConnected => InternalClient.IsConnected;
|
public bool IsConnected => InternalClient.IsConnected;
|
||||||
|
|
||||||
public bool IsStarted => _connectionCancellationToken != null;
|
public bool IsStarted => _connectionCancellationToken != null;
|
||||||
|
|
||||||
public IMqttClient InternalClient { get; }
|
public IMqttClient InternalClient { get; }
|
||||||
|
|
||||||
public int PendingApplicationMessagesCount => _messageQueue.Count;
|
public int PendingApplicationMessagesCount => _messageQueue.Count;
|
||||||
|
|
||||||
public IManagedMqttClientOptions Options { get; private set; }
|
public IManagedMqttClientOptions Options { get; private set; }
|
||||||
|
|
||||||
public IMqttClientConnectedHandler ConnectedHandler
|
public IMqttClientConnectedHandler ConnectedHandler
|
||||||
@@ -92,7 +92,7 @@ namespace MQTTnet.Extensions.ManagedClient
|
|||||||
|
|
||||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||||
if (options.ClientOptions == null) throw new ArgumentException("The client options are not set.", nameof(options));
|
if (options.ClientOptions == null) throw new ArgumentException("The client options are not set.", nameof(options));
|
||||||
|
|
||||||
if (!_maintainConnectionTask?.IsCompleted ?? false) throw new InvalidOperationException("The managed client is already started.");
|
if (!_maintainConnectionTask?.IsCompleted ?? false) throw new InvalidOperationException("The managed client is already started.");
|
||||||
|
|
||||||
Options = options;
|
Options = options;
|
||||||
@@ -292,7 +292,14 @@ namespace MQTTnet.Extensions.ManagedClient
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await InternalClient.DisconnectAsync().ConfigureAwait(false);
|
using (var disconnectTimeout = new CancellationTokenSource(Options.ClientOptions.CommunicationTimeout))
|
||||||
|
{
|
||||||
|
await InternalClient.DisconnectAsync(disconnectTimeout.Token).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
_logger.Warning("Timeout while sending DISCONNECT packet.");
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace MQTTnet.Client.Disconnecting
|
namespace MQTTnet.Client.Disconnecting
|
||||||
{
|
{
|
||||||
public class MqttClientDisconnectOptions
|
public sealed class MqttClientDisconnectOptions
|
||||||
{
|
{
|
||||||
public MqttClientDisconnectReason ReasonCode { get; set; } = MqttClientDisconnectReason.NormalDisconnection;
|
public MqttClientDisconnectReason ReasonCode { get; set; } = MqttClientDisconnectReason.NormalDisconnection;
|
||||||
|
|
||||||
|
|||||||
@@ -128,7 +128,21 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|
||||||
return client.DisconnectAsync(new MqttClientDisconnectOptions());
|
return client.DisconnectAsync(CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task DisconnectAsync(this IMqttClient client, MqttClientDisconnectOptions options)
|
||||||
|
{
|
||||||
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|
||||||
|
return client.DisconnectAsync(options, CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task DisconnectAsync(this IMqttClient client, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|
||||||
|
return client.DisconnectAsync(new MqttClientDisconnectOptions(), cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<MqttClientSubscribeResult> SubscribeAsync(this IMqttClient client, params MqttTopicFilter[] topicFilters)
|
public static Task<MqttClientSubscribeResult> SubscribeAsync(this IMqttClient client, params MqttTopicFilter[] topicFilters)
|
||||||
@@ -176,13 +190,6 @@ namespace MQTTnet.Client
|
|||||||
return client.ConnectAsync(options, CancellationToken.None);
|
return client.ConnectAsync(options, CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task DisconnectAsync(this IMqttClient client, MqttClientDisconnectOptions options)
|
|
||||||
{
|
|
||||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
|
||||||
|
|
||||||
return client.DisconnectAsync(options, CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Task SendExtendedAuthenticationExchangeDataAsync(this IMqttClient client, MqttExtendedAuthenticationExchangeData data)
|
public static Task SendExtendedAuthenticationExchangeDataAsync(this IMqttClient client, MqttExtendedAuthenticationExchangeData data)
|
||||||
{
|
{
|
||||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ namespace MQTTnet.Diagnostics
|
|||||||
{
|
{
|
||||||
logger.Publish(MqttNetLogLevel.Warning, message, parameters, exception);
|
logger.Publish(MqttNetLogLevel.Warning, message, parameters, exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void Warning(this IMqttNetScopedLogger logger, string message, params object[] parameters)
|
||||||
|
{
|
||||||
|
logger.Publish(MqttNetLogLevel.Warning, message, parameters, null);
|
||||||
|
}
|
||||||
|
|
||||||
public static void Error(this IMqttNetScopedLogger logger, Exception exception, string message, params object[] parameters)
|
public static void Error(this IMqttNetScopedLogger logger, Exception exception, string message, params object[] parameters)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user