WingMan – Diff between revs 32 and 36

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 32 Rev 36
1 using System; 1 using System;
2 using System.Threading; 2 using System.Threading;
3 using System.Threading.Tasks; 3 using System.Threading.Tasks;
4 using Open.Nat; 4 using Open.Nat;
5   5  
6 namespace WingMan.Discovery 6 namespace WingMan.Discovery
7 { 7 {
8 public class Discovery : IDisposable 8 public class Discovery : IDisposable
9 { 9 {
10 public delegate void PortMapFailed(object sender, DiscoveryFailedEventArgs args); 10 public delegate void PortMapFailed(object sender, DiscoveryFailedEventArgs args);
11   11  
12 public Discovery() 12 public Discovery()
13 { 13 {
14 NatDiscoverer = new NatDiscoverer(); 14 NatDiscoverer = new NatDiscoverer();
15 } 15 }
16   16  
17 public Discovery(CancellationTokenSource cancellationTokenSource, TaskScheduler taskScheduler) : this() 17 public Discovery(CancellationTokenSource cancellationTokenSource, TaskScheduler taskScheduler) : this()
18 { 18 {
19 CancellationTokenSource = cancellationTokenSource; 19 CancellationTokenSource = cancellationTokenSource;
20 TaskScheduler = taskScheduler; 20 TaskScheduler = taskScheduler;
21 } 21 }
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; }
25 private static TaskScheduler TaskScheduler { get; set; } 25 private static TaskScheduler TaskScheduler { get; set; }
26   26  
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()
31 { 31 {
32 PMPCancellationTokenSource?.Dispose(); 32 PmpCancellationTokenSource?.Dispose();
33 PMPCancellationTokenSource = null; 33 PmpCancellationTokenSource = null;
34   34  
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);
47 case DiscoveryType.PMP: 47 case DiscoveryType.Pmp:
48 return await CreatePMPPortMapping(port); 48 return await CreatePmpPortMapping(port);
49 default: 49 default:
50 throw new ArgumentException("Unknown disocvery type"); 50 throw new ArgumentException("Unknown discovery type");
51 } 51 }
52 } 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;
-   79 }
-   80 catch
-   81 {
-   82 return false;
-   83 }
-   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
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"));
64   96  
65 return true; 97 return true;
66 } 98 }
67 catch (Exception ex) 99 catch (Exception ex)
68 { 100 {
69 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ => 101 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
70 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.UPnP, ex)), 102 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Upnp, ex)),
71 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler); 103 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
72   104  
73 return false; 105 return false;
74 } 106 }
75 } 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);
-   117 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
-   118 await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
-   119 return true;
-   120 }
-   121 catch
-   122 {
-   123 return false;
-   124 }
-   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,
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"));
87 return true; 137 return true;
88 } 138 }
89 catch (Exception ex) 139 catch (Exception ex)
90 { 140 {
91 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ => 141 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
92 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.PMP, ex)), 142 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Pmp, ex)),
93 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler); 143 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
94   144  
95 return false; 145 return false;
96 } 146 }
97 } 147 }
98 } 148 }
99 } 149 }
100   150