This repo is archived. You can view files and clone it, but cannot push or open issues or pull requests.
Data-Rate/DataCore/GetAdapterStats.cs

134 lines
4.9 KiB
C#
Raw Normal View History

2021-10-06 14:04:27 +13:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Timers;
using System.Threading;
using System.Threading.Tasks;
namespace DataCore
{
public static class GetAdapterStats
{
static NetworkInterface[] systemAdapters = NetworkInterface.GetAllNetworkInterfaces();
static long[] currentAdapterTransfers = new long[systemAdapters.Length];
static long uploadStart = 0;
static long dowloadStart = 0;
// currentOverwriteStatus will equal zero on first pass, meaning no values should be read after the current value
// one means all values before should be read before the values following the current position
public static byte currentOverwriteStatus = 0;
public static int ratesIndex = 0;
static long[] UploadRateHistory = new long[1000];
static long[] DownloadRateHistory = new long[1000];
/// <summary>
/// Gets the rate of input data flow from the host computer
/// </summary>
/// <param name="sum">Declares that the input rates of multiple adapters should be summed. If false, returns the value of the highest transfer. Less accurate as a shorter response time is needed</param>
/// <returns>The number of bytes received since the last run</returns>
public static long InputRate(bool sum = true)
{
long returnRate = 0;
//foreach (NetworkInterface adapter in systemAdapters)
for(int i=0; i<systemAdapters.Length; i++)
{
if (sum)
{
long totalRate = systemAdapters[i].GetIPStatistics().BytesReceived;
Thread.Sleep(100);
returnRate = returnRate + ((systemAdapters[i].GetIPStatistics().BytesReceived - totalRate) * 10);
}
else
{
long totalRate = systemAdapters[i].GetIPStatistics().BytesReceived;
Thread.Sleep(10);
currentAdapterTransfers[i] = ((systemAdapters[i].GetIPStatistics().BytesReceived - totalRate) * 100);
}
}
if(!sum)
{
returnRate = currentAdapterTransfers.Max();
}
return returnRate;
}
/// <summary>
/// Gets the rate of output data flow from the host computer
/// </summary>
/// <param name="sum">Declares that the output rates of multiple adapters should be summed. If false, returns the value of the highest transfer</param>
/// <returns>The number of bytes sent since the last run</returns>
public static long OutputRate(bool sum = true)
{
long returnRate = 0;
for (int i = 0; i < systemAdapters.Length; i++)
{
if (sum)
{
long totalRate = systemAdapters[i].GetIPStatistics().BytesSent;
Thread.Sleep(100);
returnRate = returnRate + ((systemAdapters[i].GetIPStatistics().BytesSent - totalRate)* 10);
}
else
{
long totalRate = systemAdapters[i].GetIPStatistics().BytesSent;
Thread.Sleep(10);
currentAdapterTransfers[i] = ((systemAdapters[i].GetIPStatistics().BytesSent - totalRate) * 100);
}
}
if (!sum)
{
returnRate = currentAdapterTransfers.Max();
}
return returnRate;
}
public static void logRates()
{
for (int i = 0; i < systemAdapters.Length; i++)
{
uploadStart = uploadStart + systemAdapters[i].GetIPStatistics().BytesSent;
dowloadStart = dowloadStart + systemAdapters[i].GetIPStatistics().BytesReceived;
}
System.Timers.Timer logTime = new System.Timers.Timer(1000);
logTime.Elapsed += DoLog;
logTime.AutoReset = true;
logTime.Enabled = true;
logTime.Start();
}
private static void DoLog(Object source, ElapsedEventArgs e)
{
if(ratesIndex == UploadRateHistory.Length)
{
ratesIndex = 0;
}
long currentUp = 0;
long currentDown = 0;
long tempUp = 0;
long tempDown = 0;
for (int i = 0; i < systemAdapters.Length; i++)
{
currentUp = systemAdapters[i].GetIPStatistics().BytesSent;
currentDown = systemAdapters[i].GetIPStatistics().BytesReceived;
tempUp = tempUp + (currentUp - uploadStart);
tempDown = tempDown + (currentDown - dowloadStart);
}
uploadStart = tempUp;
dowloadStart = tempDown;
ratesIndex++;
return;
}
}
}