コード例 #1
0
        private void NetService_AddressResolved(object sender, EventArgs e)
        {
            if (sender is NSNetService netService)
            {
                netService = (NSNetService)sender;

                Debug.WriteLine($"{nameof(NetService_AddressResolved)}: Name {netService?.Name} Type {netService?.Type} Domain {netService?.Domain} " +
                                $"HostName {netService?.HostName} Port {netService?.Port} Addresses {GetZeroconfHostKey(netService)}");

                if (netService.TxtRecordData != null)
                {
                    NSDictionary dict = NSNetService.DictionaryFromTxtRecord(netService.TxtRecordData);
                    if (dict != null)
                    {
                        if (dict.Count > 0)
                        {
                            foreach (var key in dict.Keys)
                            {
                                Debug.WriteLine($"{nameof(Browser_FoundService)}: Key {key} Value {dict[key].ToString()}");
                            }
                        }
                        else
                        {
                            Debug.WriteLine($"{nameof(Browser_FoundService)}: Service.DictionaryFromTxtRecord has 0 entries");
                        }
                    }
                    else
                    {
                        Debug.WriteLine($"{nameof(Browser_FoundService)}: Service.DictionaryFromTxtRecord returned null");
                    }
                }
                else
                {
                    Debug.WriteLine($"{nameof(Browser_FoundService)}: TxtRecordData is null");
                }

                string serviceKey = GetNsNetServiceKey(netService);
                lock (discoveredServiceDict)
                {
                    discoveredServiceDict[serviceKey] = netService;
                }
            }
        }
コード例 #2
0
        void RefreshZeroconfHostDict()
        {
            // Do not walk discoveredServiceDict[] directly
            // If a NSNetService is in discoveredServiceDict[], it was resolved successfully before it was added

            List <NSNetService> nsNetServiceList = new List <NSNetService>();

            lock (discoveredServiceDict)
            {
                nsNetServiceList.AddRange(discoveredServiceDict.Values);
            }

            // For each NSNetService, create a ZeroconfHost record

            foreach (var nsNetService in nsNetServiceList)
            {
                Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Name {nsNetService.Name} Type {nsNetService.Type} Domain {nsNetService.Domain} " +
                                $"HostName {nsNetService.HostName} Port {nsNetService.Port}");

                // Obtain or create ZeroconfHost

                ZeroconfHost host = GetOrCreateZeroconfHost(nsNetService);

                // Add service to ZeroconfHost record

                Service svc = new Service();
                svc.Name = GetNsNetServiceName(nsNetService);
                svc.Port = (int)nsNetService.Port;
                // svc.Ttl = is not available

                NSData txtRecordData = nsNetService.GetTxtRecordData();
                if (txtRecordData != null)
                {
                    NSDictionary txtDict = NSNetService.DictionaryFromTxtRecord(txtRecordData);
                    if (txtDict != null)
                    {
                        if (txtDict.Count > 0)
                        {
                            foreach (var key in txtDict.Keys)
                            {
                                Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Key {key} Value {txtDict[key].ToString()}");
                            }

                            Dictionary <string, string> propertyDict = new Dictionary <string, string>();

                            foreach (var key in txtDict.Keys)
                            {
                                propertyDict[key.ToString()] = txtDict[key].ToString();
                            }
                            svc.AddPropertySet(propertyDict);
                        }
                        else
                        {
                            Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Service.DictionaryFromTxtRecord has 0 entries");
                        }
                    }
                    else
                    {
                        Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Service.DictionaryFromTxtRecord returned null");
                    }
                }

                host.AddService(svc);
            }
        }