コード例 #1
0
        public void StartServiceSearch(string protocol)
        {
            const string localDomainForParse = ".local.";
            const string localDomain         = "local.";
            int          localDomainLength   = localDomain.Length;

            // All previous service discovery results are discarded

            lock (discoveredServiceDict)
            {
                discoveredServiceDict.Clear();
            }

            lock (zeroconfHostDict)
            {
                zeroconfHostDict.Clear();
            }

            string serviceType = string.Empty;
            string domain      = string.Empty;

            if (protocol.ToLower().EndsWith(localDomainForParse))
            {
                serviceType = protocol.Substring(0, protocol.Length - localDomainLength);
                domain      = protocol.Substring(serviceType.Length);
            }
            else
            {
                serviceType = BonjourBrowser.GetServiceType(protocol);
                if (serviceType != null)
                {
                    if (protocol.Length > serviceType.Length)
                    {
                        domain = protocol.Substring(serviceType.Length);

                        //           6 = delim.Length
                        //          /----\ 
                        // _foo._bar._tcp. example.com.
                        // 012345678901234 567890123456 index = [0, 26]
                        // 123456789012345 678901234567 length = 27
                        //   serviceType      domain
                    }
                    else
                    {
                        domain = string.Empty;
                    }
                }
                else
                {
                    serviceType = protocol;
                    domain      = string.Empty;
                }
            }

            Debug.WriteLine($"{nameof(StartServiceSearch)}: {nameof(netServiceBrowser)}.SearchForServices(Type {serviceType} Domain {domain})");

            netServiceBrowser.SearchForServices(serviceType, domain);
        }
コード例 #2
0
        /// <summary>
        /// Xamarin.iOS only: returns the list of NSBonjourServices from Info.plist
        /// </summary>
        /// <param name="domain">Optional domain (example: "local.") to append to each service; null = no domain appended; non-null must terminate with "."</param>
        /// <returns></returns>
        public static IReadOnlyList <string> GetiOSInfoPlistServices(string domain = null)
        {
            var serviceList = new List <string>();

#if __IOS__
            if (UIDevice.CurrentDevice.CheckSystemVersion(14, 5) && !UseBSDSocketsZeroconfOniOS)
            {
                serviceList.AddRange(BonjourBrowser.GetNSBonjourServices(domain));
            }
#endif

            return(serviceList);
        }
コード例 #3
0
        static internal async Task <IReadOnlyList <string> > GetDomains(TimeSpan scanTime, CancellationToken cancellationToken = default(CancellationToken))
        {
            BonjourBrowser bonjourBrowser = new BonjourBrowser(scanTime);

            bonjourBrowser.StartDomainSearch();

            await Task.Delay(scanTime, cancellationToken).ConfigureAwait(false);

            bonjourBrowser.StopDomainSearch();

            IReadOnlyList <string> domainList = bonjourBrowser.GetFoundDomainList();

            return(domainList);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        static internal async Task <IReadOnlyList <IZeroconfHost> > ResolveAsync(ResolveOptions options,
                                                                                 Action <IZeroconfHost> 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");
            }

            List <IZeroconfHost> combinedResultList = new List <IZeroconfHost>();

            // Seems you must reuse the one BonjourBrowser (which is really an NSNetServiceBrowser)... multiple instances do not play well together

            BonjourBrowser bonjourBrowser = new BonjourBrowser(options.ScanTime);

            foreach (var protocol in options.Protocols)
            {
                bonjourBrowser.StartServiceSearch(protocol);

                await Task.Delay(options.ScanTime, cancellationToken).ConfigureAwait(false);

                bonjourBrowser.StopServiceSearch();

                // Simpleminded callback implementation
                var results = bonjourBrowser.ReturnZeroconfHostResults();
                foreach (var result in results)
                {
                    if (callback != null)
                    {
                        callback(result);
                    }
                }

                combinedResultList.AddRange(results);
            }

            return(combinedResultList);
        }