WingMan – Blame information for rev 36

Subversion Repositories:
Rev:
Rev Author Line No. Line
32 office 1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
4 using Open.Nat;
5  
6 namespace WingMan.Discovery
7 {
8 public class Discovery : IDisposable
9 {
10 public delegate void PortMapFailed(object sender, DiscoveryFailedEventArgs args);
11  
12 public Discovery()
13 {
14 NatDiscoverer = new NatDiscoverer();
15 }
16  
17 public Discovery(CancellationTokenSource cancellationTokenSource, TaskScheduler taskScheduler) : this()
18 {
19 CancellationTokenSource = cancellationTokenSource;
20 TaskScheduler = taskScheduler;
21 }
22  
23 private static CancellationTokenSource CancellationTokenSource { get; set; }
24 private static NatDiscoverer NatDiscoverer { get; set; }
25 private static TaskScheduler TaskScheduler { get; set; }
26  
36 office 27 private static CancellationTokenSource UpnPCancellationTokenSource { get; set; }
28 private static CancellationTokenSource PmpCancellationTokenSource { get; set; }
32 office 29  
30 public void Dispose()
31 {
36 office 32 PmpCancellationTokenSource?.Dispose();
33 PmpCancellationTokenSource = null;
32 office 34  
36 office 35 PmpCancellationTokenSource?.Dispose();
36 PmpCancellationTokenSource = null;
32 office 37 }
38  
39 public event PortMapFailed OnPortMapFailed;
40  
41 public async Task<bool> CreateMapping(DiscoveryType type, int port)
42 {
43 switch (type)
44 {
36 office 45 case DiscoveryType.Upnp:
46 return await CreateUpnPMapping(port);
47 case DiscoveryType.Pmp:
48 return await CreatePmpPortMapping(port);
32 office 49 default:
36 office 50 throw new ArgumentException("Unknown discovery type");
32 office 51 }
52 }
53  
36 office 54 public async Task<bool> DeleteMapping(DiscoveryType type, int port)
32 office 55 {
36 office 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 {
32 office 69 try
70 {
36 office 71 UpnPCancellationTokenSource = new CancellationTokenSource();
32 office 72 var linkedCancellationTokenSource =
36 office 73 CancellationTokenSource.CreateLinkedTokenSource(UpnPCancellationTokenSource.Token,
32 office 74 CancellationTokenSource.Token);
75 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource);
36 office 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 }
85  
86 private async Task<bool> CreateUpnPMapping(int port)
87 {
88 try
89 {
90 UpnPCancellationTokenSource = new CancellationTokenSource();
91 var linkedCancellationTokenSource =
92 CancellationTokenSource.CreateLinkedTokenSource(UpnPCancellationTokenSource.Token,
93 CancellationTokenSource.Token);
94 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Upnp, linkedCancellationTokenSource);
32 office 95 await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
96  
97 return true;
98 }
99 catch (Exception ex)
100 {
101 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
36 office 102 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Upnp, ex)),
32 office 103 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
104  
105 return false;
106 }
107 }
108  
36 office 109 private async Task<bool> DeletePmpPortMapping(int port)
32 office 110 {
111 try
112 {
36 office 113 PmpCancellationTokenSource = new CancellationTokenSource();
32 office 114 var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
36 office 115 PmpCancellationTokenSource.Token,
32 office 116 CancellationTokenSource.Token);
117 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
36 office 118 await device.DeletePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
119 return true;
120 }
121 catch
122 {
123 return false;
124 }
125 }
126  
127 private async Task<bool> CreatePmpPortMapping(int port)
128 {
129 try
130 {
131 PmpCancellationTokenSource = new CancellationTokenSource();
132 var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
133 PmpCancellationTokenSource.Token,
134 CancellationTokenSource.Token);
135 var device = await NatDiscoverer.DiscoverDeviceAsync(PortMapper.Pmp, linkedCancellationTokenSource);
32 office 136 await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, "WingMan Host"));
137 return true;
138 }
139 catch (Exception ex)
140 {
141 await Task.Delay(0, CancellationTokenSource.Token).ContinueWith(_ =>
36 office 142 OnPortMapFailed?.Invoke(this, new DiscoveryFailedEventArgs(DiscoveryType.Pmp, ex)),
32 office 143 CancellationTokenSource.Token, TaskContinuationOptions.None, TaskScheduler);
144  
145 return false;
146 }
147 }
148 }
149 }