Now tracking interface state on start and restoring this state when process exits. This may cause longer service shutdown times if there are lots of interfaces to modify. Need testing.

IPC
Sean McArde 2016-09-21 14:13:28 -07:00
parent 2b2b88381e
commit 152db18c47
2 changed files with 38 additions and 9 deletions

View File

@ -8,14 +8,14 @@ namespace WifiSitter
/// <summary>
/// Class used to track state of detected network adapters
/// </summary>
public class NetworkState
{
public class NetworkState {
#region fields
private List<SitterNic> _nics;
private bool _checkNet;
private bool _netAvailable;
private bool _processingState;
private string[] _ignoreAdapters; // List of Nic names to ignore during normal operation
private List<string[]> _originalNicState = new List<string[]>();
#endregion // fields
@ -25,12 +25,20 @@ namespace WifiSitter
if (NicWhitelist == null)
NicWhitelist = new string[] { };
this.Nics = QueryNetworkAdapters(NicWhitelist);
// Loop through nics and add id:state to _originalNicState list
Nics.ForEach(x => _originalNicState.Add(new string[] { x.Id, x.IsEnabled.ToString() }));
_ignoreAdapters = NicWhitelist;
Initialize();
}
public NetworkState(List<SitterNic> Nics, string[] NicWhitelist) {
this.Nics = Nics;
// Loop through nics and add id:state to _originalNicState list
Nics.ForEach(x => _originalNicState.Add(new string[] { x.Id, x.IsEnabled.ToString() }));
_ignoreAdapters = NicWhitelist;
Initialize();
}
@ -60,9 +68,13 @@ namespace WifiSitter
}
public void UpdateNics(List<SitterNic> Nics) {
foreach (var n in Nics) {
if (!_originalNicState.Any(x => x[0] == n.Id)) _originalNicState.Add(new string[] { n.Id, n.IsEnabled.ToString() });
}
this.Nics = Nics;
}
internal static List<SitterNic> QueryNetworkAdapters(string[] WhiteList) {
List<SitterNic> result = new List<SitterNic>();
foreach (var n in NetworkInterface.GetAllNetworkInterfaces().Where(x => (x.NetworkInterfaceType != NetworkInterfaceType.Loopback
@ -95,6 +107,9 @@ namespace WifiSitter
private set { _nics = value; }
}
public List<string[]> OriginalNicState {
get { return _originalNicState; }
}
public bool CheckNet {
get { return _checkNet; }

View File

@ -33,6 +33,8 @@ namespace WifiSitter
this.AutoLog = true;
this.CanPauseAndContinue = true;
}
Intialize();
}
#endregion // constructor
@ -119,7 +121,7 @@ namespace WifiSitter
List<NetshInterface> notInNetstate = new List<NetshInterface>();
// Skip checking for disabled adapters we already know about
// Only check disabled adapters we don't already know about
foreach (var n in netsh) {
if (!nics.Any(x => x.Name == n.InterfaceName)) {
notInNetstate.Add(n);
@ -177,8 +179,7 @@ namespace WifiSitter
Console.WriteLine(" {0}", log);
Console.ResetColor();
}
public void WriteLog(LogType type, params string[] msg) {
if (this.ServiceExecutionMode == ServiceExecutionMode.Console) {
@ -232,9 +233,7 @@ namespace WifiSitter
}
private void WorkerThreadFunc() {
Intialize();
while (!_shutdownEvent.WaitOne(0)) {
if (_paused) {
@ -309,6 +308,20 @@ namespace WifiSitter
}
}
private void ResetNicState (NetworkState netstate) {
foreach (var n in netstate.OriginalNicState) {
var id = n[0];
var stat = n[1];
SitterNic now = netstate.Nics.Where(x => x.Id == id).FirstOrDefault();
if (now != null) {
if (stat.ToLower() != now.IsEnabled.ToString().ToLower()) {
if (stat == true.ToString()) now.Enable();
else now.Disable();
}
}
}
}
#endregion // methods
@ -322,6 +335,7 @@ namespace WifiSitter
}
protected override void OnStopImpl() {
ResetNicState(netstate);
_shutdownEvent.Set();
if (!_thread.Join(3000)) {
_thread.Abort();