コード例 #1
0
 public PingRequestExportModel(PingRequest pingRequest)
 {
     Address           = pingRequest.Address.ToString();
     Status            = pingRequest.Status.ToString();
     RoundtripTime     = pingRequest.RoundtripTime;
     TimeToLive        = pingRequest.TimeToLive;
     BufferLength      = pingRequest.BufferLength;
     HasMatchingBuffer = pingRequest.HasMatchingBuffer;
     RequestTime       = pingRequest.RequestTime.ToString("O");
 }
コード例 #2
0
        public async Task <PingSession> StartAsync(PingRequestOptions options, CancellationToken cancellationToken)
        {
            var pingSender  = new Ping();
            var pingOptions = new PingOptions
            {
                Ttl = options.TimeTolive
            };
            var pingRequests = new List <PingRequest>();

            var buffer = CreateBuffer(options.BufferSize);

            var startTime = DateTime.Now;
            var timer     = new Stopwatch();

            timer.Start();

            while (!cancellationToken.IsCancellationRequested && (options.NumberOfPings == -1 || pingRequests.Count < options.NumberOfPings))
            {
                var requestTime = DateTime.Now;
                var pingReply   = await pingSender.SendPingAsync(options.Address, (int)options.PingTimeout.TotalMilliseconds, buffer, pingOptions);

                var pingRequest = new PingRequest
                {
                    Address           = pingReply.Address,
                    Status            = pingReply.Status,
                    RoundtripTime     = pingReply.RoundtripTime,
                    TimeToLive        = pingReply.Options?.Ttl ?? 0,
                    BufferLength      = pingReply.Buffer.Length,
                    HasMatchingBuffer = CheckBuffer(buffer, pingReply.Buffer),
                    RequestTime       = requestTime
                };

                pingRequests.Add(pingRequest);

                var partialSession = new PingSession(startTime, timer.Elapsed, pingRequests);
                PingCompleted?.Invoke(this, new PingCompletedEventArgs
                {
                    CompletedPing = pingRequest,
                    Session       = partialSession
                });

                try
                {
                    await Task.Delay(options.DelayBetweenPings, cancellationToken);
                }
                catch { }
            }

            timer.Stop();
            var endTime = DateTime.Now;

            return(new PingSession(startTime, endTime, timer.Elapsed, pingRequests));
        }