WingMan

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 35  →  ?path2? @ 36
/trunk/WingMan/Discovery/Discovery.cs
@@ -24,16 +24,16 @@
private static NatDiscoverer NatDiscoverer { get; set; }
private static TaskScheduler TaskScheduler { get; set; }
 
private static CancellationTokenSource UPnPCancellationTokenSource { get; set; }
private static CancellationTokenSource PMPCancellationTokenSource { get; set; }
private static CancellationTokenSource UpnPCancellationTokenSource { get; set; }
private static CancellationTokenSource PmpCancellationTokenSource { get; set; }
 
public void Dispose()
{
PMPCancellationTokenSource?.Dispose();
PMPCancellationTokenSource = null;
PmpCancellationTokenSource?.Dispose();
PmpCancellationTokenSource = null;
 
PMPCancellationTokenSource?.Dispose();
PMPCancellationTokenSource = null;
PmpCancellationTokenSource?.Dispose();
PmpCancellationTokenSource = null;
}
 
public event PortMapFailed OnPortMapFailed;
@@ -42,24 +42,56 @@
{
switch (type)
{
case DiscoveryType.UPnP:
return await CreateUPnPMapping(port);
case DiscoveryType.PMP:
return await CreatePMPPortMapping(port);
case DiscoveryType.Upnp:
return await CreateUpnPMapping(port);
case DiscoveryType.Pmp:
return await CreatePmpPortMapping(port);
default:
throw new ArgumentException("Unknown disocvery type");
throw new ArgumentException("Unknown discovery type");
}
}
 
private async Task<bool> CreateUPnPMapping(int port)
public async Task<bool> DeleteMapping(DiscoveryType type, int port)
{
switch (type)
{
case DiscoveryType.Upnp:
return await DeleteUpnPMapping(port);
case DiscoveryType.Pmp:
return await DeletePmpPortMapping(port);
default:
throw new ArgumentException("Unknown discovery type");
}
}
 
private async Task<bool> DeleteUpnPMapping(int port)
{
try
{
UPnPCancellationTokenSource = new CancellationTokenSource();
UpnPCancellationTokenSource = new CancellationTokenSource();
var linkedCancellationTokenSource =
CancellationTokenSource.CreateLinkedTokenSource(UPnPCancellationTokenSource.Token,
CancellationTokenSource.CreateLinkedTokenSource(UpnPCancellationTokenSource.Token,
CancellationTokenSource.Token);
var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource);
await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
 
return true;
}
catch
{
return false;
}
}
 
private async Task<bool> CreateUpnPMapping(int port)
{
try
{
UpnPCancellationTokenSource = new CancellationTokenSource();
var linkedCancellationTokenSource =
CancellationTokenSource.CreateLinkedTokenSource(UpnPCancellationTokenSource.Token,
CancellationTokenSource.Token);
var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource);
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
 
return true;
@@ -67,7 +99,7 @@
catch (Exception ex)
{
await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.UPnP, ex)),
OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Upnp, ex)),
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
 
return false;
@@ -74,15 +106,33 @@
}
}
 
private async Task<bool> CreatePMPPortMapping(int port)
private async Task<bool> DeletePmpPortMapping(int port)
{
try
{
PMPCancellationTokenSource = new CancellationTokenSource();
PmpCancellationTokenSource = new CancellationTokenSource();
var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
PMPCancellationTokenSource.Token,
PmpCancellationTokenSource.Token,
CancellationTokenSource.Token);
var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
return true;
}
catch
{
return false;
}
}
 
private async Task<bool> CreatePmpPortMapping(int port)
{
try
{
PmpCancellationTokenSource = new CancellationTokenSource();
var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
PmpCancellationTokenSource.Token,
CancellationTokenSource.Token);
var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
return true;
}
@@ -89,7 +139,7 @@
catch (Exception ex)
{
await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.PMP, ex)),
OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Pmp, ex)),
CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
 
return false;