-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingExtensions.cs
204 lines (190 loc) · 11.5 KB
/
PingExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//--------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: PingExtensions.cs
//
//--------------------------------------------------------------------------
using System.Threading.Tasks;
namespace System.Net.NetworkInformation
{
/// <summary>Extension methods for working with Ping asynchronously.</summary>
public static class PingExtensions
{
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="address">An IPAddress that identifies the computer that is the destination for the ICMP echo message.</param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, IPAddress address, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="hostNameOrAddress">
/// A String that identifies the computer that is the destination for the ICMP echo message.
/// The value specified for this parameter can be a host name or a string representation of an IP address.
/// </param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="address">An IPAddress that identifies the computer that is the destination for the ICMP echo message.</param>
/// <param name="timeout">
/// An Int32 value that specifies the maximum number of milliseconds (after sending the echo message)
/// to wait for the ICMP echo reply message.
/// </param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="hostNameOrAddress">
/// A String that identifies the computer that is the destination for the ICMP echo message.
/// The value specified for this parameter can be a host name or a string representation of an IP address.
/// </param>
/// <param name="timeout">
/// An Int32 value that specifies the maximum number of milliseconds (after sending the echo message)
/// to wait for the ICMP echo reply message.
/// </param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="address">An IPAddress that identifies the computer that is the destination for the ICMP echo message.</param>
/// <param name="timeout">
/// An Int32 value that specifies the maximum number of milliseconds (after sending the echo message)
/// to wait for the ICMP echo reply message.
/// </param>
/// <param name="buffer">
/// A Byte array that contains data to be sent with the ICMP echo message and returned
/// in the ICMP echo reply message. The array cannot contain more than 65,500 bytes.
/// </param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, byte[] buffer, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, buffer, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="hostNameOrAddress">
/// A String that identifies the computer that is the destination for the ICMP echo message.
/// The value specified for this parameter can be a host name or a string representation of an IP address.
/// </param>
/// <param name="timeout">
/// An Int32 value that specifies the maximum number of milliseconds (after sending the echo message)
/// to wait for the ICMP echo reply message.
/// </param>
/// <param name="buffer">
/// A Byte array that contains data to be sent with the ICMP echo message and returned
/// in the ICMP echo reply message. The array cannot contain more than 65,500 bytes.
/// </param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, buffer, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="address">An IPAddress that identifies the computer that is the destination for the ICMP echo message.</param>
/// <param name="timeout">
/// An Int32 value that specifies the maximum number of milliseconds (after sending the echo message)
/// to wait for the ICMP echo reply message.
/// </param>
/// <param name="buffer">
/// A Byte array that contains data to be sent with the ICMP echo message and returned
/// in the ICMP echo reply message. The array cannot contain more than 65,500 bytes.
/// </param>
/// <param name="options">A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.</param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, byte[] buffer, PingOptions options, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, buffer, options, tcs));
}
/// <summary>
/// Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message.
/// </summary>
/// <param name="ping">The Ping.</param>
/// <param name="hostNameOrAddress">
/// A String that identifies the computer that is the destination for the ICMP echo message.
/// The value specified for this parameter can be a host name or a string representation of an IP address.
/// </param>
/// <param name="timeout">
/// An Int32 value that specifies the maximum number of milliseconds (after sending the echo message)
/// to wait for the ICMP echo reply message.
/// </param>
/// <param name="buffer">
/// A Byte array that contains data to be sent with the ICMP echo message and returned
/// in the ICMP echo reply message. The array cannot contain more than 65,500 bytes.
/// </param>
/// <param name="options">A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.</param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options, object userToken)
{
return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, buffer, options, tcs));
}
/// <summary>The core implementation of SendTask.</summary>
/// <param name="ping">The Ping.</param>
/// <param name="userToken">A user-defined object stored in the resulting Task.</param>
/// <param name="sendAsync">
/// A delegate that initiates the asynchronous send.
/// The provided TaskCompletionSource must be passed as the user-supplied state to the actual Ping.SendAsync method.
/// </param>
/// <returns></returns>
private static Task<PingReply> SendTaskCore(Ping ping, object userToken, Action<TaskCompletionSource<PingReply>> sendAsync)
{
// Validate we're being used with a real smtpClient. The rest of the arg validation
// will happen in the call to sendAsync.
if (ping == null) throw new ArgumentNullException("ping");
// Create a TaskCompletionSource to represent the operation
var tcs = new TaskCompletionSource<PingReply>(userToken);
// Register a handler that will transfer completion results to the TCS Task
PingCompletedEventHandler handler = null;
handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Reply, () => ping.PingCompleted -= handler);
ping.PingCompleted += handler;
// Try to start the async operation. If starting it fails (due to parameter validation)
// unregister the handler before allowing the exception to propagate.
try
{
sendAsync(tcs);
}
catch (Exception exc)
{
ping.PingCompleted -= handler;
tcs.TrySetException(exc);
}
// Return the task to represent the asynchronous operation
return tcs.Task;
}
}
}