Skip to content

Commit

Permalink
Fixed the wrong values for the bandwidth calculation and improved the…
Browse files Browse the repository at this point in the history
… graph accordingly.
  • Loading branch information
wokhan committed Nov 14, 2020
1 parent 2186be7 commit 24a68a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Console/UI/Controls/BandwidthGraph.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ public void UpdateGraph()
{
var colorbrush = connectionGroup.First().Color;
var color = OxyColor.FromArgb(colorbrush.A, colorbrush.R, colorbrush.G, colorbrush.B);
Model.Series.Add(new LineSeries() { Title = seriesInTitle, Color = color, ItemsSource = seriesInValues = new ObservableCollection<DataPoint>(), InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline });
Model.Series.Add(new LineSeries() { Title = seriesOutTitle, Color = color, LineStyle = LineStyle.Dash, ItemsSource = seriesOutValues = new ObservableCollection<DataPoint>(), InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline });
Model.Series.Add(new LineSeries() { Title = seriesInTitle, Color = color, ItemsSource = seriesInValues = new ObservableCollection<DataPoint>(), InterpolationAlgorithm = InterpolationAlgorithms.UniformCatmullRomSpline });
Model.Series.Add(new LineSeries() { Title = seriesOutTitle, Color = color, LineStyle = LineStyle.Dash, ItemsSource = seriesOutValues = new ObservableCollection<DataPoint>(), InterpolationAlgorithm = InterpolationAlgorithms.UniformCatmullRomSpline });
}

var lastIn = connectionGroup.Sum(connection => connection.InboundBandwidth);
var lastIn = connectionGroup.Sum(connection => (long)connection.InboundBandwidth);
seriesInValues.Add(DateTimeAxis.CreateDataPoint(datetime, lastIn));
if (seriesInValues.Count > 3 && seriesInValues[^2].Y == lastIn && seriesInValues[^3].Y == lastIn)
{
seriesInValues.RemoveAt(seriesInValues.Count - 2);
}

var lastOut = connectionGroup.Sum(connection => connection.OutboundBandwidth);
var lastOut = connectionGroup.Sum(connection => (long)connection.OutboundBandwidth);
seriesOutValues.Add(DateTimeAxis.CreateDataPoint(datetime, lastOut));
if (seriesOutValues.Count > 3 && seriesOutValues[^2].Y == lastOut && seriesOutValues[^3].Y == lastOut)
{
Expand Down
35 changes: 28 additions & 7 deletions Console/ViewModels/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ private bool TryEnableStats()
{
try
{
// Ignoring bandwidth measurement for loopbacks as it is meaningless anyway
if (this.RemoteAddress == "127.0.0.1")
{
return false;
}

if (this.rawConnection is MIB_TCPROW_OWNER_MODULE)
{
rawrow = ((MIB_TCPROW_OWNER_MODULE)this.rawConnection).ToTCPRow();
Expand Down Expand Up @@ -162,7 +168,7 @@ internal void UpdateValues(IConnectionOwnerInfo b)

RemotePort = (b.RemotePort == -1 ? String.Empty : b.RemotePort.ToString());
State = Enum.GetName(typeof(ConnectionStatus), b.State);
if (b.State == ConnectionStatus.ESTABLISHED)
if (b.State == ConnectionStatus.ESTABLISHED && !IsAccessDenied)
{
if (!statsEnabled)
{
Expand Down Expand Up @@ -277,11 +283,22 @@ public Color Color
set => this.SetValue(ref _color, value, NotifyPropertyChanged);
}

private double _inboundBandwidth;
public double InboundBandwidth { get => _inboundBandwidth; private set => this.SetValue(ref _inboundBandwidth, value, NotifyPropertyChanged); }
private ulong _inboundBandwidth;
public ulong InboundBandwidth
{
get => _inboundBandwidth;
private set => this.SetValue(ref _inboundBandwidth, value, NotifyPropertyChanged);
}

private ulong _outboundBandwidth;
public ulong OutboundBandwidth
{
get => _outboundBandwidth;
private set => this.SetValue(ref _outboundBandwidth, value, NotifyPropertyChanged);
}

private double _outboundBandwidth;
public double OutboundBandwidth { get => _outboundBandwidth; private set => this.SetValue(ref _outboundBandwidth, value, NotifyPropertyChanged); }
private ulong _lastInboundReadValue;
private ulong _lastOutboundReadValue;

private bool statsEnabled;
private void EstimateBandwidth()
Expand All @@ -296,8 +313,12 @@ private void EstimateBandwidth()
if (rawrow != null && !IsAccessDenied)
{
var bandwidth = (rawrow is TCPHelper.MIB_TCPROW ? TCPHelper.GetTCPBandwidth((TCPHelper.MIB_TCPROW)rawrow) : TCP6Helper.GetTCPBandwidth((TCP6Helper.MIB_TCP6ROW)rawrow));
InboundBandwidth = bandwidth.InboundBandwidth;
OutboundBandwidth = bandwidth.OutboundBandwidth;
// Fix according to https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-setpertcpconnectionestats
// One must subtract the previously read value to get the right one (as reenabling statistics doesn't work as before starting from Win 10 1709)
InboundBandwidth = bandwidth.InboundBandwidth >= _lastInboundReadValue ? bandwidth.InboundBandwidth - _lastInboundReadValue : bandwidth.InboundBandwidth;
OutboundBandwidth = bandwidth.OutboundBandwidth >= _lastOutboundReadValue ? bandwidth.OutboundBandwidth - _lastOutboundReadValue : bandwidth.OutboundBandwidth;
_lastInboundReadValue = bandwidth.InboundBandwidth;
_lastOutboundReadValue = bandwidth.OutboundBandwidth;
return;
}
}
Expand Down

0 comments on commit 24a68a1

Please sign in to comment.