private Task <IEnumerable <NatDevice> > DiscoverAsync(PortMapper portMapper, bool onlyOne, CancellationTokenSource cts) { TraceSource.LogInfo("Start Discovery"); var searcherTasks = new List <Task <IEnumerable <NatDevice> > >(); if (portMapper.HasFlag(PortMapper.Upnp)) { var upnpSearcher = new UpnpSearcher(new IPAddressesProvider()); upnpSearcher.DeviceFound += (sender, args) => { if (onlyOne) { cts.Cancel(); } }; searcherTasks.Add(upnpSearcher.Search(cts.Token)); } if (portMapper.HasFlag(PortMapper.Pmp)) { var pmpSearcher = new PmpSearcher(new IPAddressesProvider()); pmpSearcher.DeviceFound += (sender, args) => { if (onlyOne) { cts.Cancel(); } }; searcherTasks.Add(pmpSearcher.Search(cts.Token)); } return(TaskExtension.WhenAll(searcherTasks.ToArray()) .ContinueWith(t => { TraceSource.LogInfo("Stop Discovery"); var devices = searcherTasks.SelectMany(x => x.Result); foreach (var device in devices) { var key = device.ToString(); NatDevice nat; if (Devices.TryGetValue(key, out nat)) { nat.Touch(); } else { Devices.Add(key, device); } } return devices; })); }
private async Task<IEnumerable<NatDevice>> DiscoverAsync(PortMapper portMapper, bool onlyOne, CancellationTokenSource cts) { TraceSource.LogInfo("Start Discovery"); var searcherTasks = new List<Task<IEnumerable<NatDevice>>>(); if(portMapper.HasFlag(PortMapper.Upnp)) { var upnpSearcher = new UpnpSearcher(new IPAddressesProvider()); upnpSearcher.DeviceFound += (sender, args) => { if (onlyOne) cts.Cancel(); }; searcherTasks.Add(upnpSearcher.Search(cts.Token)); } if(portMapper.HasFlag(PortMapper.Pmp)) { var pmpSearcher = new PmpSearcher(new IPAddressesProvider()); pmpSearcher.DeviceFound += (sender, args) => { if (onlyOne) cts.Cancel(); }; searcherTasks.Add(pmpSearcher.Search(cts.Token)); } await Task.WhenAll(searcherTasks); TraceSource.LogInfo("Stop Discovery"); var devices = searcherTasks.SelectMany(x => x.Result); foreach (var device in devices) { var key = device.ToString(); NatDevice nat; if(Devices.TryGetValue(key, out nat)) { nat.Touch(); } else { Devices.Add(key, device); } } return devices; }