private Point GetPoint(long val, long nth, long resolution, PingGraphInfo pgi) { double w = NetGraph.ActualWidth; double h = NetGraph.ActualHeight; double x = (w / (resolution - 1)) * nth; double y = 0; long lowest = pgi.lowest; long highest = pgi.highest; if (pgi.scope > 0) { highest = pgi.scope; lowest = 0; } if (val < lowest) { val = lowest; } else if (val >= highest) { val = highest; } val = val - lowest; highest = highest - lowest; double mod = (double)val / (double)highest; y = h * mod; return(new Point(x, h - y)); }
public void ReTracert() { if (p == null) { p = new Ping(); } if (this.error != IPStatus.Success) { return; } byte[] test = Encoding.Default.GetBytes("tracert " + DateTime.UtcNow.Ticks.ToString()); List <PingGraphInfo> temp = new List <PingGraphInfo>();; PingOptions po = new PingOptions(); po.Ttl = 1; PingReply pr = p.Send(address, 1000, test, po); while (pr.Status != IPStatus.Success) { PingGraphInfo subpgi = new PingGraphInfo(); subpgi.address = pr.Address == null ? null : pr.Address.ToString(); subpgi.maxpoints = this.maxpoints; temp.Add(subpgi); if (po.Ttl++ >= 255) { break; } pr = p.Send(address, 1000, test, po); } if (route == null || route.Count <= 0) { route = temp; this.points.Add(new DrawPoint(this.points[this.points.Count - 1].value, 3)); return; } else if (temp.Count == route.Count) { for (int n = 0; n < temp.Count; n++) { if (temp[n].address != route[n].address) { route = temp; this.points.Add(new DrawPoint(this.points[this.points.Count - 1].value, 3)); return; } } } else { route = temp; this.points.Add(new DrawPoint(this.points[this.points.Count - 1].value, 3)); return; } }
private void WindowSizeChanged(object sender, SizeChangedEventArgs e) { try { PingGraphInfo pgi = ((Window)sender).Tag as PingGraphInfo; ReDrawGraph(pgi); } catch (Exception ex) { MessageBox.Show("ERROR: " + ex.Message + "\n" + ex.StackTrace); } }
private void UpdateRoute(PingGraphInfo pgi) { if (tracert.HasItems) { tracert.Items.Clear(); } List <ObservablePingInfo> opi = pgi.GetRoute(); if (opi != null) { foreach (ObservablePingInfo pi in opi) { tracert.Items.Add(pi); } } }
private void UpdateStatusbar(PingGraphInfo pgi) { double percent = 0; if (pgi.lost > 0) { percent = (double)pgi.lost / (double)pgi.packages; percent *= 100; } long last = pgi.points[pgi.points.Count - 1].value; CurrentPing.Content = "Ping: " + last.ToString(); AvgPing.Content = "Average Ping (ttl): " + pgi.avg + " (" + pgi.ttl + ")"; HiLoPing.Content = "Highest/Lowest ping: " + pgi.highest + "/" + pgi.lowest; Packets.Content = "Packets Sent/Lost (%): " + pgi.packages + "/" + pgi.lost + " (" + percent + "%)"; }
public PingGraphInfo(string addr, long max) { if (p == null) { p = new Ping(); } scope = 0; points = new List <DrawPoint>(); route = new List <PingGraphInfo>(); address = addr; maxpoints = max; this.DoPing(); if (this.error != IPStatus.Success) { return; } byte[] test = Encoding.Default.GetBytes("tracert " + DateTime.UtcNow.Ticks.ToString()); PingOptions po = new PingOptions(); po.Ttl = 1; PingReply pr = p.Send(addr, 1000, test, po); while (pr.Status != IPStatus.Success) { PingGraphInfo subpgi = new PingGraphInfo(); subpgi.address = pr.Address == null ? null : pr.Address.ToString(); subpgi.maxpoints = this.maxpoints; subpgi.DoPing(); route.Add(subpgi); if (po.Ttl++ >= 255) { break; } pr = p.Send(addr, 1000, test, po); } }
private void WindowTick(object sender, EventArgs e) { try { Task.Factory.StartNew(() => { this.Dispatcher.Invoke((Action)(() => { PingGraphInfo pgi = ((Window)((DispatcherTimer)sender).Tag).Tag as PingGraphInfo; pgi.DoPing(); ReDrawGraph(pgi); UpdateStatusbar(pgi); UpdateRoute(pgi); })); }); } catch (Exception ex) { MessageBox.Show("ERROR: " + ex.Message + "\n" + ex.StackTrace); } }
private void WindowLoaded(object sender, RoutedEventArgs e) { try { if (string.IsNullOrEmpty(addr)) { MessageBox.Show("You gotta enter an address"); this.Close(); return; } this.Title = this.addr + " loading"; PingGraphInfo pgi = new PingGraphInfo(this.addr, 100); if (pgi.error != IPStatus.Success) { MessageBox.Show("Couldnt find: " + this.addr); this.Close(); return; } this.Tag = pgi; pgi.owner = this; UpdateRoute(pgi); this.SizeChanged += WindowSizeChanged; DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(500); timer.Tag = this; timer.Tick += WindowTick; timer.Start(); this.Title = this.addr + " pinging"; } catch (Exception ex) { MessageBox.Show("ERROR: " + ex.Message + "\n" + ex.StackTrace); } }
private void ReDrawGraph(PingGraphInfo pgi) { NetGraph.Children.RemoveRange(0, NetGraph.Children.Count); PointCollection pcmid = new PointCollection(); if (pgi.scope <= 0) { pcmid.Add(new Point(0, NetGraph.ActualHeight / 2)); pcmid.Add(new Point(NetGraph.ActualWidth, NetGraph.ActualHeight / 2)); } else { double mod = (double)pgi.avg / (double)pgi.scope; pcmid.Add(new Point(0, NetGraph.ActualHeight - (NetGraph.ActualHeight * mod))); pcmid.Add(new Point(NetGraph.ActualWidth, NetGraph.ActualHeight - (NetGraph.ActualHeight * mod))); } Polyline midline = new Polyline(); midline.StrokeThickness = 1; midline.Stroke = Brushes.Blue; midline.Points = pcmid; NetGraph.Children.Add(midline); PointCollection pc = new PointCollection(); Point pt; for (int n = 0; n < pgi.points.Count; n++) { pt = GetPoint(pgi.points[n].value, n, pgi.maxpoints, pgi); pc.Add(pt); if (pgi.points[n].intresting == 1) { PointCollection circle = Circle(pt.X, pt.Y, 5, 50); Polyline plcircle = new Polyline(); plcircle.StrokeThickness = 2; plcircle.Stroke = Brushes.Red; plcircle.Points = circle; NetGraph.Children.Add(plcircle); } else if (pgi.points[n].intresting == 2) { NetGraph.Children.Add(Dot(pt.X, pt.Y, 5, 50, Brushes.HotPink)); } else if (pgi.points[n].intresting == 3) { PointCollection cross = new PointCollection(); cross.Add(new Point(pt.X, NetGraph.ActualHeight)); cross.Add(new Point(pt.X, 0)); Polyline crossline = new Polyline(); crossline.StrokeThickness = 3; crossline.Stroke = Brushes.LightBlue; crossline.Points = cross; NetGraph.Children.Add(crossline); } } Polyline polyline = new Polyline(); polyline.StrokeThickness = 2; polyline.Stroke = Brushes.Red; polyline.Points = pc; NetGraph.Children.Add(polyline); }