示例#1
0
        ///// <summary>
        ///// Sends a single (non-periodic) message to the specified IP address to see if it supports the
        ///// specified port mapping protocol, and begin listening indefinitely for responses.
        ///// </summary>
        ///// <param name="gatewayAddress">The IP address</param>
        ///// <param name="type"></param>
        //public void Search (IPAddress gatewayAddress, NatProtocol type)
        //{
        //	lock (Locker) {
        //		if (type == NatProtocol.Pmp) {
        //			PmpSearcher.Instance.SearchAsync (gatewayAddress).FireAndForget ();
        //		} else if (type == NatProtocol.Upnp) {
        //			UpnpSearcher.Instance.SearchAsync (gatewayAddress).FireAndForget ();
        //		} else {
        //			throw new InvalidOperationException ("Unsuported type given");
        //		}
        //	}
        //}

        /// <summary>
        /// Periodically send a multicast UDP message to scan for new devices, and begin listening indefinitely
        /// for responses.
        /// </summary>
        //  public void StartDiscovery(params NatProtocol [] devices)
        //	{
        //	}

        /// <summary>
        /// Stop listening for responses to the search messages, and cancel any pending searches.
        /// </summary>
        public void Dispose()
        {
            if (_pmpSearcher != null)
            {
                _pmpSearcher.Dispose();
                _pmpSearcher = null;
            }
            if (_upnpSearcher != null)
            {
                _upnpSearcher.Dispose();
                _upnpSearcher = null;
            }
        }
示例#2
0
        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);
        }
示例#3
0
        public async Task <bool> SearchAndConfigure(int[] localUdpPorts, int timeoutS = 20)
        {
            _upnpSearcher = new UpnpSearcher(this, d => Configure(d, localUdpPorts));
            _pmpSearcher  = new PmpSearcher(this, d => Configure(d, localUdpPorts));
            _pmpSearcher.SearchAsync().FireAndForget(this);
            _upnpSearcher.SearchAsync().FireAndForget(this);

            var sw = Stopwatch.StartNew();

            for (; ;)
            {
                await Task.Delay(10);

                if (_succeeded)
                {
                    return(true);
                }
                if (sw.Elapsed.TotalSeconds > timeoutS)
                {
                    return(false);
                }
            }
        }