mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MQTTnet.Internal;
|
|
|
|
namespace MQTTnet.Extensions.ManagedClient
|
|
{
|
|
public class ManagedMqttClientStorageManager
|
|
{
|
|
private readonly List<ManagedMqttApplicationMessage> _messages = new List<ManagedMqttApplicationMessage>();
|
|
private readonly AsyncLock _messagesLock = new AsyncLock();
|
|
|
|
private readonly IManagedMqttClientStorage _storage;
|
|
|
|
public ManagedMqttClientStorageManager(IManagedMqttClientStorage storage)
|
|
{
|
|
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
|
|
}
|
|
|
|
public async Task<List<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
|
|
{
|
|
var loadedMessages = await _storage.LoadQueuedMessagesAsync().ConfigureAwait(false);
|
|
_messages.AddRange(loadedMessages);
|
|
|
|
return _messages;
|
|
}
|
|
|
|
public async Task AddAsync(ManagedMqttApplicationMessage applicationMessage)
|
|
{
|
|
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
|
|
|
|
using (await _messagesLock.WaitAsync(CancellationToken.None).ConfigureAwait(false))
|
|
{
|
|
_messages.Add(applicationMessage);
|
|
await SaveAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
public async Task RemoveAsync(ManagedMqttApplicationMessage applicationMessage)
|
|
{
|
|
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
|
|
|
|
using (await _messagesLock.WaitAsync(CancellationToken.None).ConfigureAwait(false))
|
|
{
|
|
var index = _messages.IndexOf(applicationMessage);
|
|
if (index == -1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_messages.RemoveAt(index);
|
|
await SaveAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
private Task SaveAsync()
|
|
{
|
|
return _storage.SaveQueuedMessagesAsync(_messages);
|
|
}
|
|
}
|
|
}
|