Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
if (isAsync)
{
_taskCompletionSource = tcs = new TaskCompletionSource<PingReply>();

}

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

// 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)
{
dst[i] = buffer[i];
}
Span<byte> destination = new Span<byte>(_requestBuffer.DangerousGetHandle().ToPointer(), buffer.Length);
buffer.CopyTo(destination);
Comment on lines +312 to +313
Copy link
Member

Choose a reason for hiding this comment

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

While the change is valid, we may do this better with the SafeLocalAllocHandle type itself, as a part of the reduce unsafe theme.

Copy link
Author

Choose a reason for hiding this comment

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

how to change?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's any thing can be done correctly. This change is the only valid piece in this PR.

}

// Releases the unmanaged memory after ping completion.
private void FreeUnmanagedStructures()
{
Expand Down Expand Up @@ -341,29 +339,24 @@
{
const int DontFragmentFlag = 2;

IPAddress address = new IPAddress(reply.address);
IPStatus ipStatus = GetStatusFromCode((int)reply.status);
IPAddress address = new IPAddress(reply.address);
IPStatus ipStatus = GetStatusFromCode((int)reply.status);

long rtt;
PingOptions? options;
byte[] buffer;
var (rtt, options, buffer) = ipStatus == IPStatus.Success

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,42): error CS0173: (NETCORE_ENGINEERING_TELEMETRY=Build) Type of conditional expression cannot be determined because there is no implicit conversion between '(uint, System.Net.NetworkInformation.PingOptions, byte[])' and '(long, <null>, byte[])'

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,42): error CS8131: (NETCORE_ENGINEERING_TELEMETRY=Build) Deconstruct assignment requires an expression with a type on the right-hand-side.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,18): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'rtt'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,23): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'options'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,32): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'buffer'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,42): error CS0173: (NETCORE_ENGINEERING_TELEMETRY=Build) Type of conditional expression cannot be determined because there is no implicit conversion between '(uint, System.Net.NetworkInformation.PingOptions, byte[])' and '(long, <null>, byte[])'

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,42): error CS8131: (NETCORE_ENGINEERING_TELEMETRY=Build) Deconstruct assignment requires an expression with a type on the right-hand-side.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,18): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'rtt'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,23): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'options'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,32): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'buffer'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,42): error CS0173: (NETCORE_ENGINEERING_TELEMETRY=Build) Type of conditional expression cannot be determined because there is no implicit conversion between '(uint, System.Net.NetworkInformation.PingOptions, byte[])' and '(long, <null>, byte[])'

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,42): error CS8131: (NETCORE_ENGINEERING_TELEMETRY=Build) Deconstruct assignment requires an expression with a type on the right-hand-side.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,18): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'rtt'.

Check failure on line 345 in src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs#L345

src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs(345,23): error CS8130: (NETCORE_ENGINEERING_TELEMETRY=Build) Cannot infer the type of implicitly-typed deconstruction variable 'options'.
? (reply.roundTripTime,
Copy link
Member

Choose a reason for hiding this comment

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

Using ?: here is much less readable.

new PingOptions(reply.options.ttl, (reply.options.flags & DontFragmentFlag) > 0),
CreateBuffer(reply.data, reply.dataSize))
: (0L, null, Array.Empty<byte>());

if (ipStatus == IPStatus.Success)
{
// Only copy the data if we succeed w/ the ping operation.
rtt = reply.roundTripTime;
options = new PingOptions(reply.options.ttl, (reply.options.flags & DontFragmentFlag) > 0);
buffer = new byte[reply.dataSize];
Marshal.Copy(reply.data, buffer, 0, reply.dataSize);
}
else
return new PingReply(address, options, ipStatus, rtt, buffer);

static byte[] CreateBuffer(IntPtr data, int dataSize)
{
rtt = 0;
options = null;
buffer = Array.Empty<byte>();
Span<byte> bufferSpan = stackalloc byte[dataSize];
Marshal.Copy(data, bufferSpan, 0, dataSize);

return bufferSpan.ToArray();
}
Comment on lines +353 to 359
Copy link
Member

Choose a reason for hiding this comment

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

This method is pure regression. It's still allocating a new array, and copying the data twice with a temporary span.


return new PingReply(address, options, ipStatus, rtt, buffer);
}

private static PingReply CreatePingReplyFromIcmp6EchoReply(in Interop.IpHlpApi.ICMPV6_ECHO_REPLY reply, IntPtr dataPtr, int sendSize)
Expand Down
Loading