DNSServiceHasData() public static method

public static DNSServiceHasData ( IntPtr handle, System.TimeSpan waitFor ) : bool
handle System.IntPtr
waitFor System.TimeSpan
return bool
Esempio n. 1
0
        private void BrowseCallback(IntPtr h, DnsServiceFlags flags, int ifIndex, DnsServiceErrorType errorCode, string serviceName, string regType, string domain, IntPtr context)
        {
            errorCode.ThrowException("BrowseCallback");

            Dictionary <string, BonjourDevice> deviceList;

            lock (lookups)
                if (!lookups.TryGetValue(h, out deviceList))
                {
                    return;
                }

            var device = new BonjourDevice {
                DeviceId = serviceName, IsComplete = false, Error = DnsServiceErrorType.NoError
            };

            lock (deviceList)
                deviceList[serviceName] = device;

            IntPtr handle;
            var    r = Interop.DNSServiceResolve(out handle, DnsServiceFlags.Default | DnsServiceFlags.ForceMulticast, ifIndex, serviceName, regType, domain, resolveCallback, h);

            if (r != DnsServiceErrorType.NoError)
            {
                device.Error = r;
                return;
            }

            lock (deviceList)
                deviceList["0x" + handle.ToString("X")] = device;

            try
            {
                if (!Interop.DNSServiceHasData(handle, TimeSpan.FromSeconds(5)))
                {
                    return;
                }

                r = Interop.DNSServiceProcessResult(handle);

                if (r != DnsServiceErrorType.NoError)
                {
                    device.Error = r;
                    return;
                }
            }
            finally
            {
                lock (deviceList)
                    deviceList.Remove("0x" + handle.ToString("X"));
                Interop.DNSServiceRefDeallocate(handle);
            }
        }
Esempio n. 2
0
        private IList <IClientDevice> FindDevicesImpl(string deviceType, string domain, TimeSpan?timeout)
        {
            IntPtr handle;
            var    r = Interop.DNSServiceBrowse(out handle, DnsServiceFlags.Default | DnsServiceFlags.ForceMulticast, 0, deviceType, domain, browseCallback, IntPtr.Zero);

            r.ThrowException("DNSServiceBrowse");

            var deviceList = new Dictionary <string, BonjourDevice>(StringComparer.OrdinalIgnoreCase);

            lock (lookups)
                lookups.Add(handle, deviceList);

            isCancelled = false;

            try
            {
                TimeSpan quantum = TimeSpan.FromSeconds(0.5);
                TimeSpan elapsed = TimeSpan.Zero;

                while (timeout == null || elapsed < timeout.Value)
                {
                    if (Interop.DNSServiceHasData(handle, quantum))
                    {
                        r = Interop.DNSServiceProcessResult(handle);

                        r.ThrowException("DNSServiceProcessResult");

                        break;
                    }

                    elapsed += quantum;

                    if (isCancelled)
                    {
                        break;
                    }
                }
            }
            finally
            {
                lock (lookups)
                    lookups.Remove(handle);
                Interop.DNSServiceRefDeallocate(handle);
            }

            return(deviceList.Values.Distinct().Where(x => x.IsComplete).OfType <IClientDevice>().ToList());
        }
Esempio n. 3
0
        private void ResolveCallback(IntPtr h, DnsServiceFlags flags, int ifIndex, DnsServiceErrorType errorCode, string fullName, string hostTarget, ushort port, ushort txtLen, byte[] txtRecord, IntPtr context)
        {
            Dictionary <string, BonjourDevice> deviceList;

            lock (lookups)
                if (!lookups.TryGetValue(context, out deviceList))
                {
                    return;
                }

            BonjourDevice device;

            lock (deviceList)
                if (!deviceList.TryGetValue("0x" + h.ToString("X"), out device))
                {
                    return;
                }

            if (errorCode != DnsServiceErrorType.NoError)
            {
                device.Error = errorCode;
                return;
            }

            var hostPort = (ushort)IPAddress.NetworkToHostOrder((short)port);

            device.Host = new System.Net.IPEndPoint(0, hostPort);

            var txt = txtRecord.FromArray();

            device.Name     = txt["DvNm"];
            device.PairCode = txt["Pair"];

            IntPtr handle;
            var    r = Interop.DNSServiceQueryRecord(out handle, DnsServiceFlags.Default | DnsServiceFlags.ForceMulticast, ifIndex, hostTarget, DnsServiceType.A, DnsServiceClass.IN, queryPtrCallback, context);

            if (r != DnsServiceErrorType.NoError)
            {
                device.Error = errorCode;
                return;
            }

            lock (deviceList)
                deviceList[hostTarget] = device;

            try
            {
                if (!Interop.DNSServiceHasData(handle, TimeSpan.FromSeconds(5)))
                {
                    return;
                }

                r = Interop.DNSServiceProcessResult(handle);

                if (r != DnsServiceErrorType.NoError)
                {
                    device.Error = r;
                }
            }
            finally
            {
                Interop.DNSServiceRefDeallocate(handle);
                lock (deviceList)
                    deviceList.Remove(hostTarget);
            }
        }