mscorlib(4.0.0.0) API with additions
Ping.cs
2 using System.Net.Sockets;
5 using System.Threading;
7 
9 {
11  public class Ping : Component
12  {
13  internal class AsyncStateObject
14  {
15  internal byte[] buffer;
16 
17  internal string hostName;
18 
19  internal int timeout;
20 
21  internal PingOptions options;
22 
23  internal object userToken;
24 
25  internal AsyncStateObject(string hostName, byte[] buffer, int timeout, PingOptions options, object userToken)
26  {
27  this.hostName = hostName;
28  this.buffer = buffer;
29  this.timeout = timeout;
30  this.options = options;
31  this.userToken = userToken;
32  }
33  }
34 
35  private const int MaxUdpPacket = 65791;
36 
37  private const int MaxBufferSize = 65500;
38 
39  private const int DefaultTimeout = 5000;
40 
41  private const int DefaultSendBufferSize = 32;
42 
43  private byte[] defaultSendBuffer;
44 
45  private bool ipv6;
46 
47  private bool cancelled;
48 
49  private bool disposeRequested;
50 
51  private object lockObject = new object();
52 
53  internal ManualResetEvent pingEvent;
54 
55  private RegisteredWaitHandle registeredWait;
56 
57  private SafeLocalFree requestBuffer;
58 
59  private SafeLocalFree replyBuffer;
60 
61  private int sendSize;
62 
63  private SafeCloseIcmpHandle handlePingV4;
64 
65  private SafeCloseIcmpHandle handlePingV6;
66 
67  private AsyncOperation asyncOp;
68 
69  private SendOrPostCallback onPingCompletedDelegate;
70 
71  private ManualResetEvent asyncFinished;
72 
73  private const int Free = 0;
74 
75  private const int InProgress = 1;
76 
77  private new const int Disposed = 2;
78 
79  private int status;
80 
81  private bool InAsyncCall
82  {
83  get
84  {
85  if (asyncFinished == null)
86  {
87  return false;
88  }
89  return !asyncFinished.WaitOne(0);
90  }
91  set
92  {
93  if (asyncFinished == null)
94  {
95  asyncFinished = new ManualResetEvent(!value);
96  }
97  else if (value)
98  {
99  asyncFinished.Reset();
100  }
101  else
102  {
103  asyncFinished.Set();
104  }
105  }
106  }
107 
108  private byte[] DefaultSendBuffer
109  {
110  get
111  {
112  if (defaultSendBuffer == null)
113  {
114  defaultSendBuffer = new byte[32];
115  for (int i = 0; i < 32; i++)
116  {
117  defaultSendBuffer[i] = (byte)(97 + i % 23);
118  }
119  }
120  return defaultSendBuffer;
121  }
122  }
123 
126 
127  private void CheckStart(bool async)
128  {
129  if (disposeRequested)
130  {
131  throw new ObjectDisposedException(GetType().FullName);
132  }
133  switch (Interlocked.CompareExchange(ref status, 1, 0))
134  {
135  case 1:
136  throw new InvalidOperationException(SR.GetString("net_inasync"));
137  case 2:
138  throw new ObjectDisposedException(GetType().FullName);
139  }
140  if (async)
141  {
142  InAsyncCall = true;
143  }
144  }
145 
146  private void Finish(bool async)
147  {
148  status = 0;
149  if (async)
150  {
151  InAsyncCall = false;
152  }
153  if (disposeRequested)
154  {
155  InternalDispose();
156  }
157  }
158 
162  {
163  if (this.PingCompleted != null)
164  {
165  this.PingCompleted(this, e);
166  }
167  }
168 
169  private void PingCompletedWaitCallback(object operationState)
170  {
171  OnPingCompleted((PingCompletedEventArgs)operationState);
172  }
173 
175  public Ping()
176  {
177  onPingCompletedDelegate = PingCompletedWaitCallback;
178  }
179 
180  private void InternalDispose()
181  {
182  disposeRequested = true;
183  if (Interlocked.CompareExchange(ref status, 2, 0) == 0)
184  {
185  if (handlePingV4 != null)
186  {
187  handlePingV4.Close();
188  handlePingV4 = null;
189  }
190  if (handlePingV6 != null)
191  {
192  handlePingV6.Close();
193  handlePingV6 = null;
194  }
195  UnregisterWaitHandle();
196  if (pingEvent != null)
197  {
198  pingEvent.Close();
199  pingEvent = null;
200  }
201  if (replyBuffer != null)
202  {
203  replyBuffer.Close();
204  replyBuffer = null;
205  }
206  if (asyncFinished != null)
207  {
208  asyncFinished.Close();
209  asyncFinished = null;
210  }
211  }
212  }
213 
214  private void UnregisterWaitHandle()
215  {
216  lock (lockObject)
217  {
218  if (registeredWait != null)
219  {
220  registeredWait.Unregister(null);
221  registeredWait = null;
222  }
223  }
224  }
225 
229  protected override void Dispose(bool disposing)
230  {
231  if (disposing)
232  {
233  InternalDispose();
234  }
235  base.Dispose(disposing);
236  }
237 
239  public void SendAsyncCancel()
240  {
241  lock (lockObject)
242  {
243  if (!InAsyncCall)
244  {
245  return;
246  }
247  cancelled = true;
248  }
249  asyncFinished.WaitOne();
250  }
251 
252  private static void PingCallback(object state, bool signaled)
253  {
254  Ping ping = (Ping)state;
255  PingCompletedEventArgs arg = null;
256  bool flag = false;
257  AsyncOperation asyncOperation = null;
258  SendOrPostCallback d = null;
259  try
260  {
261  lock (ping.lockObject)
262  {
263  flag = ping.cancelled;
264  asyncOperation = ping.asyncOp;
265  d = ping.onPingCompletedDelegate;
266  if (!flag)
267  {
268  SafeLocalFree safeLocalFree = ping.replyBuffer;
269  PingReply reply2;
270  if (ping.ipv6)
271  {
272  Icmp6EchoReply reply = (Icmp6EchoReply)Marshal.PtrToStructure(safeLocalFree.DangerousGetHandle(), typeof(Icmp6EchoReply));
273  reply2 = new PingReply(reply, safeLocalFree.DangerousGetHandle(), ping.sendSize);
274  }
275  else
276  {
277  IcmpEchoReply reply3 = (IcmpEchoReply)Marshal.PtrToStructure(safeLocalFree.DangerousGetHandle(), typeof(IcmpEchoReply));
278  reply2 = new PingReply(reply3);
279  }
280  arg = new PingCompletedEventArgs(reply2, null, cancelled: false, asyncOperation.UserSuppliedState);
281  }
282  else
283  {
284  arg = new PingCompletedEventArgs(null, null, cancelled: true, asyncOperation.UserSuppliedState);
285  }
286  }
287  }
288  catch (Exception innerException)
289  {
290  PingException error = new PingException(SR.GetString("net_ping"), innerException);
291  arg = new PingCompletedEventArgs(null, error, cancelled: false, asyncOperation.UserSuppliedState);
292  }
293  finally
294  {
295  ping.FreeUnmanagedStructures();
296  ping.UnregisterWaitHandle();
297  ping.Finish(async: true);
298  }
299  asyncOperation.PostOperationCompleted(d, arg);
300  }
301 
312  public PingReply Send(string hostNameOrAddress)
313  {
314  return Send(hostNameOrAddress, 5000, DefaultSendBuffer, null);
315  }
316 
328  public PingReply Send(string hostNameOrAddress, int timeout)
329  {
330  return Send(hostNameOrAddress, timeout, DefaultSendBuffer, null);
331  }
332 
343  public PingReply Send(IPAddress address)
344  {
345  return Send(address, 5000, DefaultSendBuffer, null);
346  }
347 
361  public PingReply Send(IPAddress address, int timeout)
362  {
363  return Send(address, timeout, DefaultSendBuffer, null);
364  }
365 
380  public PingReply Send(string hostNameOrAddress, int timeout, byte[] buffer)
381  {
382  return Send(hostNameOrAddress, timeout, buffer, null);
383  }
384 
401  public PingReply Send(IPAddress address, int timeout, byte[] buffer)
402  {
403  return Send(address, timeout, buffer, null);
404  }
405 
423  public PingReply Send(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options)
424  {
425  if (ValidationHelper.IsBlankString(hostNameOrAddress))
426  {
427  throw new ArgumentNullException("hostNameOrAddress");
428  }
429  if (!IPAddress.TryParse(hostNameOrAddress, out IPAddress address))
430  {
431  try
432  {
433  address = Dns.GetHostAddresses(hostNameOrAddress)[0];
434  }
435  catch (ArgumentException)
436  {
437  throw;
438  }
439  catch (Exception innerException)
440  {
441  throw new PingException(SR.GetString("net_ping"), innerException);
442  }
443  }
444  return Send(address, timeout, buffer, options);
445  }
446 
464  public PingReply Send(IPAddress address, int timeout, byte[] buffer, PingOptions options)
465  {
466  if (buffer == null)
467  {
468  throw new ArgumentNullException("buffer");
469  }
470  if (buffer.Length > 65500)
471  {
472  throw new ArgumentException(SR.GetString("net_invalidPingBufferSize"), "buffer");
473  }
474  if (timeout < 0)
475  {
476  throw new ArgumentOutOfRangeException("timeout");
477  }
478  if (address == null)
479  {
480  throw new ArgumentNullException("address");
481  }
482  TestIsIpSupported(address);
483  if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
484  {
485  throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "address");
486  }
487  IPAddress address2 = (address.AddressFamily != AddressFamily.InterNetwork) ? new IPAddress(address.GetAddressBytes(), address.ScopeId) : new IPAddress(address.GetAddressBytes());
489  CheckStart(async: false);
490  try
491  {
492  return InternalSend(address2, buffer, timeout, options, async: false);
493  }
494  catch (Exception innerException)
495  {
496  throw new PingException(SR.GetString("net_ping"), innerException);
497  }
498  finally
499  {
500  Finish(async: false);
501  }
502  }
503 
516  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
517  public void SendAsync(string hostNameOrAddress, object userToken)
518  {
519  SendAsync(hostNameOrAddress, 5000, DefaultSendBuffer, userToken);
520  }
521 
537  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
538  public void SendAsync(string hostNameOrAddress, int timeout, object userToken)
539  {
540  SendAsync(hostNameOrAddress, timeout, DefaultSendBuffer, userToken);
541  }
542 
555  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
556  public void SendAsync(IPAddress address, object userToken)
557  {
558  SendAsync(address, 5000, DefaultSendBuffer, userToken);
559  }
560 
576  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
577  public void SendAsync(IPAddress address, int timeout, object userToken)
578  {
579  SendAsync(address, timeout, DefaultSendBuffer, userToken);
580  }
581 
600  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
601  public void SendAsync(string hostNameOrAddress, int timeout, byte[] buffer, object userToken)
602  {
603  SendAsync(hostNameOrAddress, timeout, buffer, null, userToken);
604  }
605 
624  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
625  public void SendAsync(IPAddress address, int timeout, byte[] buffer, object userToken)
626  {
627  SendAsync(address, timeout, buffer, null, userToken);
628  }
629 
649  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
650  public void SendAsync(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options, object userToken)
651  {
652  if (ValidationHelper.IsBlankString(hostNameOrAddress))
653  {
654  throw new ArgumentNullException("hostNameOrAddress");
655  }
656  if (buffer == null)
657  {
658  throw new ArgumentNullException("buffer");
659  }
660  if (buffer.Length > 65500)
661  {
662  throw new ArgumentException(SR.GetString("net_invalidPingBufferSize"), "buffer");
663  }
664  if (timeout < 0)
665  {
666  throw new ArgumentOutOfRangeException("timeout");
667  }
668  if (IPAddress.TryParse(hostNameOrAddress, out IPAddress address))
669  {
670  SendAsync(address, timeout, buffer, options, userToken);
671  return;
672  }
673  CheckStart(async: true);
674  try
675  {
676  cancelled = false;
677  asyncOp = AsyncOperationManager.CreateOperation(userToken);
678  AsyncStateObject state = new AsyncStateObject(hostNameOrAddress, buffer, timeout, options, userToken);
679  ThreadPool.QueueUserWorkItem(ContinueAsyncSend, state);
680  }
681  catch (Exception innerException)
682  {
683  Finish(async: true);
684  throw new PingException(SR.GetString("net_ping"), innerException);
685  }
686  }
687 
707  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
708  public void SendAsync(IPAddress address, int timeout, byte[] buffer, PingOptions options, object userToken)
709  {
710  if (buffer == null)
711  {
712  throw new ArgumentNullException("buffer");
713  }
714  if (buffer.Length > 65500)
715  {
716  throw new ArgumentException(SR.GetString("net_invalidPingBufferSize"), "buffer");
717  }
718  if (timeout < 0)
719  {
720  throw new ArgumentOutOfRangeException("timeout");
721  }
722  if (address == null)
723  {
724  throw new ArgumentNullException("address");
725  }
726  TestIsIpSupported(address);
727  if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
728  {
729  throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "address");
730  }
731  IPAddress address2 = (address.AddressFamily != AddressFamily.InterNetwork) ? new IPAddress(address.GetAddressBytes(), address.ScopeId) : new IPAddress(address.GetAddressBytes());
733  CheckStart(async: true);
734  try
735  {
736  cancelled = false;
737  asyncOp = AsyncOperationManager.CreateOperation(userToken);
738  InternalSend(address2, buffer, timeout, options, async: true);
739  }
740  catch (Exception innerException)
741  {
742  Finish(async: true);
743  throw new PingException(SR.GetString("net_ping"), innerException);
744  }
745  }
746 
757  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
759  {
760  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
761  {
762  SendAsync(address, tcs);
763  });
764  }
765 
769  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
770  public Task<PingReply> SendPingAsync(string hostNameOrAddress)
771  {
772  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
773  {
774  SendAsync(hostNameOrAddress, tcs);
775  });
776  }
777 
782  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
783  public Task<PingReply> SendPingAsync(IPAddress address, int timeout)
784  {
785  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
786  {
787  SendAsync(address, timeout, tcs);
788  });
789  }
790 
795  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
796  public Task<PingReply> SendPingAsync(string hostNameOrAddress, int timeout)
797  {
798  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
799  {
800  SendAsync(hostNameOrAddress, timeout, tcs);
801  });
802  }
803 
820  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
821  public Task<PingReply> SendPingAsync(IPAddress address, int timeout, byte[] buffer)
822  {
823  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
824  {
825  SendAsync(address, timeout, buffer, tcs);
826  });
827  }
828 
834  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
835  public Task<PingReply> SendPingAsync(string hostNameOrAddress, int timeout, byte[] buffer)
836  {
837  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
838  {
839  SendAsync(hostNameOrAddress, timeout, buffer, tcs);
840  });
841  }
842 
860  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
861  public Task<PingReply> SendPingAsync(IPAddress address, int timeout, byte[] buffer, PingOptions options)
862  {
863  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
864  {
865  SendAsync(address, timeout, buffer, options, tcs);
866  });
867  }
868 
875  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
876  public Task<PingReply> SendPingAsync(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options)
877  {
878  return SendPingAsyncCore(delegate(TaskCompletionSource<PingReply> tcs)
879  {
880  SendAsync(hostNameOrAddress, timeout, buffer, options, tcs);
881  });
882  }
883 
884  private Task<PingReply> SendPingAsyncCore(Action<TaskCompletionSource<PingReply>> sendAsync)
885  {
887  PingCompletedEventHandler handler = null;
888  handler = delegate(object sender, PingCompletedEventArgs e)
889  {
890  HandleCompletion(tcs, e, handler);
891  };
892  PingCompleted += handler;
893  try
894  {
895  sendAsync(tcs);
896  }
897  catch
898  {
899  PingCompleted -= handler;
900  throw;
901  }
902  return tcs.Task;
903  }
904 
905  private void HandleCompletion(TaskCompletionSource<PingReply> tcs, PingCompletedEventArgs e, PingCompletedEventHandler handler)
906  {
907  if (e.UserState == tcs)
908  {
909  try
910  {
911  PingCompleted -= handler;
912  }
913  finally
914  {
915  if (e.Error != null)
916  {
917  tcs.TrySetException(e.Error);
918  }
919  else if (e.Cancelled)
920  {
921  tcs.TrySetCanceled();
922  }
923  else
924  {
925  tcs.TrySetResult(e.Reply);
926  }
927  }
928  }
929  }
930 
931  private void ContinueAsyncSend(object state)
932  {
933  AsyncStateObject asyncStateObject = (AsyncStateObject)state;
934  try
935  {
936  IPAddress address = Dns.GetHostAddresses(asyncStateObject.hostName)[0];
937  new NetworkInformationPermission(NetworkInformationAccess.Ping).Demand();
938  InternalSend(address, asyncStateObject.buffer, asyncStateObject.timeout, asyncStateObject.options, async: true);
939  }
940  catch (Exception innerException)
941  {
942  PingException error = new PingException(SR.GetString("net_ping"), innerException);
943  PingCompletedEventArgs arg = new PingCompletedEventArgs(null, error, cancelled: false, asyncOp.UserSuppliedState);
944  Finish(async: true);
945  asyncOp.PostOperationCompleted(onPingCompletedDelegate, arg);
946  }
947  }
948 
949  private PingReply InternalSend(IPAddress address, byte[] buffer, int timeout, PingOptions options, bool async)
950  {
951  ipv6 = ((address.AddressFamily == AddressFamily.InterNetworkV6) ? true : false);
952  sendSize = buffer.Length;
953  if (!ipv6 && handlePingV4 == null)
954  {
955  handlePingV4 = UnsafeNetInfoNativeMethods.IcmpCreateFile();
956  if (handlePingV4.IsInvalid)
957  {
958  handlePingV4 = null;
959  throw new Win32Exception();
960  }
961  }
962  else if (ipv6 && handlePingV6 == null)
963  {
964  handlePingV6 = UnsafeNetInfoNativeMethods.Icmp6CreateFile();
965  if (handlePingV6.IsInvalid)
966  {
967  handlePingV6 = null;
968  throw new Win32Exception();
969  }
970  }
971  IPOptions options2 = new IPOptions(options);
972  if (replyBuffer == null)
973  {
974  replyBuffer = SafeLocalFree.LocalAlloc(65791);
975  }
976  int num;
977  try
978  {
979  if (async)
980  {
981  if (pingEvent == null)
982  {
983  pingEvent = new ManualResetEvent(initialState: false);
984  }
985  else
986  {
987  pingEvent.Reset();
988  }
989  registeredWait = ThreadPool.RegisterWaitForSingleObject(pingEvent, PingCallback, this, -1, executeOnlyOnce: true);
990  }
991  SetUnmanagedStructures(buffer);
992  if (!ipv6)
993  {
994  num = (int)((!async) ? UnsafeNetInfoNativeMethods.IcmpSendEcho2(handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)address.m_Address, requestBuffer, (ushort)buffer.Length, ref options2, replyBuffer, 65791u, (uint)timeout) : UnsafeNetInfoNativeMethods.IcmpSendEcho2(handlePingV4, pingEvent.SafeWaitHandle, IntPtr.Zero, IntPtr.Zero, (uint)address.m_Address, requestBuffer, (ushort)buffer.Length, ref options2, replyBuffer, 65791u, (uint)timeout));
995  }
996  else
997  {
998  IPEndPoint iPEndPoint = new IPEndPoint(address, 0);
999  SocketAddress socketAddress = iPEndPoint.Serialize();
1000  byte[] sourceSocketAddress = new byte[28];
1001  num = (int)((!async) ? UnsafeNetInfoNativeMethods.Icmp6SendEcho2(handlePingV6, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, sourceSocketAddress, socketAddress.m_Buffer, requestBuffer, (ushort)buffer.Length, ref options2, replyBuffer, 65791u, (uint)timeout) : UnsafeNetInfoNativeMethods.Icmp6SendEcho2(handlePingV6, pingEvent.SafeWaitHandle, IntPtr.Zero, IntPtr.Zero, sourceSocketAddress, socketAddress.m_Buffer, requestBuffer, (ushort)buffer.Length, ref options2, replyBuffer, 65791u, (uint)timeout));
1002  }
1003  }
1004  catch
1005  {
1006  UnregisterWaitHandle();
1007  throw;
1008  }
1009  if (num == 0)
1010  {
1011  num = Marshal.GetLastWin32Error();
1012  if (async && (long)num == 997)
1013  {
1014  return null;
1015  }
1016  FreeUnmanagedStructures();
1017  UnregisterWaitHandle();
1018  if (async || num < 11002 || num > 11045)
1019  {
1020  throw new Win32Exception(num);
1021  }
1022  return new PingReply((IPStatus)num);
1023  }
1024  if (async)
1025  {
1026  return null;
1027  }
1028  FreeUnmanagedStructures();
1029  PingReply result;
1030  if (ipv6)
1031  {
1032  Icmp6EchoReply reply = (Icmp6EchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
1033  result = new PingReply(reply, replyBuffer.DangerousGetHandle(), sendSize);
1034  }
1035  else
1036  {
1037  IcmpEchoReply reply2 = (IcmpEchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(IcmpEchoReply));
1038  result = new PingReply(reply2);
1039  }
1040  GC.KeepAlive(replyBuffer);
1041  return result;
1042  }
1043 
1044  private void TestIsIpSupported(IPAddress ip)
1045  {
1046  if (ip.AddressFamily == AddressFamily.InterNetwork && !Socket.OSSupportsIPv4)
1047  {
1048  throw new NotSupportedException(SR.GetString("net_ipv4_not_installed"));
1049  }
1050  if (ip.AddressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6)
1051  {
1052  throw new NotSupportedException(SR.GetString("net_ipv6_not_installed"));
1053  }
1054  }
1055 
1056  private unsafe void SetUnmanagedStructures(byte[] buffer)
1057  {
1058  requestBuffer = SafeLocalFree.LocalAlloc(buffer.Length);
1059  byte* ptr = (byte*)(void*)requestBuffer.DangerousGetHandle();
1060  for (int i = 0; i < buffer.Length; i++)
1061  {
1062  ptr[i] = buffer[i];
1063  }
1064  }
1065 
1066  private void FreeUnmanagedStructures()
1067  {
1068  if (requestBuffer != null)
1069  {
1070  requestBuffer.Close();
1071  requestBuffer = null;
1072  }
1073  }
1074  }
1075 }
Provides concurrency management for classes that support asynchronous method calls....
PingReply Send(IPAddress address, int timeout, byte[] buffer, PingOptions options)
Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buf...
Definition: Ping.cs:464
PingReply Send(IPAddress address, int timeout, byte[] buffer)
Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buf...
Definition: Ping.cs:401
Ping()
Initializes a new instance of the T:System.Net.NetworkInformation.Ping class.
Definition: Ping.cs:175
The host name is a domain name system (DNS) style host name.
PingCompletedEventHandler PingCompleted
Occurs when an asynchronous operation to send an Internet Control Message Protocol (ICMP) echo messag...
Definition: Ping.cs:125
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Task< PingReply > SendPingAsync(IPAddress address, int timeout)
Send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the c...
Definition: Ping.cs:783
void SendAsync(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the spe...
Definition: Ping.cs:650
void SendAsync(IPAddress address, int timeout, byte[] buffer, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the spe...
Definition: Ping.cs:625
Represents a handle that has been registered when calling M:System.Threading.ThreadPool....
delegate void SendOrPostCallback(object state)
Represents a method to be called when a message is to be dispatched to a synchronization context.
static bool QueueUserWorkItem(WaitCallback callBack, object state)
Queues a method for execution, and specifies an object containing data to be used by the method....
Definition: ThreadPool.cs:278
SafeWaitHandle SafeWaitHandle
Gets or sets the native operating system handle.
Definition: WaitHandle.cs:86
Tracks the lifetime of an asynchronous operation.
Allows an application to determine whether a remote computer is accessible over the network.
Definition: Ping.cs:11
Used to control how T:System.Net.NetworkInformation.Ping data packets are transmitted.
Definition: PingOptions.cs:4
bool Set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
static readonly IPAddress Any
Provides an IP address that indicates that the server must listen for client activity on all network ...
Definition: IPAddress.cs:14
Definition: __Canon.cs:3
Provides data for the E:System.Net.NetworkInformation.Ping.PingCompleted event.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
The exception that is thrown when a Overload:System.Net.NetworkInformation.Ping.Send or Overload:Syst...
Definition: PingException.cs:7
Task< PingReply > SendPingAsync(string hostNameOrAddress, int timeout, byte[] buffer)
Sends an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the ...
Definition: Ping.cs:835
Task< PingReply > SendPingAsync(IPAddress address, int timeout, byte[] buffer, PingOptions options)
Send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the c...
Definition: Ping.cs:861
Task< PingReply > SendPingAsync(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options)
Sends an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the ...
Definition: Ping.cs:876
Implements the Berkeley sockets interface.
Definition: Socket.cs:16
void SendAsync(string hostNameOrAddress, int timeout, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the speci...
Definition: Ping.cs:538
delegate void Action()
Encapsulates a method that has no parameters and does not return a value.
static bool TryParse(string ipString, out IPAddress address)
Determines whether a string is a valid IP address.
Definition: IPAddress.cs:357
virtual bool WaitOne(int millisecondsTimeout, bool exitContext)
Blocks the current thread until the current T:System.Threading.WaitHandle receives a signal,...
Definition: WaitHandle.cs:162
AddressFamily AddressFamily
Gets the address family of the IP address.
Definition: IPAddress.cs:111
Provides information about the status and data resulting from a Overload:System.Net....
Definition: PingReply.cs:6
Task< PingReply > SendPingAsync(IPAddress address)
Send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the c...
Definition: Ping.cs:758
static IPAddress [] GetHostAddresses(string hostNameOrAddress)
Returns the Internet Protocol (IP) addresses for the specified host.
Definition: Dns.cs:606
The exception that is thrown when an operation is performed on a disposed object.
Provides an Internet Protocol (IP) address.
Definition: IPAddress.cs:10
SecurityAction
Specifies the security actions that can be performed using declarative security.
bool TrySetResult(TResult result)
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
bool TrySetException(Exception exception)
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce)
Registers a delegate to wait for a T:System.Threading.WaitHandle, specifying a 32-bit unsigned intege...
Definition: ThreadPool.cs:82
Throws an exception for a Win32 error code.
static readonly IPAddress IPv6Any
The M:System.Net.Sockets.Socket.Bind(System.Net.EndPoint) method uses the F:System....
Definition: IPAddress.cs:37
AddressFamily
Specifies the addressing scheme that an instance of the T:System.Net.Sockets.Socket class can use.
Definition: AddressFamily.cs:5
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
delegate void PingCompletedEventHandler(object sender, PingCompletedEventArgs e)
Represents the method that will handle the E:System.Net.NetworkInformation.Ping.PingCompleted event o...
Task< PingReply > SendPingAsync(string hostNameOrAddress, int timeout)
Sends an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the ...
Definition: Ping.cs:796
void OnPingCompleted(PingCompletedEventArgs e)
Raises the E:System.Net.NetworkInformation.Ping.PingCompleted event.
Definition: Ping.cs:161
PingReply Send(IPAddress address)
Attempts to send an Internet Control Message Protocol (ICMP) echo message to the computer that has th...
Definition: Ping.cs:343
Notifies one or more waiting threads that an event has occurred. This class cannot be inherited.
Task< PingReply > SendPingAsync(string hostNameOrAddress)
Sends an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the ...
Definition: Ping.cs:770
Represents the producer side of a T:System.Threading.Tasks.Task`1 unbound to a delegate,...
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
void SendAsync(string hostNameOrAddress, int timeout, byte[] buffer, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the spe...
Definition: Ping.cs:601
PingReply Send(string hostNameOrAddress, int timeout)
Attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer,...
Definition: Ping.cs:328
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
void SendAsync(IPAddress address, int timeout, byte[] buffer, PingOptions options, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message with the spe...
Definition: Ping.cs:708
void SendAsync(IPAddress address, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the compu...
Definition: Ping.cs:556
PingReply Send(IPAddress address, int timeout)
Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buf...
Definition: Ping.cs:361
Task< TResult > Task
Gets the T:System.Threading.Tasks.Task`1 created by this T:System.Threading.Tasks....
virtual void Close()
Releases all resources held by the current T:System.Threading.WaitHandle.
Definition: WaitHandle.cs:714
long ScopeId
Gets or sets the IPv6 address scope identifier.
Definition: IPAddress.cs:128
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Net.NetworkInformation.Ping object,...
Definition: Ping.cs:229
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
PingReply Send(string hostNameOrAddress)
Attempts to send an Internet Control Message Protocol (ICMP) echo message to the specified computer,...
Definition: Ping.cs:312
PingReply Send(string hostNameOrAddress, int timeout, byte[] buffer)
Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buf...
Definition: Ping.cs:380
static void PtrToStructure(IntPtr ptr, object structure)
Marshals data from an unmanaged block of memory to a managed object.
Definition: Marshal.cs:1198
NetworkInformationAccess
Specifies permission to access information about network interfaces and traffic statistics.
byte [] GetAddressBytes()
Provides a copy of the T:System.Net.IPAddress as an array of bytes.
Definition: IPAddress.cs:473
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
static AsyncOperation CreateOperation(object userSuppliedState)
Returns an T:System.ComponentModel.AsyncOperation for tracking the duration of a particular asynchron...
object UserSuppliedState
Gets or sets an object used to uniquely identify an asynchronous operation.
void SendAsyncCancel()
Cancels all pending asynchronous requests to send an Internet Control Message Protocol (ICMP) echo me...
Definition: Ping.cs:239
void PostOperationCompleted(SendOrPostCallback d, object arg)
Ends the lifetime of an asynchronous operation.
PingReply Send(string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options)
Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buf...
Definition: Ping.cs:423
The exception that is thrown when a method call is invalid for the object's current state.
Provides simple domain name resolution functionality.
Definition: Dns.cs:13
static bool OSSupportsIPv6
Indicates whether the underlying operating system and network adaptors support Internet Protocol vers...
Definition: Socket.cs:268
bool TrySetCanceled()
Attempts to transition the underlying T:System.Threading.Tasks.Task`1 into the F:System....
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
Controls access to network information and traffic statistics for the local computer....
void SendAsync(IPAddress address, int timeout, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the compu...
Definition: Ping.cs:577
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
IPStatus
Reports the status of sending an Internet Control Message Protocol (ICMP) echo message to a computer.
Definition: IPStatus.cs:4
bool Reset()
Sets the state of the event to nonsignaled, causing threads to block.
void SendAsync(string hostNameOrAddress, object userToken)
Asynchronously attempts to send an Internet Control Message Protocol (ICMP) echo message to the speci...
Definition: Ping.cs:517
Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I...
Definition: ThreadPool.cs:14
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18
Specifies the IP options to be inserted into outgoing datagrams.
bool Unregister(WaitHandle waitObject)
Cancels a registered wait operation issued by the M:System.Threading.ThreadPool.RegisterWaitForSingle...
Task< PingReply > SendPingAsync(IPAddress address, int timeout, byte[] buffer)
Send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the c...
Definition: Ping.cs:821
static bool OSSupportsIPv4
Indicates whether the underlying operating system and network adaptors support Internet Protocol vers...
Definition: Socket.cs:234