Advertisement
Guest User

Untitled

a guest
Aug 29th, 2011
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Net.NetworkInformation;
  8. using System.Diagnostics;
  9. using System.Runtime.InteropServices;
  10.  
  11. namespace upcping
  12. {
  13.     class Program
  14.     {
  15.         private static List<DateTime> peaks;
  16.  
  17.         public enum CtrlTypes
  18.         {
  19.             CTRL_C_EVENT = 0,
  20.             CTRL_BREAK_EVENT,
  21.             CTRL_CLOSE_EVENT,
  22.             CTRL_LOGOFF_EVENT = 5,
  23.             CTRL_SHUTDOWN_EVENT
  24.         }
  25.  
  26.         public delegate bool HandlerRoutine(CtrlTypes CtrlType);
  27.        
  28.         [DllImport("Kernel32")]
  29.         public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
  30.  
  31.         private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
  32.         {
  33.             if (peaks.Count > 0)
  34.             {
  35.                 Trace.WriteLine("\r\n\r\nPeaks:");
  36.  
  37.                 foreach (var peak in peaks)
  38.                     Trace.WriteLine(peak.ToLongTimeString());
  39.             }
  40.  
  41.             return false;
  42.         }
  43.  
  44.         static void Main(string[] args)
  45.         {
  46.             string[] ips = { //"192.168.1.1", // router
  47.                              "192.168.100.1", // modem
  48.                              "84.10.148.1", // router UPC #1
  49.                              "89.75.2.161", // router UPC #2
  50.                              "213.180.146.27" }; // onet.pl
  51.  
  52.             const int interval = 1000; // w ms
  53.             const int packetSize = 32; // w bajtach
  54.             const int timeout = 2000; // w ms
  55.             const int threshold = 100; // w ms
  56.  
  57.             SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
  58.  
  59.             Ping ping = new Ping();
  60.             var tasks = new Task<PingReply>[ips.Length];
  61.             byte[] buffer = Encoding.ASCII.GetBytes(new string('F', packetSize));
  62.             int row = 0;
  63.             peaks = new List<DateTime>();
  64.  
  65.             TextWriterTraceListener textFileTracer = new TextWriterTraceListener(DateTime.Now.ToShortDateString() + " " +
  66.                 DateTime.Now.ToShortTimeString().Replace(':', '.') + ".txt");
  67.             ConsoleTraceListener consoleTracer = new ConsoleTraceListener();
  68.             Trace.Listeners.Add(textFileTracer);
  69.             Trace.Listeners.Add(consoleTracer);
  70.             Trace.AutoFlush = true;
  71.  
  72.             while (true)
  73.             {
  74.                 if (row % 30 == 0)
  75.                 {
  76.                     Trace.Write(new string(' ', DateTime.Now.ToLongTimeString().Length));
  77.                     foreach (var ip in ips)
  78.                         Trace.Write(string.Format("{0,16}", ip));
  79.                     Trace.WriteLine("");
  80.                     row = 0;
  81.                 }
  82.                 row++;
  83.  
  84.                 for (int i = 0; i < ips.Length; i++)
  85.                     tasks[i] = new Task<PingReply>((object ip) => ping.Send(ip.ToString(), timeout, buffer,
  86.                         new PingOptions() { DontFragment = true }), ips[i]);
  87.  
  88.                 var start = DateTime.Now;
  89.  
  90.                 foreach (var task in tasks)
  91.                     task.Start();
  92.  
  93.                 try
  94.                 {
  95.                     Task.WaitAll(tasks);
  96.                 }
  97.                 catch (Exception)
  98.                 {
  99.                     Trace.WriteLine("Error");
  100.                     continue;
  101.                 }
  102.                 finally
  103.                 {
  104.                     var elapsed = interval - (int)(DateTime.Now - start).TotalMilliseconds;
  105.  
  106.                     if (elapsed > 0)
  107.                         Thread.Sleep(elapsed);
  108.                 }
  109.  
  110.                 StringBuilder sb = new StringBuilder();
  111.                 foreach (var task in tasks)
  112.                 {
  113.                     PingReply result = task.Result;
  114.  
  115.                     if (result.Status == IPStatus.TimedOut)
  116.                         sb.AppendFormat("{0,16}", "timeout");
  117.                     else if (result.Status != IPStatus.Success)
  118.                         sb.AppendFormat("{0,16}", "*");
  119.                     else if (result.RoundtripTime >= threshold)
  120.                     {
  121.                         sb.AppendFormat("{0,16}", "**** " + result.RoundtripTime + "ms");
  122.  
  123.                         DateTime lastPeak = peaks.FindLast(dateTime => (DateTime.Now - dateTime).TotalSeconds > 30);
  124.                         if (lastPeak == default(DateTime)) peaks.Add(DateTime.Now);
  125.                     }
  126.                     else
  127.                         sb.AppendFormat("{0,14}ms", (result.RoundtripTime == 0) ? "<1" : result.RoundtripTime.ToString());
  128.                 }
  129.  
  130.                 for (int i = 0; i < tasks.Length; i++)
  131.                     tasks[i].Dispose();
  132.  
  133.                 string log = sb.ToString();
  134.                
  135.                 Trace.WriteLine(DateTime.Now.ToLongTimeString() + log);
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement