-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Preparing V2.7 (slightly updating the design).
feat: updated the Adapters page and logic to rely on ObservableObject. fix: modified the bandwidth computation for the Adapters page, still off compared to the task manager's one though :thinking_face:
Showing
8 changed files
with
127 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,62 @@ | ||
using System.ComponentModel; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using System.Diagnostics; | ||
using System.Net.NetworkInformation; | ||
|
||
namespace Wokhan.WindowsFirewallNotifier.Console.ViewModels; | ||
|
||
public partial class ExposedInterfaceView : INotifyPropertyChanged | ||
public partial class ExposedInterfaceView : ObservableObject | ||
{ | ||
public event PropertyChangedEventHandler? PropertyChanged; | ||
public string MAC { get; init; } | ||
|
||
public void NotifyPropertyChanged(string propertyName) | ||
{ | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | ||
} | ||
[ObservableProperty] | ||
private NetworkInterface _information; | ||
|
||
public string MAC => String.Join(":", Information.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2"))); | ||
[ObservableProperty] | ||
public IPInterfaceStatistics _statistics; | ||
|
||
public NetworkInterface Information { get; private set; } | ||
[ObservableProperty] | ||
private IPInterfaceProperties _properties; | ||
|
||
public IPInterfaceStatistics Statistics => Information.GetIPStatistics(); | ||
[ObservableProperty] | ||
private ComputedBandwidth _bandwidth = new(); | ||
|
||
private readonly int delay = 400; // Arbitrary delay, must be lower than the Adapters page timer | ||
private readonly Stopwatch sw = new(); | ||
private async Task ComputeBandwidth() | ||
{ | ||
var initial = Information.GetIPStatistics(); | ||
sw.Restart(); | ||
await Task.Delay(delay); | ||
var final = Information.GetIPStatistics(); | ||
sw.Stop(); | ||
|
||
var inb = AdjustBandwidth(initial.BytesReceived, final.BytesReceived, sw.ElapsedMilliseconds); | ||
var outb = AdjustBandwidth(initial.BytesSent, final.BytesSent, sw.ElapsedMilliseconds); | ||
|
||
Bandwidth = new ComputedBandwidth(inb, outb); | ||
} | ||
|
||
private static double AdjustBandwidth(long bytes1, long bytes2, long elapsedms) | ||
{ | ||
return Math.Max(0, (bytes2 - bytes1) * 8 / elapsedms * 1000); | ||
} | ||
|
||
public IPInterfaceProperties Properties => Information.GetIPProperties(); | ||
public record ComputedBandwidth(double In = 0, double Out = 0); | ||
|
||
public ExposedInterfaceView(NetworkInterface inter) | ||
public ExposedInterfaceView(NetworkInterface netInterface) | ||
{ | ||
this.Information = inter; | ||
this.MAC = String.Join(":", netInterface.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2"))); | ||
|
||
UpdateInner(netInterface); | ||
} | ||
|
||
internal void UpdateInner(NetworkInterface inter) | ||
internal void UpdateInner(NetworkInterface netInterface) | ||
{ | ||
this.Information = inter; | ||
NotifyPropertyChanged(nameof(Information)); | ||
NotifyPropertyChanged(nameof(Statistics)); | ||
NotifyPropertyChanged(nameof(Properties)); | ||
NotifyPropertyChanged(nameof(MAC)); | ||
Information = netInterface; | ||
|
||
_ = ComputeBandwidth(); | ||
|
||
Statistics = Information.GetIPStatistics(); | ||
Properties = Information.GetIPProperties(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters