Skip to content

Commit

Permalink
simplify WritePacketBuffer, remove unused helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mregen committed Jul 23, 2024
1 parent f9ca93a commit b791425
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 31 deletions.
9 changes: 2 additions & 7 deletions Source/MQTTnet.AspnetCore/MqttConnectionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.IO.Pipelines;
using System.Net;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -232,14 +233,8 @@ static void WritePacketBuffer(PipeWriter output, MqttPacketBuffer buffer)
var span = output.GetSpan(buffer.Length);

buffer.Packet.AsSpan().CopyTo(span);

int offset = buffer.Packet.Count;
foreach (var segment in buffer.Payload)
{
segment.Span.CopyTo(span.Slice(offset));
offset += segment.Length;
}

buffer.Payload.CopyTo(destination: span.Slice(offset));
output.Advance(buffer.Length);
}
}
24 changes: 0 additions & 24 deletions Source/MQTTnet/Internal/MqttMemoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,6 @@ public static void Copy(ReadOnlySequence<byte> sequence, int sourceIndex, byte[]
sequence.Slice(sourceIndex).CopyTo(destination.AsSpan(destinationIndex, length));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(ReadOnlySequence<byte> sequence, int sourceIndex, Memory<byte> destination, int destinationIndex, int length)
{
var offset = destinationIndex;
foreach (var segment in sequence)
{
if (segment.Length < sourceIndex)
{
sourceIndex -= segment.Length;
continue;
}

var targetLength = Math.Min(segment.Length - sourceIndex, length);
segment.Span.Slice(sourceIndex, targetLength).CopyTo(destination.Span.Slice(offset));
offset += targetLength;
length -= targetLength;
if (length == 0)
{
break;
}
sourceIndex = 0;
}
}

public static bool SequenceEqual(ArraySegment<byte> source, ArraySegment<byte> target)
{
return source.AsSpan().SequenceEqual(target);
Expand Down

0 comments on commit b791425

Please sign in to comment.