Skip to content

Commit b79e0a7

Browse files
committed
Added automatic sample purging when storage reaches limit.
Changed internal storage from Stack to Deque in NetworkInterfaceSampler. Changed NetworkInterfaceSampler to implement IEnumerable<Sample>. Added application icon.
1 parent 8175a0c commit b79e0a7

File tree

11 files changed

+165
-95
lines changed

11 files changed

+165
-95
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.*/
22
bin/
33
obj/
4+
packages/
5+
*.user

Source/Controls/NetGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected override void OnPaint(PaintEventArgs e) {
8989
// Drawing start point on x-axis.
9090
var x = surface.Right - 1;
9191

92-
foreach (var sample in sampler.GetSamples()) {
92+
foreach (var sample in sampler) {
9393
var downstream = sample.Downstream / (float)sampler.MaximumSpeed;
9494
var upstream = sample.Upstream / (float)sampler.MaximumSpeed;
9595
var downDominant = downstream > upstream;

Source/FodyWeavers.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Weavers>
3+
<Costura/>
4+
</Weavers>

Source/Forms/NetGraphForm.Designer.cs

Lines changed: 68 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Forms/NetGraphForm.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ private Options Options
5050

5151
private Sample LastSample
5252
{
53-
get { return sampler.GetSamples().First(); }
53+
get { return sampler.First(); }
5454
}
5555

5656
private IEnumerable<Sample> LatestSamples
5757
{
58-
get { return sampler.GetSamples().Take(10); }
58+
get { return sampler.Take(10); }
5959
}
6060

6161
private ulong AverageDownloadSpeed
@@ -90,11 +90,13 @@ private void SyncOptionsWithUI(Options options) {
9090
}
9191

9292
private void UpdateStats() {
93-
dlRaw.Text = Math.Round(LastSample.Downstream / 1000f, 1).ToString("F1");
94-
ulRaw.Text = Math.Round(LastSample.Upstream / 1000f, 1).ToString("F1");
93+
const string FORMAT = "F1";
9594

96-
dlAvg.Text = Math.Round(AverageDownloadSpeed / 1000f, 1).ToString("F1");
97-
ulAvg.Text = Math.Round(AverageUploadSpeed / 1000f, 1).ToString("F1");
95+
dlRaw.Text = Math.Round(LastSample.Downstream / 1000f, 1).ToString(FORMAT);
96+
ulRaw.Text = Math.Round(LastSample.Upstream / 1000f, 1).ToString(FORMAT);
97+
98+
dlAvg.Text = Math.Round(AverageDownloadSpeed / 1000f, 1).ToString(FORMAT);
99+
ulAvg.Text = Math.Round(AverageUploadSpeed / 1000f, 1).ToString(FORMAT);
98100
}
99101

100102
private void UpdateTray() {
@@ -137,7 +139,7 @@ private void ToggleWindowVisibility() {
137139
private bool CanSnap(int clientEdge, int containerEdge, int tension) {
138140
const int DOCK_DISTANCE = 10;
139141

140-
return Math.Abs(clientEdge - containerEdge) <= DOCK_DISTANCE && tension <= DOCK_DISTANCE && tension >= -DOCK_DISTANCE;
142+
return Math.Abs(clientEdge - containerEdge) <= DOCK_DISTANCE && Math.Abs(tension) <= DOCK_DISTANCE;
141143
}
142144

143145
#region Event handlers

Source/Forms/NetGraphForm.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@
121121
<value>17, 17</value>
122122
</metadata>
123123
<metadata name="trayIcon.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
124-
<value>394, 17</value>
124+
<value>271, 17</value>
125125
</metadata>
126126
<metadata name="trayMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
127-
<value>489, 17</value>
127+
<value>366, 17</value>
128128
</metadata>
129129
<metadata name="samplerBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
130130
<value>97, 17</value>

Source/Forms/OptionsForm.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/NetworkInterfaceSampler.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
using System;
1+
using Nito.Collections;
2+
using System;
3+
using System.Collections;
24
using System.Collections.Generic;
35
using System.ComponentModel;
46
using System.Linq;
57
using System.Net.NetworkInformation;
68

79
namespace ScriptFUSION.UpDown_Meter {
8-
public class NetworkInterfaceSampler : INotifyPropertyChanged {
10+
public class NetworkInterfaceSampler : INotifyPropertyChanged, IEnumerable<Sample> {
911
private NetworkInterface nic;
12+
1013
private ulong maximumSpeed;
1114

1215
private PropertyChangedEventHandler propertyChangedHandlers;
1316

14-
public NetworkInterfaceSampler() {
15-
Reset();
16-
}
17-
1817
public delegate void SampleAddedDelegate(NetworkInterfaceSampler sampler, Sample sample);
1918

2019
public delegate void SamplesClearedDelegate(NetworkInterfaceSampler sampler);
@@ -30,9 +29,9 @@ event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
3029
}
3130

3231
/// <summary>
33-
/// Stack of relative samples from the current NetworkInterface.
32+
/// Collection of relative samples from the current NetworkInterface.
3433
/// </summary>
35-
private Stack<Sample> Samples { get; set; }
34+
private Deque<Sample> Samples { get; set; } = new Deque<Sample>(0x1000);
3635

3736
/// <summary>
3837
/// Latest absolute sample from the current NetworkInterface.
@@ -66,17 +65,21 @@ public ulong MaximumSpeed
6665
}
6766

6867
public void Reset() {
69-
Samples = new Stack<Sample>(Samples?.Count ?? 0x100);
68+
Samples.Clear();
7069
LastSample = null;
7170

7271
RaiseSamplesCleared();
7372
}
7473

7574
public void SampleAdapter() {
76-
var sample = CreateRelativeSample();
77-
Samples.Push(sample);
75+
// Remove oldest sample when at capacity.
76+
if (Samples.Count == Samples.Capacity) {
77+
Samples.RemoveFromBack();
78+
}
7879

79-
// TODO: Crop old samples.
80+
// Add latest sample.
81+
var sample = CreateRelativeSample();
82+
Samples.AddToFront(sample);
8083

8184
// TODO: Automatic calibration.
8285

@@ -103,10 +106,6 @@ private Sample CreateAbsoluteSample(IPInterfaceStatistics stats) {
103106
return new Sample(stats.BytesReceived, stats.BytesSent);
104107
}
105108

106-
public IEnumerable<Sample> GetSamples() {
107-
return Samples.ToArray<Sample>();
108-
}
109-
110109
private void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string property = null) {
111110
propertyChangedHandlers?.Invoke(this, new PropertyChangedEventArgs(property));
112111
}
@@ -118,5 +117,13 @@ private void RaiseSampleAdded(Sample sample) {
118117
public void RaiseSamplesCleared() {
119118
SamplesCleared?.Invoke(this);
120119
}
120+
121+
public IEnumerator<Sample> GetEnumerator() {
122+
return Samples.GetEnumerator();
123+
}
124+
125+
IEnumerator IEnumerable.GetEnumerator() {
126+
return Samples.GetEnumerator();
127+
}
121128
}
122129
}

Source/Resources/udm.ico

4.55 KB
Binary file not shown.

0 commit comments

Comments
 (0)