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); }
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); }