Explorar el Código

Add event for processed application message (managed client only)

release/3.x.x
Christian hace 6 años
padre
commit
034e7fd750
Se han modificado 3 ficheros con 38 adiciones y 8 borrados
  1. +19
    -0
      Frameworks/MQTTnet.NetStandard/ManagedClient/ApplicationMessageProcessedEventArgs.cs
  2. +2
    -0
      Frameworks/MQTTnet.NetStandard/ManagedClient/IManagedMqttClient.cs
  3. +17
    -8
      Frameworks/MQTTnet.NetStandard/ManagedClient/ManagedMqttClient.cs

+ 19
- 0
Frameworks/MQTTnet.NetStandard/ManagedClient/ApplicationMessageProcessedEventArgs.cs Ver fichero

@@ -0,0 +1,19 @@
using System;

namespace MQTTnet.ManagedClient
{
public class ApplicationMessageProcessedEventArgs : EventArgs
{
public ApplicationMessageProcessedEventArgs(MqttApplicationMessage applicationMessage, Exception exception)
{
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
Exception = exception;
}

public MqttApplicationMessage ApplicationMessage { get; }
public Exception Exception { get; }

public bool HasFailed => Exception != null;
public bool HasSucceeded => Exception == null;
}
}

+ 2
- 0
Frameworks/MQTTnet.NetStandard/ManagedClient/IManagedMqttClient.cs Ver fichero

@@ -12,6 +12,8 @@ namespace MQTTnet.ManagedClient
event EventHandler<MqttClientConnectedEventArgs> Connected;
event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;

event EventHandler<ApplicationMessageProcessedEventArgs> ApplicationMessageProcessed;

Task StartAsync(IManagedMqttClientOptions options);
Task StopAsync();



+ 17
- 8
Frameworks/MQTTnet.NetStandard/ManagedClient/ManagedMqttClient.cs Ver fichero

@@ -27,7 +27,7 @@ namespace MQTTnet.ManagedClient
private IManagedMqttClientOptions _options;

private bool _subscriptionsNotPushed;
public ManagedMqttClient(IMqttClient mqttClient, IMqttNetLogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
@@ -43,6 +43,7 @@ namespace MQTTnet.ManagedClient
public event EventHandler<MqttClientConnectedEventArgs> Connected;
public event EventHandler<MqttClientDisconnectedEventArgs> Disconnected;
public event EventHandler<MqttApplicationMessageReceivedEventArgs> ApplicationMessageReceived;
public event EventHandler<ApplicationMessageProcessedEventArgs> ApplicationMessageProcessed;

public async Task StartAsync(IManagedMqttClientOptions options)
{
@@ -57,7 +58,7 @@ namespace MQTTnet.ManagedClient
if (_connectionCancellationToken != null) throw new InvalidOperationException("The managed client is already started.");

_options = options;
if (_options.Storage != null)
{
_storageManager = new ManagedMqttClientStorageManager(_options.Storage);
@@ -65,7 +66,7 @@ namespace MQTTnet.ManagedClient
}

_connectionCancellationToken = new CancellationTokenSource();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(async () => await MaintainConnectionAsync(_connectionCancellationToken.Token), _connectionCancellationToken.Token).ConfigureAwait(false);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
@@ -85,7 +86,7 @@ namespace MQTTnet.ManagedClient

return Task.FromResult(0);
}
public async Task PublishAsync(IEnumerable<MqttApplicationMessage> applicationMessages)
{
if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
@@ -96,7 +97,7 @@ namespace MQTTnet.ManagedClient
{
await _storageManager.AddAsync(applicationMessage).ConfigureAwait(false);
}
_messageQueue.Add(applicationMessage);
}
}
@@ -188,7 +189,7 @@ namespace MQTTnet.ManagedClient

StartPublishing();

return;
}

@@ -209,7 +210,7 @@ namespace MQTTnet.ManagedClient
_logger.Error<ManagedMqttClient>(exception, "Unhandled exception while maintaining connection.");
}
}
private async Task PublishQueuedMessagesAsync(CancellationToken cancellationToken)
{
try
@@ -227,7 +228,7 @@ namespace MQTTnet.ManagedClient
continue;
}

await TryPublishQueuedMessageAsync(message).ConfigureAwait(false);
await TryPublishQueuedMessageAsync(message).ConfigureAwait(false);
}
}
catch (OperationCanceledException)
@@ -245,6 +246,7 @@ namespace MQTTnet.ManagedClient

private async Task TryPublishQueuedMessageAsync(MqttApplicationMessage message)
{
Exception transmitException = null;
try
{
await _mqttClient.PublishAsync(message).ConfigureAwait(false);
@@ -256,6 +258,8 @@ namespace MQTTnet.ManagedClient
}
catch (MqttCommunicationException exception)
{
transmitException = exception;

_logger.Warning<ManagedMqttClient>(exception, "Publishing application message failed.");

if (message.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
@@ -265,8 +269,13 @@ namespace MQTTnet.ManagedClient
}
catch (Exception exception)
{
transmitException = exception;
_logger.Error<ManagedMqttClient>(exception, "Unhandled exception while publishing queued application message.");
}
finally
{
ApplicationMessageProcessed?.Invoke(this, new ApplicationMessageProcessedEventArgs(message, transmitException));
}
}

private async Task PushSubscriptionsAsync()


Cargando…
Cancelar
Guardar