Beispiel #1
0
        string GetZeroconfHostKey(NSNetService service)
        {
            StringBuilder sb = new StringBuilder();

            if (service.Addresses != null)
            {
                foreach (NSData address in service.Addresses)
                {
                    if (address != null)
                    {
                        Sockaddr  saddr  = Sockaddr.CreateSockaddr(address.Bytes);
                        IPAddress ipAddr = Sockaddr.CreateIPAddress(saddr);
                        if (ipAddr != null)
                        {
                            sb.Append((sb.Length == 0 ? ipAddr.ToString() : $";{ipAddr.ToString()}"));
                        }
                    }
                    else
                    {
                        Console.WriteLine($"{nameof(GetZeroconfHostKey)}: Got null entry in NSNetService.Addresses, Service {service?.Name}");
                    }
                }
            }
            else
            {
                Console.WriteLine($"{nameof(GetZeroconfHostKey)}: NSNetService.Addresses is null, Service {service?.Name}");
            }

            return(sb.ToString());
        }
Beispiel #2
0
        ZeroconfHost GetOrCreateZeroconfHost(NSNetService service)
        {
            ZeroconfHost host;
            string       hostKey = GetZeroconfHostKey(service);

            lock (zeroconfHostDict)
            {
                if (!zeroconfHostDict.TryGetValue(hostKey, out host))
                {
                    host             = new ZeroconfHost();
                    host.DisplayName = service.Name;

                    List <string> ipAddrList = new List <string>();
                    foreach (NSData address in service.Addresses)
                    {
                        Sockaddr  saddr  = Sockaddr.CreateSockaddr(address.Bytes);
                        IPAddress ipAddr = Sockaddr.CreateIPAddress(saddr);
                        if (ipAddr != null)
                        {
                            ipAddrList.Add(ipAddr.ToString());
                        }
                    }
                    host.IPAddresses = ipAddrList;

                    host.Id = host.IPAddress;

                    zeroconfHostDict[hostKey] = host;
                }
            }

            return(host);
        }