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));
        }
Beispiel #2
0
        private Task StartPing()
        {
            while (true)
            {
                int timeOut;
                using (TcpClient tcpClient = new TcpClient())
                {
                    timeOut = (tcpClient.ConnectAsync(_host, _port).Wait(1000))
                        ? _period * 1000
                        : (_period - 1) * 1000;
                    string replyLog = $"{DateTime.Now:dd MMMM yyyy HH:mm:ss} {_host} {((tcpClient.Connected) ? "Success" : "Failed")}";
                    PingCompleted?.Invoke(replyLog, _configProtocol);
                }

                Thread.Sleep(timeOut);
            }
        }
Beispiel #3
0
        private void PingRoundCompleted(object sender, PingCompletedEventArgs ev)
        {
            if (ev.Cancelled)
            {
                PingCompleted?.Invoke("Пинг отменён.", _configProtocol);
                ((AutoResetEvent)ev.UserState).Set();
            }

            if (ev.Error != null)
            {
                PingCompleted?.Invoke($"Ошибка пинга: {ev.Error}", _configProtocol);
                ((AutoResetEvent)ev.UserState).Set();
            }

            PingReply pingReply = ev.Reply;
            var       replyLog  = $"{DateTime.Now:dd MMMM yyyy HH:mm:ss} {_host} {pingReply.Status}";

            ((AutoResetEvent)ev.UserState).Set();
            PingCompleted?.Invoke(replyLog, _configProtocol);
        }
Beispiel #4
0
        private async Task StartAsync()
        {
            while (true)
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    var urlBuilder = new UriBuilder(_host);
                    urlBuilder.Scheme = "http";
                    string host = urlBuilder.ToString();

                    Uri url;
                    if (!Uri.TryCreate(host, UriKind.Absolute, out url))
                    {
                        PingCompleted?.Invoke($"Ошибка url {host} для HTTP протокола!", _configProtocol);
                        return;
                    }

                    try
                    {
                        HttpResponseMessage httpResponse = await httpClient.GetAsync(url);

                        httpResponse.EnsureSuccessStatusCode();
                        string replyLog =
                            $"{DateTime.Now:dd MMMM yyyy HH:mm:ss} {host} {((int) httpResponse.StatusCode == _validCode ? "Success" : "Failed")}";

                        PingCompleted?.Invoke(replyLog, _configProtocol);
                    }
                    catch (HttpRequestException e)
                    {
                        string replyLog = $"\nОшибка :{url} - {e.Message}";
                        PingCompleted?.Invoke(replyLog, _configProtocol);
                    }
                }

                Thread.Sleep(_period * 1000);
            }
        }
Beispiel #5
0
 protected void OnPingCompleted(PingCompletedEventArgs e)
 {
     PingCompleted?.Invoke(this, e);
 }
Beispiel #6
0
 protected virtual void OnPingCompleted()
 {
     PingCompleted?.Invoke(this, EventArgs.Empty);
 }