-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Update Ping.Windows.cs for async task handling #122054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
191ab3e
15f12f9
7bd0a95
e4dfa7b
d096651
658d95b
e7d7713
bd38e20
92e3188
4641bb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ | |
| if (isAsync) | ||
| { | ||
| _taskCompletionSource = tcs = new TaskCompletionSource<PingReply>(); | ||
|
|
||
| } | ||
|
|
||
| _ipv6 = (address.AddressFamily == AddressFamily.InterNetworkV6); | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
| // Releases the unmanaged memory after ping completion. | ||
| private void FreeUnmanagedStructures() | ||
| { | ||
|
|
@@ -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
|
||
| ? (reply.roundTripTime, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
There was a problem hiding this comment.
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
SafeLocalAllocHandletype itself, as a part of the reduce unsafe theme.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to change?
There was a problem hiding this comment.
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.