示例#1
0
        private static void DisplayTcpReply(Probe pingItem, bool isPortOpen, int portnumber, int errorCode, long elapsedTime)
        {
            if (pingItem.Thread.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  [" + elapsedTime.ToString() + "ms]");
            }
            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.IsLogOutputEnabled && 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() + " "));
                }
            }
        }
示例#2
0
        public static void StartStop(Probe probe)
        {
            if (string.IsNullOrEmpty(probe.Hostname))
            {
                return;
            }

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

                if (probe.Thread != null)
                {
                    probe.Thread.CancelAsync();
                }

                // TODO:
                //if (pingItem.Hostname != null && _Aliases.ContainsKey(pingItem.Hostname))
                //    pingItem.Alias = _Aliases[pingItem.Hostname];

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

                probe.Thread           = new BackgroundWorker();
                probe.ThreadResetEvent = new AutoResetEvent(false);
                if (probe.Hostname.Count(f => f == ':') == 1)
                {
                    probe.Thread.DoWork += new DoWorkEventHandler(Thread_PerformTcpProbe);
                }
                else
                {
                    probe.Thread.DoWork += new DoWorkEventHandler(Thread_PerformIcmpProbe);
                }
                probe.Thread.WorkerSupportsCancellation = true;
                probe.Thread.WorkerReportsProgress      = true;
                probe.Thread.ProgressChanged           += new ProgressChangedEventHandler(Thread_ProgressChanged);
                probe.Thread.RunWorkerAsync(probe);
            }
            else
            {
                probe.Thread.CancelAsync();
                probe.ThreadResetEvent.WaitOne();
                probe.Status   = ProbeStatus.Inactive;
                probe.IsActive = false;

                probe.WriteFinalStatisticsToHistory();
            }
        }
示例#3
0
        private static void DisplayIcmpReply(Probe pingItem)
        {
            if (pingItem.Reply == null)
            {
                return;
            }
            if (pingItem.Thread.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.IsLogOutputEnabled && 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() + " "));
                }
            }
        }