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
        public static Task <TResult> TimeoutAfter <TResult>(this Task <TResult> task, TimeSpan timeout)
        {
#if DEBUG
            return(task);
#endif
            var timeoutCancellationTokenSource = new CancellationTokenSource();

            return(TaskExtension.WhenAny(task, TaskExtension.Delay(timeout, timeoutCancellationTokenSource.Token))
                   .ContinueWith(t =>
            {
                Task completedTask = t.Result;

                if (completedTask == task)
                {
                    timeoutCancellationTokenSource.Cancel();
                    return task;
                }
                throw new TimeoutException(
                    "The operation has timed out. The network is broken, router has gone or is too busy.");
            }).Unwrap());
        }