Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private Task<PingReply> DoSendPingCore(IPAddress address, byte[] buffer, int tim
TaskCompletionSource<PingReply>? tcs = null;
if (isAsync)
{
_taskCompletionSource = tcs = new TaskCompletionSource<PingReply>();
_taskCompletionSource = tcs = new TaskCompletionSource<PingReply>(TaskCreationOptions.RunContinuationsAsynchronously);

}

_ipv6 = (address.AddressFamily == AddressFamily.InterNetworkV6);
Expand Down Expand Up @@ -303,17 +304,18 @@ private void PingCallback()
}
}

// Copies _requestBuffer into unmanaged memory for async icmpsendecho APIs.
// Copies _requestBuffer into unmanaged memory for async icmpsendecho APIs
private unsafe void SetUnmanagedStructures(byte[] buffer)
{
_requestBuffer?.Dispose();
_requestBuffer = SafeLocalAllocHandle.LocalAlloc(buffer.Length);
byte* dst = (byte*)_requestBuffer.DangerousGetHandle();
for (int i = 0; i < buffer.Length; ++i)
fixed (byte* sourcePtr = buffer)
{
dst[i] = buffer[i];
Span<byte> source = new Span<byte>(sourcePtr, buffer.Length);
Span<byte> destination = new Span<byte>(_requestBuffer.DangerousGetHandle().ToPointer(), buffer.Length);
source.CopyTo(destination);
}
}

// Releases the unmanaged memory after ping completion.
private void FreeUnmanagedStructures()
{
Expand Down
Loading