[System] Remove Process.Start and related API from TvOS/WatchOS.
[mono.git] / mcs / class / System / System.Net.NetworkInformation / Ping.cs
index 3f2ae21e2ea5b25933aea2257bd298a0bb517a9b..abb6d5d6bce4f5fcae3d6a2a5b1e705925971278 100644 (file)
@@ -26,6 +26,7 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
+
 using System;
 using System.IO;
 using System.Text;
@@ -286,7 +287,8 @@ namespace System.Net.NetworkInformation {
 
                private PingReply SendUnprivileged (IPAddress address, int timeout, byte [] buffer, PingOptions options)
                {
-                       DateTime sentTime = DateTime.Now;
+#if MONO_FEATURE_PROCESS_START
+                       DateTime sentTime = DateTime.UtcNow;
 
                        Process ping = new Process ();
                        string args = BuildPingArgs (address, timeout, options);
@@ -301,6 +303,7 @@ namespace System.Net.NetworkInformation {
                        ping.StartInfo.RedirectStandardOutput = true;
                        ping.StartInfo.RedirectStandardError = true;
 
+                       IPStatus status = IPStatus.Unknown;
                        try {
                                ping.Start ();
 
@@ -309,23 +312,24 @@ namespace System.Net.NetworkInformation {
                                string stderr = ping.StandardError.ReadToEnd ();
 #pragma warning restore 219
                                
-                               trip_time = (long) (DateTime.Now - sentTime).TotalMilliseconds;
+                               trip_time = (long) (DateTime.UtcNow - sentTime).TotalMilliseconds;
                                if (!ping.WaitForExit (timeout) || (ping.HasExited && ping.ExitCode == 2))
-                                       return new PingReply (address, buffer, options, trip_time, IPStatus.TimedOut); 
-
-                               if (ping.ExitCode == 1)
-                                       return new PingReply (address, buffer, options, trip_time, IPStatus.TtlExpired);
-                       } catch (Exception) {
-                               return new PingReply (address, buffer, options, trip_time, IPStatus.Unknown);
+                                       status = IPStatus.TimedOut;
+                               else if (ping.ExitCode == 0)
+                                       status = IPStatus.Success;
+                               else if (ping.ExitCode == 1)
+                                       status = IPStatus.TtlExpired;
+                       } catch {
                        } finally {
-                               if (ping != null) {
-                                       if (!ping.HasExited)
-                                               ping.Kill ();
-                                       ping.Dispose ();
-                               }
+                               if (!ping.HasExited)
+                                       ping.Kill ();
+                               ping.Dispose ();
                        }
 
-                       return new PingReply (address, buffer, options, trip_time, IPStatus.Success);
+                       return new PingReply (address, buffer, options, trip_time, status);
+#else
+                       throw new NotSupportedException ("Ping is not supported on this platform.");
+#endif // MONO_FEATURE_PROCESS_START
                }
 
                // Async
@@ -593,4 +597,3 @@ namespace System.Net.NetworkInformation {
                }
        }
 }
-