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

Loading…
Cancel
Save