コード例 #1
0
        // FIXME this is blocked by some providers as "ICMP flood"
        // FIXME make selector between this and Unity
        private static void PingICMPAsync(ServerData ret, int pingTimeout, byte[] pingBuffer, PingOptions pingOptions)
        {
            using (var ping = new Ping())
            {
                ServerList.pings.Add(ping);

                string prefix = String.Format("[P {0}][IP:p {1}:{2}]", ret.Project, ret.IP, ret.port);

                ping.PingCompleted += (s, e) => {
                    if (e.Cancelled)
                    {
                        Console.WriteLine(String.Format(
                                              "{0} Ping was cancelled.",
                                              prefix
                                              ));

                        ((AutoResetEvent)e.UserState).Set();
                        return;
                    }

                    if (e.Error != null)
                    {
                        Console.WriteLine(String.Format(
                                              "{0} Error! {1}",
                                              prefix,
                                              e.Error.ToString()
                                              ));

                        ((AutoResetEvent)e.UserState).Set();
                        return;
                    }

                    ServerList.ProcessICMPPing(ret, pingTimeout, e.Reply);
                    ((AutoResetEvent)e.UserState).Set();
                };

                AutoResetEvent pingWaiter = new AutoResetEvent(false);
                try
                {
                    ping.SendAsync(IPAddress.Parse(ret.IP), pingTimeout, pingBuffer, pingOptions, pingWaiter);
                    return;
                }
                catch (FormatException) {
                    // server IP was probably server NS
                    // we will retry call directly
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.ToString());
                    return;
                }

                try
                {
                    ping.SendAsync(ret.IP, pingTimeout, pingBuffer, pingOptions, pingWaiter);
                    return;
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.ToString());
                    return;
                }
            }
        }