Skip to content

Commit

Permalink
MemoryOwner copy buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
nckex committed Jul 22, 2023
1 parent 35bff92 commit ce23b80
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion NetX/NetX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageProjectUrl>https://github.com/project-nevareth/NetX</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>2.2.2</Version>
<Version>2.2.3</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion NetX/NetXConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ private bool TryGetReceivedMessage(
var next = buffer.GetPosition(size);
buffer = buffer.Slice(next);

netXMessage = new NetXMessage(messageId, _options.CopyBuffer ? messageBuffer.ToArray() : messageBuffer);
var messageMemory = MemoryOwner<byte>.Allocate(messageBuffer.Length);
messageBuffer.CopyTo(messageMemory.Memory);

netXMessage = new NetXMessage(messageId, messageMemory);

return true;
}
Expand Down
17 changes: 13 additions & 4 deletions NetX/NetXMessage.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
using System;
using System.Buffers;
using CommunityToolkit.HighPerformance.Buffers;

namespace NetX
{
public readonly struct NetXMessage
public readonly struct NetXMessage : IDisposable
{
public Guid Id { get; }
public ReadOnlyMemory<byte> Buffer { get; }
public ReadOnlyMemory<byte> Buffer => _memoryOwner.Memory;

public NetXMessage(Guid id, ReadOnlyMemory<byte> buffer)
private readonly MemoryOwner<byte> _memoryOwner;

public NetXMessage(Guid id, MemoryOwner<byte> memoryOwner)
{
Id = id;
Buffer = buffer;
_memoryOwner = memoryOwner;
}

public void Dispose()
{
_memoryOwner.Dispose();
}
}
}
1 change: 0 additions & 1 deletion samples/ServerClientSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using NetX;
using NetX.Options;
using Serilog;
using static System.Net.Mime.MediaTypeNames;

namespace ServerClientSample
{
Expand Down

0 comments on commit ce23b80

Please sign in to comment.