Network Code Style (#98)

Thanks to @aquacomputer
This commit is contained in:
Aqua Computer
2019-07-15 11:52:03 +02:00
committed by Phyxion
parent d9fa3dbdf2
commit d1eae8e759
3 changed files with 216 additions and 213 deletions
+92 -106
View File
@@ -1,113 +1,99 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
using System;
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
using System;
using System.Diagnostics;
using System.Globalization;
using System.Net.NetworkInformation;
namespace OpenHardwareMonitor.Hardware.Nic
{
internal class Nic : Hardware
{
private Sensor dataUploaded;
private Sensor dataDownloaded;
private Sensor uploadSpeed;
private Sensor downloadSpeed;
private Sensor networkUtilization;
private long lastTick;
private long bytesUploaded;
private long bytesDownloaded;
private readonly NetworkInterface networkInterface;
using System.Globalization;
using System.Net.NetworkInformation;
public Nic(NetworkInterface networkInterface, ISettings settings)
: base(networkInterface.Name, new Identifier("nic", networkInterface.Id), settings)
{
this.networkInterface = networkInterface;
dataUploaded = new Sensor("Data Uploaded", 2, SensorType.Data, this, settings);
ActivateSensor(dataUploaded);
dataDownloaded = new Sensor("Data Downloaded", 3, SensorType.Data, this, settings);
ActivateSensor(dataDownloaded);
uploadSpeed = new Sensor("Upload Speed", 7, SensorType.Throughput, this, settings);
ActivateSensor(uploadSpeed);
downloadSpeed = new Sensor("Download Speed", 8, SensorType.Throughput, this, settings);
ActivateSensor(downloadSpeed);
networkUtilization = new Sensor("Network Utilization", 1, SensorType.Load, this, settings);
ActivateSensor(networkUtilization);
bytesUploaded = NetworkInterface.GetIPStatistics().BytesSent;
bytesDownloaded = NetworkInterface.GetIPStatistics().BytesReceived;
lastTick = Stopwatch.GetTimestamp();
}
public override HardwareType HardwareType
{
get
{
return HardwareType.NIC;
}
}
namespace OpenHardwareMonitor.Hardware.Nic {
internal class Nic : Hardware {
private Sensor dataUploaded;
private Sensor dataDownloaded;
private Sensor uploadSpeed;
private Sensor downloadSpeed;
private Sensor networkUtilization;
private long lastTick;
internal NetworkInterface NetworkInterface
{
get
{
return networkInterface;
}
}
private long bytesUploaded;
private long bytesDownloaded;
private readonly NetworkInterface networkInterface;
public override void Update()
{
long newTick = Stopwatch.GetTimestamp();
double dt = new TimeSpan(newTick - lastTick).TotalSeconds;
IPv4InterfaceStatistics interfaceStats = NetworkInterface.GetIPv4Statistics();
public Nic(NetworkInterface networkInterface, ISettings settings)
: base(networkInterface.Name, new Identifier("nic", networkInterface.Id), settings) {
this.networkInterface = networkInterface;
dataUploaded = new Sensor("Data Uploaded", 2, SensorType.Data, this, settings);
ActivateSensor(dataUploaded);
dataDownloaded = new Sensor("Data Downloaded", 3, SensorType.Data, this, settings);
ActivateSensor(dataDownloaded);
uploadSpeed = new Sensor("Upload Speed", 7, SensorType.Throughput, this, settings);
ActivateSensor(uploadSpeed);
downloadSpeed = new Sensor("Download Speed", 8, SensorType.Throughput, this, settings);
ActivateSensor(downloadSpeed);
networkUtilization = new Sensor("Network Utilization", 1, SensorType.Load, this, settings);
ActivateSensor(networkUtilization);
bytesUploaded = NetworkInterface.GetIPStatistics().BytesSent;
bytesDownloaded = NetworkInterface.GetIPStatistics().BytesReceived;
lastTick = Stopwatch.GetTimestamp();
}
// Report out the number of GB (2^30 Bytes) that this interface has up/downloaded. Note
// that these values can reset back at zero (eg: after waking from sleep).
dataUploaded.Value = (float)(interfaceStats.BytesSent / (double)0x40000000);
dataDownloaded.Value = (float)(interfaceStats.BytesReceived / (double)0x40000000);
public override HardwareType HardwareType {
get {
return HardwareType.NIC;
}
}
// Detect a reset in interface stats if the new total is less than what was previously
// seen. While setting the previous values to zero doesn't encapsulate the value the
// instant before the reset, it is the best approximation we have.
if (interfaceStats.BytesSent < bytesUploaded || interfaceStats.BytesReceived < bytesDownloaded)
{
bytesUploaded = 0;
bytesDownloaded = 0;
}
internal NetworkInterface NetworkInterface {
get {
return networkInterface;
}
}
long dBytesUploaded = interfaceStats.BytesSent - bytesUploaded;
long dBytesDownloaded = interfaceStats.BytesReceived - bytesDownloaded;
// Upload and download speeds are reported as the number of bytes transfered over the
// time difference since the last report. In this way, the values represent the average
// number of bytes up/downloaded in a second.
uploadSpeed.Value = (float)(dBytesUploaded / dt);
downloadSpeed.Value = (float)(dBytesDownloaded / dt);
// Network speed is in bits per second, so when calculating the load on the NIC we first
// grab the total number of bits up/downloaded
long dbits = (dBytesUploaded + dBytesDownloaded) * 8;
// Converts the ratio of total bits transferred over time over theoretical max bits
// transfer rate into a percentage load
double load = (dbits / dt / NetworkInterface.Speed) * 100;
// Finally clamp the value between 0% and 100% to avoid reporting nonsensical numbers
networkUtilization.Value = (float)Math.Min(Math.Max(load, 0), 100);
// Store the recorded values and time, so they can be used in the next update
bytesUploaded = interfaceStats.BytesSent;
bytesDownloaded = interfaceStats.BytesReceived;
lastTick = newTick;
}
}
}
public override void Update() {
long newTick = Stopwatch.GetTimestamp();
double dt = new TimeSpan(newTick - lastTick).TotalSeconds;
IPv4InterfaceStatistics interfaceStats = NetworkInterface.GetIPv4Statistics();
// Report out the number of GB (2^30 Bytes) that this interface has up/downloaded. Note
// that these values can reset back at zero (eg: after waking from sleep).
dataUploaded.Value = (float)(interfaceStats.BytesSent / (double)0x40000000);
dataDownloaded.Value = (float)(interfaceStats.BytesReceived / (double)0x40000000);
// Detect a reset in interface stats if the new total is less than what was previously
// seen. While setting the previous values to zero doesn't encapsulate the value the
// instant before the reset, it is the best approximation we have.
if (interfaceStats.BytesSent < bytesUploaded || interfaceStats.BytesReceived < bytesDownloaded) {
bytesUploaded = 0;
bytesDownloaded = 0;
}
long dBytesUploaded = interfaceStats.BytesSent - bytesUploaded;
long dBytesDownloaded = interfaceStats.BytesReceived - bytesDownloaded;
// Upload and download speeds are reported as the number of bytes transfered over the
// time difference since the last report. In this way, the values represent the average
// number of bytes up/downloaded in a second.
uploadSpeed.Value = (float)(dBytesUploaded / dt);
downloadSpeed.Value = (float)(dBytesDownloaded / dt);
// Network speed is in bits per second, so when calculating the load on the NIC we first
// grab the total number of bits up/downloaded
long dbits = (dBytesUploaded + dBytesDownloaded) * 8;
// Converts the ratio of total bits transferred over time over theoretical max bits
// transfer rate into a percentage load
double load = (dbits / dt / NetworkInterface.Speed) * 100;
// Finally clamp the value between 0% and 100% to avoid reporting nonsensical numbers
networkUtilization.Value = (float)Math.Min(Math.Max(load, 0), 100);
// Store the recorded values and time, so they can be used in the next update
bytesUploaded = interfaceStats.BytesSent;
bytesDownloaded = interfaceStats.BytesReceived;
lastTick = newTick;
}
}
}
+97 -107
View File
@@ -1,113 +1,103 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
namespace OpenHardwareMonitor.Hardware.Nic
{
internal class NicGroup : IGroup
{
private readonly ISettings _settings;
private List<Nic> _hardware = new List<Nic>();
private readonly object _scanLock = new object();
private readonly Dictionary<string, Nic> _nics = new Dictionary<string, Nic>();
public NicGroup(ISettings settings)
{
_settings = settings;
ScanNics(settings);
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAddressChanged;
}
private void ScanNics(ISettings settings)
{
// If no network is marked up (excluding loopback and tunnel) then don't scan
// for interfaces.
if (!NetworkInterface.GetIsNetworkAvailable())
{
return;
}
// When multiple events fire concurrently, we don't want threads interferring
// with others as they manipulate non-thread safe state.
lock (_scanLock)
{
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(DesiredNetworkType)
.OrderBy(x => x.Name);
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
var scanned = networkInterfaces.ToDictionary(x => x.Id, x => x);
var newNics = scanned.Where(x => !_nics.ContainsKey(x.Key));
var removedNics = _nics.Where(x => !scanned.ContainsKey(x.Key)).ToList();
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
foreach (var nic in removedNics)
{
nic.Value.Close();
_nics.Remove(nic.Key);
}
namespace OpenHardwareMonitor.Hardware.Nic {
internal class NicGroup : IGroup {
private readonly ISettings _settings;
private List<Nic> _hardware = new List<Nic>();
private readonly object _scanLock = new object();
private readonly Dictionary<string, Nic> _nics = new Dictionary<string, Nic>();
foreach (var nic in newNics)
{
_nics.Add(nic.Key, new Nic(nic.Value, settings));
}
public NicGroup(ISettings settings) {
_settings = settings;
ScanNics(settings);
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAddressChanged;
}
_hardware = _nics.Values.OrderBy(x => x.Name).ToList();
}
}
private void NetworkChange_NetworkAddressChanged(object sender, System.EventArgs e)
{
ScanNics(_settings);
}
private static bool DesiredNetworkType(NetworkInterface nic)
{
switch (nic.NetworkInterfaceType)
{
case NetworkInterfaceType.Loopback:
case NetworkInterfaceType.Tunnel:
case NetworkInterfaceType.Unknown:
return false;
default:
return true;
}
}
public string GetReport()
{
var report = new StringBuilder();
foreach (Nic hw in _hardware)
{
report.AppendLine(hw.NetworkInterface.Description);
report.AppendLine(hw.NetworkInterface.OperationalStatus.ToString());
report.AppendLine();
foreach (var sensor in hw.Sensors)
{
report.AppendLine(sensor.Name);
report.AppendLine(sensor.Value.ToString() + sensor.SensorType);
report.AppendLine();
}
}
return report.ToString();
}
public IEnumerable<IHardware> Hardware
{
get
{
return _hardware;
}
private void ScanNics(ISettings settings) {
// If no network is marked up (excluding loopback and tunnel) then don't scan
// for interfaces.
if (!NetworkInterface.GetIsNetworkAvailable()) {
return;
}
// When multiple events fire concurrently, we don't want threads interferring
// with others as they manipulate non-thread safe state.
lock (_scanLock) {
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(DesiredNetworkType)
.OrderBy(x => x.Name);
var scanned = networkInterfaces.ToDictionary(x => x.Id, x => x);
var newNics = scanned.Where(x => !_nics.ContainsKey(x.Key));
var removedNics = _nics.Where(x => !scanned.ContainsKey(x.Key)).ToList();
foreach (var nic in removedNics) {
nic.Value.Close();
_nics.Remove(nic.Key);
}
public void Close()
{
NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged -= NetworkChange_NetworkAddressChanged;
foreach (var nic in _hardware)
nic.Close();
}
}
}
foreach (var nic in newNics) {
_nics.Add(nic.Key, new Nic(nic.Value, settings));
}
_hardware = _nics.Values.OrderBy(x => x.Name).ToList();
}
}
private void NetworkChange_NetworkAddressChanged(object sender, System.EventArgs e) {
ScanNics(_settings);
}
private static bool DesiredNetworkType(NetworkInterface nic) {
switch (nic.NetworkInterfaceType) {
case NetworkInterfaceType.Loopback:
case NetworkInterfaceType.Tunnel:
case NetworkInterfaceType.Unknown:
return false;
default:
return true;
}
}
public string GetReport() {
var report = new StringBuilder();
foreach (Nic hw in _hardware) {
report.AppendLine(hw.NetworkInterface.Description);
report.AppendLine(hw.NetworkInterface.OperationalStatus.ToString());
report.AppendLine();
foreach (var sensor in hw.Sensors) {
report.AppendLine(sensor.Name);
report.AppendLine(sensor.Value.ToString() + sensor.SensorType);
report.AppendLine();
}
}
return report.ToString();
}
public IEnumerable<IHardware> Hardware {
get {
return _hardware;
}
}
public void Close() {
NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged -= NetworkChange_NetworkAddressChanged;
foreach (var nic in _hardware)
nic.Close();
}
}
}