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));
        }
示例#2
0
        static async Task Main(string[] args)
        {
            await Parser.Default.ParseArguments <PingArguments>(args)
            .WithParsedAsync(async pingArgs =>
            {
                var options = new PingRequestOptions
                {
                    Address           = pingArgs.TargetName,
                    BufferSize        = pingArgs.BufferSize,
                    DelayBetweenPings = pingArgs.BreakBetweenPings,
                    PingTimeout       = pingArgs.PingTimeout,
                    TimeTolive        = pingArgs.TimeToLive,
                    NumberOfPings     = pingArgs.PingUntilStopped ? -1 : pingArgs.NumberOfPings
                };

                //TODO: Error on bad layout
                PingSession session = null;
                if (pingArgs.Layout == Layout.Traditional)
                {
                    session = await TraditionalPing.StartAsync(options);
                }
                else if (pingArgs.Layout == Layout.Modern)
                {
                    session = await ModernPing.StartAsync(options);
                }

                if (session != null)
                {
                    if (!string.IsNullOrWhiteSpace(pingArgs.ExportLocation))
                    {
                        Console.WriteLine("Exporting results to {0}...", pingArgs.ExportLocation);
                        using (var file = File.Open(pingArgs.ExportLocation, FileMode.Create))
                            using (var writer = new StreamWriter(file))
                                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                                {
                                    await csv.WriteRecordsAsync(session.Requests.Select(r => new PingRequestExportModel(r)));
                                }
                    }
                }
            });
        }