diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1e9f5e4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,27 @@ +# +# Documentation: http://EditorConfig.org +# VS15 plug-in: http://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328 +# Natively supported in VS17 +# +# Configure editor for this project to match original project indentation scheme. + +# top-most EditorConfig file +root = true + +[*] +trim_trailing_whitespace = true + +[*.{cs,vb}] +indent_style = space +indent_size = 2 +dotnet_style_object_initializer = true:suggestion +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion + +# New line preferences +csharp_new_line_before_open_brace = none +csharp_new_line_before_else = false +csharp_new_line_before_catch = false +csharp_new_line_before_finally = false +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true diff --git a/Hardware/Nic/Nic.cs b/Hardware/Nic/Nic.cs index 1aa95e7..9f66010 100644 --- a/Hardware/Nic/Nic.cs +++ b/Hardware/Nic/Nic.cs @@ -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 - -*/ - -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 + +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; + } + } +} \ No newline at end of file diff --git a/Hardware/Nic/NicGroup.cs b/Hardware/Nic/NicGroup.cs index 3c16824..189c186 100644 --- a/Hardware/Nic/NicGroup.cs +++ b/Hardware/Nic/NicGroup.cs @@ -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 _hardware = new List(); - private readonly object _scanLock = new object(); - private readonly Dictionary _nics = new Dictionary(); - - 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 - 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 _hardware = new List(); + private readonly object _scanLock = new object(); + private readonly Dictionary _nics = new Dictionary(); - 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 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 Hardware { + get { + return _hardware; + } + } + + public void Close() { + NetworkChange.NetworkAddressChanged -= NetworkChange_NetworkAddressChanged; + NetworkChange.NetworkAvailabilityChanged -= NetworkChange_NetworkAddressChanged; + foreach (var nic in _hardware) + nic.Close(); + } + } +} \ No newline at end of file