Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Publish stored messages using Enumerator#2018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
nedimtokic wants to merge1 commit intodotnet:release/4.x.x
base:release/4.x.x
Choose a base branch
Loading
fromnedimtokic:mqttnet-load-queued-messages
Draft
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace MQTTnet.Extensions.ManagedClient
Expand All@@ -11,6 +12,6 @@ public interface IManagedMqttClientStorage
{
Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages);

Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync();
IAsyncEnumerable<ManagedMqttApplicationMessage> LoadQueuedMessagesAsync(CancellationToken cancellationToken = default);
}
}
}
60 changes: 46 additions & 14 deletionsSource/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -249,17 +249,6 @@ public async Task StartAsync(ManagedMqttClientOptions options)

Options = options;

if (options.Storage != null)
{
_storageManager = new ManagedMqttClientStorageManager(options.Storage);
var messages = await _storageManager.LoadQueuedMessagesAsync().ConfigureAwait(false);

foreach (var message in messages)
{
_messageQueue.Enqueue(message);
}
}

var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
_connectionCancellationToken = cancellationTokenSource;
Expand DownExpand Up@@ -444,6 +433,48 @@ async Task MaintainConnectionAsync(CancellationToken cancellationToken)
}
}

async Task PublishStoredMessagesAsync(CancellationToken cancellationToken)
{


try
{
_storageManager = new ManagedMqttClientStorageManager(Options.Storage);

while (!cancellationToken.IsCancellationRequested && InternalClient.IsConnected)
{

cancellationToken.ThrowIfCancellationRequested();

if (Options.Storage != null)
{

await foreach (var msg in _storageManager.LoadQueuedMessagesAsync().ConfigureAwait(false))
{

await EnqueueAsync(msg);

cancellationToken.ThrowIfCancellationRequested();

await TryPublishQueuedMessageAsync(msg, cancellationToken).ConfigureAwait(false);
}
}

}
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
_logger.Error(exception, "Error while publishing stored application messages.");
}
finally
{
_logger.Verbose("Stopped publishing stored messages.");
}
}

async Task PublishQueuedMessagesAsync(CancellationToken cancellationToken)
{
try
Expand DownExpand Up@@ -665,14 +696,15 @@ async Task<SendSubscribeUnsubscribeResult> SendSubscribeUnsubscribe(List<MqttTop
return new SendSubscribeUnsubscribeResult(subscribeResults, unsubscribeResults);
}

void StartPublishing()
async Task StartPublishing()
{
StopPublishing();

var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
_publishingCancellationToken = cancellationTokenSource;

await PublishStoredMessagesAsync(cancellationToken);
Task.Run(() => PublishQueuedMessagesAsync(cancellationToken), cancellationToken).RunInBackground(_logger);
}

Expand DownExpand Up@@ -717,11 +749,11 @@ async Task TryMaintainConnectionAsync(CancellationToken cancellationToken)
else if (connectionState == ReconnectionResult.Reconnected)
{
await PublishReconnectSubscriptionsAsync(cancellationToken).ConfigureAwait(false);
StartPublishing();
awaitStartPublishing();
}
else if (connectionState == ReconnectionResult.Recovered)
{
StartPublishing();
awaitStartPublishing();
}
else if (connectionState == ReconnectionResult.StillConnected)
{
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Internal;
Expand All@@ -22,12 +23,13 @@ public ManagedMqttClientStorageManager(IManagedMqttClientStorage storage)
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
}

public asyncTask<List<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
public asyncIAsyncEnumerable<ManagedMqttApplicationMessage> LoadQueuedMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var loadedMessages = await _storage.LoadQueuedMessagesAsync().ConfigureAwait(false);
_messages.AddRange(loadedMessages);

return _messages;
await foreach (var message in _storage.LoadQueuedMessagesAsync(cancellationToken).ConfigureAwait(false))
{
_messages.Add(message);
yield return message;
}
}

public async Task AddAsync(ManagedMqttApplicationMessage applicationMessage)
Expand DownExpand Up@@ -63,4 +65,4 @@ private Task SaveAsync()
return _storage.SaveQueuedMessagesAsync(_messages);
}
}
}
}

[8]ページ先頭

©2009-2025 Movatter.jp