-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
MDevoldere
committed
Sep 8, 2018
1 parent
85661f8
commit 6af33bb
Showing
27 changed files
with
1,316 additions
and
637 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
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); | ||
} | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.