Ejemplo n.º 1
0
        /// <summary>
        ///     Returns all available domains with services on them
        /// </summary>
        /// <param name="scanTime">Default is 2 seconds</param>
        /// <param name="cancellationToken"></param>
        /// <param name="retries">If the socket is busy, the number of times the resolver should retry</param>
        /// <param name="retryDelayMilliseconds">The delay time between retries</param>
        /// <param name="callback">Called per record returned as they come in.</param>
        /// <returns></returns>
        public static async Task <ILookup <string, string> > BrowseDomainsAsync(TimeSpan scanTime                   = default(TimeSpan),
                                                                                int retries                         = 2,
                                                                                int retryDelayMilliseconds          = 2000,
                                                                                Action <string, string> callback    = null,
                                                                                CancellationToken cancellationToken = default(CancellationToken))

        {
            if (retries <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retries));
            }
            if (retryDelayMilliseconds <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retryDelayMilliseconds));
            }
            if (scanTime == default(TimeSpan))
            {
                scanTime = TimeSpan.FromSeconds(2);
            }

            var options = new BrowseDomainsOptions
            {
                Retries    = retries,
                RetryDelay = TimeSpan.FromMilliseconds(retryDelayMilliseconds),
                ScanTime   = scanTime
            };

            return(await BrowseDomainsAsync(options, callback, cancellationToken).ConfigureAwait(false));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Returns all available domains with services on them
        /// </summary>
        /// <param name="options"></param>
        /// <param name="callback">Called per record returned as they come in.</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task <ILookup <string, string> > BrowseDomainsAsync(BrowseDomainsOptions options,
                                                                                Action <string, string> callback    = null,
                                                                                CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Action <string, Response> wrappedAction = null;

            if (callback != null)
            {
                wrappedAction = (address, response) =>
                {
                    foreach (var service in BrowseResponseParser(response))
                    {
                        callback(service, address);
                    }
                };
            }

            var dict = await ResolveInternal(options,
                                             wrappedAction,
                                             cancellationToken)
                       .ConfigureAwait(false);

            var r = from kvp in dict
                    from service in BrowseResponseParser(kvp.Value)
                    select new { Service = service, Address = kvp.Key };

            return(r.ToLookup(k => k.Service, k => k.Address));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Returns all available domains with services on them
        /// </summary>
        /// <param name="options"></param>
        /// <param name="callback">Called per record returned as they come in.</param>
        /// <param name="cancellationToken"></param>
        /// <param name="netInterfacesToSendRequestOn">The network interfaces/adapters to use. Use all if null</param>
        /// <returns></returns>
        public static async Task <ILookup <string, string> > BrowseDomainsAsync(BrowseDomainsOptions options,
                                                                                Action <string, string> callback    = null,
                                                                                CancellationToken cancellationToken = default(CancellationToken),
                                                                                System.Net.NetworkInformation.NetworkInterface[] netInterfacesToSendRequestOn = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

#if !__IOS__
            return(await BrowseDomainsAsyncOriginal(options, callback, cancellationToken, netInterfacesToSendRequestOn));
#else
            if (UIDevice.CurrentDevice.CheckSystemVersion(14, 5))
            {
                if (UseBSDSocketsZeroconfOniOS)
                {
                    return(await BrowseDomainsAsyncOriginal(options, callback, cancellationToken, netInterfacesToSendRequestOn));
                }
                else
                {
                    return(await ZeroconfNetServiceBrowser.BrowseDomainsAsync(options, callback, cancellationToken, netInterfacesToSendRequestOn));
                }
            }
            else
            {
                return(await BrowseDomainsAsyncOriginal(options, callback, cancellationToken, netInterfacesToSendRequestOn));
            }
#endif
        }
Ejemplo n.º 4
0
        public static IObservable <DomainService> BrowseDomains(BrowseDomainsOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(Observable.Create <DomainService>(
                       async(obs, cxl) =>
            {
                try
                {
                    Action <string, string> cb = (d, s) => obs.OnNext(new DomainService(d, s));
                    await BrowseDomainsAsync(options, cb, cxl);
                }
                catch (OperationCanceledException)
                { }
                catch (Exception e)
                {
                    obs.OnError(e);
                }
                finally
                {
                    obs.OnCompleted();
                }
            }));
        }
Ejemplo n.º 5
0
        public static IObservable <DomainService> BrowseDomains(TimeSpan scanTime          = default(TimeSpan),
                                                                int retries                = 2,
                                                                int retryDelayMilliseconds = 2000)
        {
            if (retries <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retries));
            }
            if (retryDelayMilliseconds <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(retryDelayMilliseconds));
            }
            if (scanTime == default(TimeSpan))
            {
                scanTime = TimeSpan.FromSeconds(2);
            }

            var options = new BrowseDomainsOptions
            {
                Retries    = retries,
                RetryDelay = TimeSpan.FromMilliseconds(retryDelayMilliseconds),
                ScanTime   = scanTime
            };

            return(BrowseDomains(options));
        }
        static internal async Task <ILookup <string, string> > BrowseDomainsAsync(BrowseDomainsOptions options,
                                                                                  Action <string, string> callback    = null,
                                                                                  CancellationToken cancellationToken = default(CancellationToken),
                                                                                  System.Net.NetworkInformation.NetworkInterface[] netInterfacesToSendRequestOn = null)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (netInterfacesToSendRequestOn != null)
            {
                throw new NotImplementedException($"iOS NSNetServiceBrowser/NSNetService does not support per-network interface requests");
            }

            var browseDomainProtocolList = BonjourBrowser.GetNSBonjourServices();

            ResolveOptions resolveOptions  = new ResolveOptions(browseDomainProtocolList);
            var            zeroconfResults = await ResolveAsync(resolveOptions, callback : null, cancellationToken, netInterfacesToSendRequestOn);

            List <IntermediateResult> resultsList = new List <IntermediateResult>();

            foreach (var host in zeroconfResults)
            {
                foreach (var service in host.Services)
                {
                    foreach (var ipAddr in host.IPAddresses)
                    {
                        IntermediateResult b = new IntermediateResult();
                        b.ServiceNameAndDomain = service.Key;
                        b.HostIPAndService     = $"{ipAddr}: {BonjourBrowser.GetServiceType(service.Value.Name, includeTcpUdpDelimiter: false)}";

                        resultsList.Add(b);

                        // Simpleminded callback implementation
                        if (callback != null)
                        {
                            callback(service.Key, ipAddr);
                        }
                    }
                }
            }

            ILookup <string, string> results = resultsList.ToLookup(k => k.ServiceNameAndDomain, h => h.HostIPAndService);

            return(results);
        }
Ejemplo n.º 7
0
        internal static async Task <ILookup <string, string> > BrowseDomainsAsyncOriginal(BrowseDomainsOptions options,
                                                                                          Action <string, string> callback    = null,
                                                                                          CancellationToken cancellationToken = default(CancellationToken),
                                                                                          System.Net.NetworkInformation.NetworkInterface[] netInterfacesToSendRequestOn = null)
        {
            Action <string, Response> wrappedAction = null;

            if (callback != null)
            {
                wrappedAction = (address, response) =>
                {
                    foreach (var service in BrowseResponseParser(response))
                    {
                        callback(service, address);
                    }
                };
            }

            var dict = await ResolveInternal(options,
                                             wrappedAction,
                                             cancellationToken,
                                             netInterfacesToSendRequestOn)
                       .ConfigureAwait(false);

            var r = from kvp in dict
                    from service in BrowseResponseParser(kvp.Value)
                    select new { Service = service, Address = kvp.Key };

            return(r.ToLookup(k => k.Service, k => k.Address));
        }