From d60c7c0a3fe8bf3b2d339604e55e9a1b4b61961c Mon Sep 17 00:00:00 2001 From: Martin Regen Date: Wed, 26 Jun 2024 07:24:08 +0200 Subject: [PATCH] some renaming --- .../Internal/MqttRetainedMessagesManager.cs | 2 +- Source/MQTTnet.TestApp/PublicBrokerTest.cs | 3 +-- .../Clients/MqttClient/MqttClient_Tests.cs | 3 +-- Source/MQTTnet.Tests/MQTTv5/Client_Tests.cs | 5 ++-- .../TestApplicationMessageReceivedHandler.cs | 6 ++--- Source/MQTTnet/Buffers/MqttPayloadOwner.cs | 12 +++++++++ Source/MQTTnet/Client/MqttClient.cs | 4 +-- ...MqttApplicationMessageReceivedEventArgs.cs | 25 +++++++++++-------- Source/MQTTnet/MQTTnet.csproj | 3 ++- Source/MQTTnet/MqttApplicationMessage.cs | 8 +++--- 10 files changed, 42 insertions(+), 29 deletions(-) diff --git a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs index df2aa72f4..1159a2fcf 100644 --- a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs +++ b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs @@ -110,7 +110,7 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat } } - applicationMessage.DisposePayload(); + applicationMessage.Dispose(); } catch (Exception exception) { diff --git a/Source/MQTTnet.TestApp/PublicBrokerTest.cs b/Source/MQTTnet.TestApp/PublicBrokerTest.cs index 3e9e326fd..d1b0cf462 100644 --- a/Source/MQTTnet.TestApp/PublicBrokerTest.cs +++ b/Source/MQTTnet.TestApp/PublicBrokerTest.cs @@ -144,8 +144,7 @@ static async Task ExecuteTestAsync(string name, MqttClientOptions options) MqttApplicationMessage receivedMessage = null; client.ApplicationMessageReceivedAsync += e => { - e.TransferPayload(true); - receivedMessage = e.ApplicationMessage; + receivedMessage = e.TransferApplicationMessageOwnership(true); return CompletedTask.Instance; }; diff --git a/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs b/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs index ecb1e518c..0d1f5c593 100644 --- a/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs +++ b/Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs @@ -870,8 +870,7 @@ public async Task Subscribe_In_Callback_Events() { lock (receivedMessages) { - e.TransferPayload(true); - receivedMessages.Add(e.ApplicationMessage); + receivedMessages.Add(e.TransferApplicationMessageOwnership(true)); } return CompletedTask.Instance; diff --git a/Source/MQTTnet.Tests/MQTTv5/Client_Tests.cs b/Source/MQTTnet.Tests/MQTTv5/Client_Tests.cs index 8cd119f2f..1c508d1bc 100644 --- a/Source/MQTTnet.Tests/MQTTv5/Client_Tests.cs +++ b/Source/MQTTnet.Tests/MQTTv5/Client_Tests.cs @@ -244,8 +244,7 @@ public async Task Publish_And_Receive_New_Properties() MqttApplicationMessage receivedMessage = null; receiver.ApplicationMessageReceivedAsync += e => { - e.TransferPayload(false); - receivedMessage = e.ApplicationMessage; + receivedMessage = e.TransferApplicationMessageOwnership(true); return CompletedTask.Instance; }; @@ -282,7 +281,7 @@ public async Task Publish_And_Receive_New_Properties() } finally { - receivedMessage?.DisposePayload(); + receivedMessage?.Dispose(); } } } diff --git a/Source/MQTTnet.Tests/Mockups/TestApplicationMessageReceivedHandler.cs b/Source/MQTTnet.Tests/Mockups/TestApplicationMessageReceivedHandler.cs index b9d8d07bb..679815b7d 100644 --- a/Source/MQTTnet.Tests/Mockups/TestApplicationMessageReceivedHandler.cs +++ b/Source/MQTTnet.Tests/Mockups/TestApplicationMessageReceivedHandler.cs @@ -21,7 +21,7 @@ public void Dispose() { foreach (var eventArgs in _receivedEventArgs) { - eventArgs.ApplicationMessage?.DisposePayload(); + eventArgs.ApplicationMessage?.Dispose(); } } @@ -84,8 +84,8 @@ Task OnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs e { lock (_receivedEventArgs) { - // take ownership of message payload to avoid cloning - eventArgs.TransferPayload(false); + // take ownership of application message to avoid cloning + eventArgs.TransferApplicationMessageOwnership(false); _receivedEventArgs.Add(eventArgs); } diff --git a/Source/MQTTnet/Buffers/MqttPayloadOwner.cs b/Source/MQTTnet/Buffers/MqttPayloadOwner.cs index 62e8a51a9..efd59674b 100644 --- a/Source/MQTTnet/Buffers/MqttPayloadOwner.cs +++ b/Source/MQTTnet/Buffers/MqttPayloadOwner.cs @@ -72,6 +72,18 @@ public void Dispose() _owner = null; } + /// + /// Returns a new with the same + /// and transfers the ownership + /// to the caller. + /// + public MqttPayloadOwner TransferOwnership() + { + var payload = new MqttPayloadOwner(_sequence, _owner); + _owner = null; + return payload; + } + public static implicit operator MqttPayloadOwner(ArrayPoolMemoryOwner memoryOwner) => new MqttPayloadOwner(memoryOwner.Memory, memoryOwner); public static implicit operator MqttPayloadOwner(ReadOnlySequence sequence) => new MqttPayloadOwner(sequence); public static implicit operator MqttPayloadOwner(ReadOnlyMemory memory) => new MqttPayloadOwner(memory); diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 376493de0..857d10343 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -720,9 +720,9 @@ async Task ProcessReceivedPublishPackets(CancellationToken cancellationToken) } finally { - if (eventArgs?.TransferredPayload == false) + if (eventArgs?.DisposeApplicationMessage == true) { - eventArgs.ApplicationMessage?.DisposePayload(); + eventArgs.ApplicationMessage?.Dispose(); } } } diff --git a/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs b/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs index 49e34cca3..0d37fd55f 100644 --- a/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs +++ b/Source/MQTTnet/Client/Receiving/MqttApplicationMessageReceivedEventArgs.cs @@ -29,26 +29,29 @@ public MqttApplicationMessageReceivedEventArgs( } /// - /// The invoked message receiver can take ownership of the payload to avoid cloning. - /// If not cloned, it is the obligation of the new owner to dispose the payload by - /// calling . + /// The invoked message receiver can take ownership of the application + /// message with payload to avoid cloning. + /// It is then the obligation of the new owner to dispose the obtained + /// application message. /// /// - /// If set to true, clones the applicationMessage and copies the payload. - /// The new instance does not need to be disposed. + /// If set to true, clones the ApplicationMessage and copies the payload. /// - public void TransferPayload(bool clonePayload) + public MqttApplicationMessage TransferApplicationMessageOwnership(bool clonePayload) { - TransferredPayload = true; + DisposeApplicationMessage = false; if (clonePayload) { var applicationMessage = ApplicationMessage; - if (applicationMessage != null) + // replace application message with a clone + // if the payload is owner managed + if (applicationMessage?.Payload.Owner != null) { ApplicationMessage = applicationMessage.Clone(); - applicationMessage.DisposePayload(); + applicationMessage.Dispose(); } } + return ApplicationMessage; } public MqttApplicationMessage ApplicationMessage { get; private set; } @@ -68,10 +71,10 @@ public void TransferPayload(bool clonePayload) /// Gets or sets whether the ownership of the message payload /// was handed over to the invoked code. This value determines /// if the payload can be disposed after the callback returns. - /// If transferred, the new owner is responsible + /// If transferred, the new owner of the message is responsible /// to dispose the payload after processing. /// - public bool TransferredPayload { get; private set; } = false; + public bool DisposeApplicationMessage { get; private set; } = true; /// /// Gets or sets whether this message was handled. diff --git a/Source/MQTTnet/MQTTnet.csproj b/Source/MQTTnet/MQTTnet.csproj index b97c68faa..fb3e0e5c6 100644 --- a/Source/MQTTnet/MQTTnet.csproj +++ b/Source/MQTTnet/MQTTnet.csproj @@ -44,10 +44,11 @@ all true latest-Recommended + latest - Full + portable diff --git a/Source/MQTTnet/MqttApplicationMessage.cs b/Source/MQTTnet/MqttApplicationMessage.cs index f70bd62be..855eb2f06 100644 --- a/Source/MQTTnet/MqttApplicationMessage.cs +++ b/Source/MQTTnet/MqttApplicationMessage.cs @@ -11,11 +11,11 @@ namespace MQTTnet { - public sealed class MqttApplicationMessage + public sealed class MqttApplicationMessage : IDisposable { /// - /// Create a clone of the - /// with a deep copy of the Payload which is cleaned up by the GC. + /// Create a clone of the . + /// with a deep copy of the Payload allocated from the heap. /// public MqttApplicationMessage Clone() { @@ -40,7 +40,7 @@ public MqttApplicationMessage Clone() /// /// Disposes the payload used by the current instance of the class. /// - public void DisposePayload() + public void Dispose() { Payload.Dispose(); }