Beispiel #1
0
        private static async Task PingAsync(dynamic state)
        {
            object    token   = state.token;
            byte      ttl     = state.ttl;
            IPAddress Target  = state.Target;
            byte      MaxHops = state.MaxHops;
            Action <TraceRouteHostResult> OnHostResult = state.OnHostResult;
            int PingTimeoutMs = state.PingTimeoutMs;

            byte[] buffer = state.buffer;

            PingOptions opt  = new PingOptions(ttl, true);
            Ping        ping = PingInstancePool.Get();
            Stopwatch   sw   = new Stopwatch();

            sw.Start();
            PingReply reply = await ping.SendPingAsync(Target, PingTimeoutMs, buffer, opt);

            sw.Stop();

            bool      Success       = reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired;
            long      RoundTripTime = reply.RoundtripTime;
            IPAddress ReplyFrom     = Success ? reply.Address : IPAddress.Any;

            PingInstancePool.Recycle(ping);

            TraceRouteHostResult result = new TraceRouteHostResult(token, Success, RoundTripTime, ReplyFrom, ttl, Target, MaxHops, PingTimeoutMs);

            OnHostResult(result);
        }
        private static void Ping_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            dynamic   state = e.UserState;
            Stopwatch sw    = state.sw;

            sw.Stop();
            object    token   = state.token;
            byte      ttl     = state.ttl;
            IPAddress Target  = state.Target;
            byte      MaxHops = state.MaxHops;
            Action <TraceRouteHostResult> OnHostResult = state.OnHostResult;
            int PingTimeoutMs = state.PingTimeoutMs;

            bool      Success       = !e.Cancelled && (e.Reply.Status == IPStatus.Success || e.Reply.Status == IPStatus.TtlExpired);
            long      RoundTripTime = e.Reply.RoundtripTime;
            IPAddress ReplyFrom     = Success ? e.Reply.Address : IPAddress.Any;

            ((Ping)sender).PingCompleted -= Ping_PingCompleted;
            PingInstancePool.Recycle((Ping)sender);

            TraceRouteHostResult result = new TraceRouteHostResult(token, Success, RoundTripTime, ReplyFrom, ttl, Target, MaxHops, PingTimeoutMs);

            OnHostResult(result);
        }