WingMan – Diff between revs 32 and 36

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 32 Rev 36
Line 22... Line 22...
22   22  
23 private static CancellationTokenSource CancellationTokenSource { get; set; } 23 private static CancellationTokenSource CancellationTokenSource { get; set; }
24 private static NatDiscoverer NatDiscoverer { get; set; } 24 private static NatDiscoverer NatDiscoverer { get; set; }
Line 25... Line 25...
25 private static TaskScheduler TaskScheduler { get; set; } 25 private static TaskScheduler TaskScheduler { get; set; }
26   26  
Line 27... Line 27...
27 private static CancellationTokenSource UPnPCancellationTokenSource { get; set; } 27 private static CancellationTokenSource UpnPCancellationTokenSource { get; set; }
28 private static CancellationTokenSource PMPCancellationTokenSource { get; set; } 28 private static CancellationTokenSource PmpCancellationTokenSource { get; set; }
29   29  
30 public void Dispose() 30 public void Dispose()
Line 31... Line 31...
31 { 31 {
32 PMPCancellationTokenSource?.Dispose(); 32 PmpCancellationTokenSource?.Dispose();
33 PMPCancellationTokenSource = null; 33 PmpCancellationTokenSource = null;
Line 34... Line 34...
34   34  
Line 35... Line 35...
35 PMPCancellationTokenSource?.Dispose(); 35 PmpCancellationTokenSource?.Dispose();
36 PMPCancellationTokenSource = null; 36 PmpCancellationTokenSource = null;
37 } 37 }
38   38  
39 public event PortMapFailed OnPortMapFailed; 39 public event PortMapFailed OnPortMapFailed;
40   40  
41 public async Task<bool> CreateMapping(DiscoveryType type, int port) 41 public async Task<bool> CreateMapping(DiscoveryType type, int port)
42 { 42 {
43 switch (type) 43 switch (type)
44 { 44 {
45 case DiscoveryType.UPnP: 45 case DiscoveryType.Upnp:
46 return await CreateUPnPMapping(port); 46 return await CreateUpnPMapping(port);
Line -... Line 47...
-   47 case DiscoveryType.Pmp:
-   48 return await CreatePmpPortMapping(port);
-   49 default:
-   50 throw new ArgumentException("Unknown discovery type");
-   51 }
-   52 }
-   53  
-   54 public async Task<bool> DeleteMapping(DiscoveryType type, int port)
-   55 {
-   56 switch (type)
-   57 {
-   58 case DiscoveryType.Upnp:
-   59 return await DeleteUpnPMapping(port);
-   60 case DiscoveryType.Pmp:
-   61 return await DeletePmpPortMapping(port);
-   62 default:
-   63 throw new ArgumentException("Unknown discovery type");
-   64 }
-   65 }
-   66  
-   67 private async Task<bool> DeleteUpnPMapping(int port)
-   68 {
-   69 try
-   70 {
-   71 UpnPCancellationTokenSource = new CancellationTokenSource();
-   72 var linkedCancellationTokenSource =
-   73 CancellationTokenSource.CreateLinkedTokenSource(UpnPCancellationTokenSource.Token,
-   74 CancellationTokenSource.Token);
-   75 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource);
-   76 await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
-   77  
-   78 return true;
47 case DiscoveryType.PMP: 79 }
48 return await CreatePMPPortMapping(port); 80 catch
49 default: 81 {
50 throw new ArgumentException("Unknown disocvery type"); 82 return false;
51 } 83 }
52 } 84 }
53   85  
54 private async Task<bool> CreateUPnPMapping(int port) 86 private async Task<bool> CreateUpnPMapping(int port)
55 { 87 {
56 try 88 try
Line 57... Line 89...
57 { 89 {
58 UPnPCancellationTokenSource = new CancellationTokenSource(); 90 UpnPCancellationTokenSource = new CancellationTokenSource();
59 var linkedCancellationTokenSource = 91 var linkedCancellationTokenSource =
60 CancellationTokenSource.CreateLinkedTokenSource(UPnPCancellationTokenSource.Token, 92 CancellationTokenSource.CreateLinkedTokenSource(UpnPCancellationTokenSource.Token,
61 CancellationTokenSource.Token); 93 CancellationTokenSource.Token);
62 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource); 94 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource);
63 await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host")); 95 await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
Line 64... Line 96...
64   96  
65 return true; 97 return true;
66 } 98 }
Line -... Line 99...
-   99 catch (Exception ex)
-   100 {
-   101 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
-   102 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Upnp, ex)),
-   103 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
-   104  
-   105 return false;
-   106 }
-   107 }
-   108  
-   109 private async Task<bool> DeletePmpPortMapping(int port)
-   110 {
-   111 try
-   112 {
-   113 PmpCancellationTokenSource = new CancellationTokenSource();
-   114 var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
-   115 PmpCancellationTokenSource.Token,
-   116 CancellationTokenSource.Token);
67 catch (Exception ex) 117 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
68 { 118 await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
69 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ => 119 return true;
70 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.UPnP, ex)), 120 }
71 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler); 121 catch
72   122 {
73 return false; 123 return false;
74 } 124 }
75 } 125 }
76   126  
77 private async Task<bool> CreatePMPPortMapping(int port) 127 private async Task<bool> CreatePmpPortMapping(int port)
78 { 128 {
79 try 129 try
80 { 130 {
81 PMPCancellationTokenSource = new CancellationTokenSource(); 131 PmpCancellationTokenSource = new CancellationTokenSource();
82 var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource( 132 var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
83 PMPCancellationTokenSource.Token, 133 PmpCancellationTokenSource.Token,
Line 84... Line 134...
84 CancellationTokenSource.Token); 134 CancellationTokenSource.Token);
85 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource); 135 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
86 await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host")); 136 await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));