Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,94 @@ private void FreeNativeMemory()
NativeMemory.Free(buffers);
}

private void Reserve(int count)
{
if (_handles.Length < count)
{
_handles = new MemoryHandle[count];
FreeNativeMemory();
_buffers = (QUIC_BUFFER*)NativeMemory.Alloc((nuint)count, (nuint)sizeof(QUIC_BUFFER));
}
}

private void SetBuffer(int index, ReadOnlyMemory<byte> buffer)
{
MemoryHandle handle = buffer.Pin();

_handles[index] = handle;
_buffers[index].Buffer = (byte*)handle.Pointer;
_buffers[index].Length = (uint)buffer.Length;
}

/// <summary>
/// The method initializes QUIC_BUFFER* with data from inputs, converted via toBuffer.
/// Initializes QUIC_BUFFER* with data from inputs, converted via toBuffer.
/// Note that the struct either needs to be freshly created via new or previously cleaned up with Reset.
/// </summary>
/// <param name="inputs">Inputs to get their byte array, pin them and pepare them to be passed to MsQuic as QUIC_BUFFER*.</param>
/// <param name="toBuffer">Method extracting byte array from the inputs, e.g. applicationProtocol.Protocol.</param>
/// <typeparam name="T">The type of the inputs.</typeparam>
public void Initialize<T>(IList<T> inputs, Func<T, ReadOnlyMemory<byte>> toBuffer)
{
if (_handles.Length < inputs.Count)
Reserve(inputs.Count);
_count = inputs.Count;

for (int i = 0; i < inputs.Count; ++i)
{
_handles = new MemoryHandle[inputs.Count];
ReadOnlyMemory<byte> buffer = toBuffer(inputs[i]);
SetBuffer(i, buffer);
}
if (_count < inputs.Count)
}

/// <summary>
/// Initializes QUIC_BUFFER* with the provided buffer.
/// Note that the struct either needs to be freshly created via new or previously cleaned up with Reset.
/// </summary>
/// <param name="buffer">Buffer to be passed to MsQuic as QUIC_BUFFER*.</param>
public void Initialize(ReadOnlyMemory<byte> buffer)
{
Reserve(1);
_count = 1;
SetBuffer(0, buffer);
}

/// <summary>
/// Initializes QUIC_BUFFER* with the provided buffers.
/// Note that the struct either needs to be freshly created via new or previously cleaned up with Reset.
/// </summary>
/// <param name="buffers">Buffers to be passed to MsQuic as QUIC_BUFFER*.</param>
public void Initialize(ReadOnlySequence<byte> buffers)
{
int count = 0;
foreach (ReadOnlyMemory<byte> _ in buffers)
{
FreeNativeMemory();
_buffers = (QUIC_BUFFER*)NativeMemory.Alloc((nuint)sizeof(QUIC_BUFFER), (nuint)inputs.Count);
++count;
}

_count = inputs.Count;
Reserve(count);
_count = count;
count = 0;

for (int i = 0; i < inputs.Count; ++i)
foreach (ReadOnlyMemory<byte> buffer in buffers)
{
ReadOnlyMemory<byte> buffer = toBuffer(inputs[i]);
MemoryHandle handle = buffer.Pin();
SetBuffer(count, buffer);
++count;
}
}

_handles[i] = handle;
_buffers[i].Buffer = (byte*)handle.Pointer;
_buffers[i].Length = (uint)buffer.Length;
/// <summary>
/// Initializes QUIC_BUFFER* with the provided buffers.
/// Note that the struct either needs to be freshly created via new or previously cleaned up with Reset.
/// </summary>
/// <param name="buffers">Buffers to be passed to MsQuic as QUIC_BUFFER*.</param>
public void Initialize(ReadOnlyMemory<ReadOnlyMemory<byte>> buffers)
{
int count = buffers.Length;
Reserve(count);
_count = count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I believe it is already part of Reserve(count)

Suggested change
_count = count;


for (int i = 0; i < buffers.Length; i++)
{
SetBuffer(i, buffers.Span[i]);
}
}

Expand Down
Loading