Esempio n. 1
0
        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;
            }));
        }
Esempio n. 2
0
        private async Task <IEnumerable <NatDevice> > DiscoverAsync(PortMapper portMapper, bool onlyOne, CancellationTokenSource cts)
        {
            TraceSource.LogInfo("Start Discovery");
            List <Task <IEnumerable <NatDevice> > > searcherTasks = new List <Task <IEnumerable <NatDevice> > >();

            if (portMapper.HasFlag(PortMapper.Upnp))
            {
                UpnpSearcher upnpSearcher = new UpnpSearcher(new IPAddressesProvider());
                upnpSearcher.DeviceFound += (sender, args) => { if (onlyOne)
                                                                {
                                                                    cts.Cancel();
                                                                }
                };
                searcherTasks.Add(upnpSearcher.Search(cts.Token));
            }
            if (portMapper.HasFlag(PortMapper.Pmp))
            {
                PmpSearcher 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");

            IEnumerable <NatDevice> devices = searcherTasks.SelectMany(x => x.Result);

            foreach (NatDevice device in devices)
            {
                string key = device.ToString();
                if (Devices.TryGetValue(key, out NatDevice nat))
                {
                    nat.Touch();
                }
                else
                {
                    Devices.Add(key, device);
                }
            }
            return(devices);
        }