Skip to content

Commit

Permalink
some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mregen committed Jun 26, 2024
1 parent 8852e7b commit d60c7c0
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat
}
}

applicationMessage.DisposePayload();
applicationMessage.Dispose();
}
catch (Exception exception)
{
Expand Down
3 changes: 1 addition & 2 deletions Source/MQTTnet.TestApp/PublicBrokerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
3 changes: 1 addition & 2 deletions Source/MQTTnet.Tests/Clients/MqttClient/MqttClient_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions Source/MQTTnet.Tests/MQTTv5/Client_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -282,7 +281,7 @@ public async Task Publish_And_Receive_New_Properties()
}
finally
{
receivedMessage?.DisposePayload();
receivedMessage?.Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void Dispose()
{
foreach (var eventArgs in _receivedEventArgs)
{
eventArgs.ApplicationMessage?.DisposePayload();
eventArgs.ApplicationMessage?.Dispose();
}
}

Expand Down Expand Up @@ -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);
}

Expand Down
12 changes: 12 additions & 0 deletions Source/MQTTnet/Buffers/MqttPayloadOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public void Dispose()
_owner = null;
}

/// <summary>
/// Returns a new <see cref="MqttPayloadOwner{T}"/> with the same
/// <see cref="ReadOnlySequence{T}"/> and transfers the ownership
/// to the caller.
/// </summary>
public MqttPayloadOwner<T> TransferOwnership()
{
var payload = new MqttPayloadOwner<T>(_sequence, _owner);
_owner = null;
return payload;
}

public static implicit operator MqttPayloadOwner<T>(ArrayPoolMemoryOwner<T> memoryOwner) => new MqttPayloadOwner<T>(memoryOwner.Memory, memoryOwner);
public static implicit operator MqttPayloadOwner<T>(ReadOnlySequence<T> sequence) => new MqttPayloadOwner<T>(sequence);
public static implicit operator MqttPayloadOwner<T>(ReadOnlyMemory<T> memory) => new MqttPayloadOwner<T>(memory);
Expand Down
4 changes: 2 additions & 2 deletions Source/MQTTnet/Client/MqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ async Task ProcessReceivedPublishPackets(CancellationToken cancellationToken)
}
finally
{
if (eventArgs?.TransferredPayload == false)
if (eventArgs?.DisposeApplicationMessage == true)
{
eventArgs.ApplicationMessage?.DisposePayload();
eventArgs.ApplicationMessage?.Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,29 @@ public MqttApplicationMessageReceivedEventArgs(
}

/// <summary>
/// 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 <see cref="MqttApplicationMessage.DisposePayload()"/>.
/// 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.
/// </summary>
/// <param name="clonePayload">
/// 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.
/// </param>
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; }
Expand All @@ -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.
/// </summary>
public bool TransferredPayload { get; private set; } = false;
public bool DisposeApplicationMessage { get; private set; } = true;

/// <summary>
/// Gets or sets whether this message was handled.
Expand Down
3 changes: 2 additions & 1 deletion Source/MQTTnet/MQTTnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
<NuGetAuditMode>all</NuGetAuditMode>
<NuGetAudit>true</NuGetAudit>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DebugType>Full</DebugType>
<DebugType>portable</DebugType>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
8 changes: 4 additions & 4 deletions Source/MQTTnet/MqttApplicationMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace MQTTnet
{
public sealed class MqttApplicationMessage
public sealed class MqttApplicationMessage : IDisposable
{
/// <summary>
/// Create a clone of the <see cref="MqttApplicationMessage"/>
/// with a deep copy of the Payload which is cleaned up by the GC.
/// Create a clone of the <see cref="MqttApplicationMessage"/>.
/// with a deep copy of the Payload allocated from the heap.
/// </summary>
public MqttApplicationMessage Clone()
{
Expand All @@ -40,7 +40,7 @@ public MqttApplicationMessage Clone()
/// <summary>
/// Disposes the payload used by the current instance of the <see cref="MqttApplicationMessage" /> class.
/// </summary>
public void DisposePayload()
public void Dispose()
{
Payload.Dispose();
}
Expand Down

0 comments on commit d60c7c0

Please sign in to comment.