fix network methods not shutting down

pull/1154/head
Luke Pulverenti 7 years ago
parent bcaf9bd19c
commit 5759ba8656

@ -43,7 +43,6 @@ namespace Mono.Nat
{ {
public static class NatUtility public static class NatUtility
{ {
private static ManualResetEvent searching;
public static event EventHandler<DeviceEventArgs> DeviceFound; public static event EventHandler<DeviceEventArgs> DeviceFound;
public static event EventHandler<DeviceEventArgs> DeviceLost; public static event EventHandler<DeviceEventArgs> DeviceLost;
@ -68,8 +67,6 @@ namespace Mono.Nat
NatProtocol.Pmp NatProtocol.Pmp
}; };
searching = new ManualResetEvent(false);
controllers = new List<ISearcher>(); controllers = new List<ISearcher>();
controllers.Add(PmpSearcher.Instance); controllers.Add(PmpSearcher.Instance);
@ -86,8 +83,6 @@ namespace Mono.Nat
DeviceLost(sender, args); DeviceLost(sender, args);
}; };
}); });
Task.Factory.StartNew(SearchAndListen, TaskCreationOptions.LongRunning);
} }
internal static void Log(string format, params object[] args) internal static void Log(string format, params object[] args)
@ -97,12 +92,10 @@ namespace Mono.Nat
logger.Debug(format, args); logger.Debug(format, args);
} }
private static async Task SearchAndListen() private static async Task SearchAndListen(CancellationToken cancellationToken)
{ {
while (true) while (!cancellationToken.IsCancellationRequested)
{ {
searching.WaitOne();
try try
{ {
var enabledProtocols = EnabledProtocols.ToList(); var enabledProtocols = EnabledProtocols.ToList();
@ -144,14 +137,45 @@ namespace Mono.Nat
} }
} }
private static CancellationTokenSource _currentCancellationTokenSource;
private static object _runSyncLock = new object();
public static void StartDiscovery() public static void StartDiscovery()
{ {
searching.Set(); lock (_runSyncLock)
{
if (_currentCancellationTokenSource == null)
{
return;
}
var tokenSource = new CancellationTokenSource();
_currentCancellationTokenSource = tokenSource;
//Task.Factory.StartNew(() => SearchAndListen(tokenSource.Token), tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
} }
public static void StopDiscovery() public static void StopDiscovery()
{ {
searching.Reset(); lock (_runSyncLock)
{
var tokenSource = _currentCancellationTokenSource;
if (tokenSource != null)
{
try
{
tokenSource.Cancel();
tokenSource.Dispose();
}
catch
{
}
_currentCancellationTokenSource = null;
}
}
} }
//checks if an IP address is a private address space as defined by RFC 1918 //checks if an IP address is a private address space as defined by RFC 1918
@ -159,7 +183,8 @@ namespace Mono.Nat
{ {
byte[] ba = address.GetAddressBytes(); byte[] ba = address.GetAddressBytes();
switch ((int)ba[0]) { switch ((int)ba[0])
{
case 10: case 10:
return true; //10.x.x.x return true; //10.x.x.x
case 172: case 172:

Loading…
Cancel
Save