Beispiel #1
0
        public void DisplayTcpReply(PingItem pingItem, bool isPortOpen, int portnumber, int errorCode)
        {
            if (pingItem.PingBackgroundWorker.CancellationPending)
            {
                return;
            }

            // Prefix the ping reply output with a timestamp.
            var pingOutput = new StringBuilder($"[{DateTime.Now.ToLongTimeString()}]  Port {portnumber.ToString()}: ");

            if (isPortOpen)
            {
                pingOutput.Append("OPEN");
            }
            else
            {
                pingOutput.Append("CLOSED");
            }

            // Add response to the output window.
            Application.Current.Dispatcher.BeginInvoke(
                new Action(() => pingItem.AddHistory(pingOutput.ToString())));

            // If logging is enabled, write the response to a file.
            if (ApplicationOptions.LogOutput && ApplicationOptions.LogPath.Length > 0)
            {
                var index    = pingItem.Hostname.IndexOf(':');
                var hostname = (index > 0) ? pingItem.Hostname.Substring(0, index) : pingItem.Hostname;
                var logPath  = $@"{ApplicationOptions.LogPath}\{hostname}.txt";
                using (System.IO.StreamWriter outputFile = new System.IO.StreamWriter(@logPath, true))
                {
                    outputFile.WriteLine(pingOutput.ToString().Insert(1, DateTime.Now.ToShortDateString() + " "));
                }
            }
        }
Beispiel #2
0
        public void PingStartStop(PingItem pingItem)
        {
            if (string.IsNullOrEmpty(pingItem.Hostname))
            {
                return;
            }

            if (!pingItem.IsActive)
            {
                pingItem.IsActive = true;

                if (pingItem.PingBackgroundWorker != null)
                {
                    pingItem.PingBackgroundWorker.CancelAsync();
                }

                pingItem.PingStatisticsText = string.Empty;
                pingItem.History            = new ObservableCollection <string>();
                pingItem.AddHistory($"*** Pinging {pingItem.Hostname}:");

                pingItem.PingBackgroundWorker = new BackgroundWorker();
                pingItem.PingResetEvent       = new AutoResetEvent(false);
                if (pingItem.Hostname.Count(f => f == ':') == 1)
                {
                    pingItem.PingBackgroundWorker.DoWork += new DoWorkEventHandler(backgroundThread_PerformTcpProbe);
                }
                else
                {
                    pingItem.PingBackgroundWorker.DoWork += new DoWorkEventHandler(backgroundThread_PerformIcmpProbe);
                }
                pingItem.PingBackgroundWorker.WorkerSupportsCancellation = true;
                pingItem.PingBackgroundWorker.WorkerReportsProgress      = true;
                pingItem.PingBackgroundWorker.ProgressChanged           += new ProgressChangedEventHandler(backgroundThread_ProgressChanged);
                pingItem.PingBackgroundWorker.RunWorkerAsync(pingItem);
            }
            else
            {
                pingItem.PingBackgroundWorker.CancelAsync();
                pingItem.PingResetEvent.WaitOne();
                pingItem.Status   = PingStatus.Inactive;
                pingItem.IsActive = false;
            }

            RefreshGlobalStartStop();
        }
Beispiel #3
0
        public void DisplayIcmpReply(PingItem pingItem)
        {
            if (pingItem.Reply == null)
            {
                return;
            }
            if (pingItem.PingBackgroundWorker.CancellationPending)
            {
                return;
            }

            var pingOutput = new StringBuilder($"[{DateTime.Now.ToLongTimeString()}]  ");

            // Read the status code of the ping response.
            switch (pingItem.Reply.Status)
            {
            case IPStatus.Success:
                pingOutput.Append("Reply from ");
                pingOutput.Append(pingItem.Reply.Address.ToString());
                if (pingItem.Reply.RoundtripTime < 1)
                {
                    pingOutput.Append("  [<1ms]");
                }
                else
                {
                    pingOutput.Append($"  [{pingItem.Reply.RoundtripTime} ms]");
                }
                break;

            case IPStatus.DestinationHostUnreachable:
                pingOutput.Append("Reply  [Host unreachable]");
                break;

            case IPStatus.DestinationNetworkUnreachable:
                pingOutput.Append("Reply  [Network unreachable]");
                break;

            case IPStatus.DestinationUnreachable:
                pingOutput.Append("Reply  [Unreachable]");
                break;

            case IPStatus.TimedOut:
                pingOutput.Append("Request timed out.");
                break;

            default:
                pingOutput.Append(pingItem.Reply.Status.ToString());
                break;
            }
            // Add response to the output window.
            Application.Current.Dispatcher.BeginInvoke(
                new Action(() => pingItem.AddHistory(pingOutput.ToString())));

            // If logging is enabled, write the response to a file.
            if (ApplicationOptions.LogOutput && ApplicationOptions.LogPath.Length > 0)
            {
                var logPath = $@"{ApplicationOptions.LogPath}\{pingItem.Hostname}.txt";
                using (System.IO.StreamWriter outputFile = new System.IO.StreamWriter(@logPath, true))
                {
                    outputFile.WriteLine(pingOutput.ToString().Insert(1, DateTime.Now.ToShortDateString() + " "));
                }
            }
        }