Skip to content

Commit

Permalink
1.2.0a1
Browse files Browse the repository at this point in the history
  • Loading branch information
MDevoldere committed Sep 8, 2018
1 parent 85661f8 commit 6af33bb
Show file tree
Hide file tree
Showing 27 changed files with 1,316 additions and 637 deletions.
50 changes: 33 additions & 17 deletions NetSpeedTray/NetInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Devoldere.NetSpeedTray
{
public class NetInterface
{

public int Id { get; private set; }

public string Name { get; private set; }
Expand All @@ -28,37 +27,52 @@ public class NetInterface
public NetTraffice Traffice { get; private set; }


public string StateText { get { return State.Text; } }

public string TrafficeDown { get { return Traffice.BytesReceivedSpeedText; } }

public string TrafficeUp { get { return Traffice.BytesSentSpeedText; } }


IPInterfaceProperties ipProperties;

int adressId;

public NetInterface(int _key)
public NetInterface(NetworkInterface _interface)
{
Id = _key;

State = new NetState();
Id = 0;
SetInterface(_interface);
}

Traffice = new NetTraffice();
public NetInterface(NetworkInterface _interface, int _key)
{
Id = _key;
SetInterface(_interface);
}

public NetState Update()
public NetInterface(int _key)
{
if (Id < NetworkInterface.GetAllNetworkInterfaces().Length)
Id = _key;

if (Id >= 0 && Id < NetworkInterface.GetAllNetworkInterfaces().Length)
{
OInterface = NetworkInterface.GetAllNetworkInterfaces()[Id];
Traffice.SetInterface(OInterface);
SetInterface(NetworkInterface.GetAllNetworkInterfaces()[Id]);
}
else
{
throw new Exception("No interface at postion " + Id.ToString());
}

UpdateInfo();
}
}

return State;
protected void SetInterface(NetworkInterface _interface)
{
OInterface = _interface;
State = new NetState(OInterface.OperationalStatus);
Traffice = new NetTraffice(OInterface);
Update();
}

protected void UpdateInfo()
public NetState Update()
{
State.SetState(OInterface.OperationalStatus);
Name = OInterface.Name + " (" + State.Text + ")";
Expand All @@ -70,7 +84,7 @@ protected void UpdateInfo()
Ip = "0.0.0.0";
Mbps = 0;
Text += "\rDown";
return;
//return State;
}

Mbps = (OInterface.Speed / 1000000);
Expand Down Expand Up @@ -105,11 +119,13 @@ protected void UpdateInfo()

Text += " - " + Mbps.ToString() + " Mbps\r" + macFormat + "\r" + Ip + "\r" + NetMask;

return State;
}

public void UpdateTraffice()
{
Traffice.Update();
if(State.Up)
Traffice.Update();
}
}
}
85 changes: 85 additions & 0 deletions NetSpeedTray/NetInterfaceList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

namespace Devoldere.NetSpeedTray
{
public class NetInterfaceList : List<NetInterface>
{
public int CountUp { get; private set; }

public int FirstUp { get; private set; }

public NetInterface SelectedInterface { get; private set; }

protected StringBuilder sb;


public NetInterfaceList() : base()
{
UpdateList();
}

public NetInterface GetInterface(int _id)
{
SelectedInterface = Find(x => (x.Id == _id));

if (null != SelectedInterface)
SelectedInterface.Update();

return SelectedInterface;
}

public NetInterface GetFirstUpInterface()
{
return GetInterface(FirstUp);
}

public override string ToString()
{
sb = new StringBuilder();
sb.Append("Interfaces (").Append(CountUp.ToString()).Append("/").Append(Count.ToString()).Append(")");
return sb.ToString();
}

public void UpdateList()
{
CountUp = 0;
FirstUp = -1;
Clear();

if (NetworkInterface.GetAllNetworkInterfaces().Length == 0)
{
return;
}

foreach(NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
&& nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
NetInterface ni = new NetInterface(nic, Count);

if (ni.State.Up)
{
CountUp++;

if (FirstUp == -1)
{
FirstUp = ni.Id;
}
}

ni.Update();
Add(ni);
}
}

if (FirstUp == -1)
{
FirstUp = (Count - 1);
}
}
}
}
102 changes: 42 additions & 60 deletions NetSpeedTray/NetInterfaceManager.cs
Original file line number Diff line number Diff line change
@@ -1,90 +1,72 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace Devoldere.NetSpeedTray
{
public class NetInterfaceManager
{
public List<NetInterface> InterfaceList { get; protected set; }
static protected readonly Timer TimerMain = new Timer { Interval = 10000 };

public int numInterfaceUp;
static protected readonly Timer TimerTraffice = new Timer { Interval = 1000 };

public int FirstUpInterfaceId;
static public readonly Timer TimerApp = new Timer { Interval = 1200 };

protected StringBuilder sb;
static public NetInterfaceList InterfaceList { get; private set; }

public NetInterfaceManager()
static public NetInterfaceList Init()
{
InterfaceList = new List<NetInterface>();
sb = new StringBuilder();
}

public void Update()
{
if (NetworkInterface.GetAllNetworkInterfaces().Length == 0)
{
throw new Exception("No network interface");
}

numInterfaceUp = 0;
FirstUpInterfaceId = -1;
InterfaceList.Clear();

for (int i = 0; i < NetworkInterface.GetAllNetworkInterfaces().Length; i++)
if(InterfaceList == null)
{
if (NetworkInterface.GetAllNetworkInterfaces()[i].NetworkInterfaceType != NetworkInterfaceType.Tunnel)
{
NetInterface oInterface = new NetInterface(i);
oInterface.Update();

if (oInterface.State.Up)
{
numInterfaceUp++;

if(FirstUpInterfaceId == -1)
{
FirstUpInterfaceId = i;
}
}

oInterface.Update();
InterfaceList.Add(oInterface);
}
InterfaceList = new NetInterfaceList();
TimerMain.Tick += Timer_Tick;
TimerTraffice.Tick += TimerTraffice_Tick;
}

if (InterfaceList.Count < 1)
{
throw new NullReferenceException("No valid network interface");
}

if(FirstUpInterfaceId == -1)
{
FirstUpInterfaceId = 0;
}
return InterfaceList;
}

public NetInterface GetInterface(int _id)
static public void Start()
{
NetInterface o = InterfaceList.Find(x => (x.Id == _id));

if (null != o)
o.Update();
TimerMain.Start();
TimerTraffice.Start();
TimerApp.Start();
}

return o;
static public void Stop()
{
TimerMain.Stop();
TimerTraffice.Stop();
TimerApp.Stop();
}

public NetInterface GetFirstUpInterface(int _id)
/// <summary>
/// Event timer Tick
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static public void Timer_Tick(object sender, EventArgs e)
{
return GetInterface(FirstUpInterfaceId);
foreach (NetInterface ni in InterfaceList)
{
ni.Update();
}
}

public override string ToString()
/// <summary>
/// Event timerTraffice Tick
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static public void TimerTraffice_Tick(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder("Interfaces ");
sb.Append("(").Append(numInterfaceUp.ToString()).Append("/").Append(InterfaceList.Count.ToString()).Append(")");
return sb.ToString();
foreach (NetInterface ni in InterfaceList)
{
ni.UpdateTraffice();
}
}

}
}
Loading

0 comments on commit 6af33bb

Please sign in to comment.