DNSServiceProcessResult() private method

private DNSServiceProcessResult ( IntPtr handle ) : DnsServiceErrorType
handle System.IntPtr
return DnsServiceErrorType
Ejemplo n.º 1
0
        public void Start(string serviceName, string regType, string domain, string host, ushort port, NameValueCollection txt)
        {
            string key = serviceName.Trim('.') + "." + regType.Trim('.') + "." + domain.Trim('.');

            if (m_handles.ContainsKey(key))
            {
                throw new InvalidOperationException("Service \"" + key + "\" is already registered");
            }

            ushort txtLen = 0;

            byte[] txtRecord = null;
            if (txt != null && txt.Count > 0)
            {
                txtRecord = txt.ToArray();
                if (txtRecord.Length > ushort.MaxValue)
                {
                    throw new ArgumentException("TXT data is too large", "txt");
                }
                txtLen = (ushort)txtRecord.Length;
            }

            var netPort = (ushort)IPAddress.HostToNetworkOrder((short)port);

            IntPtr handle;

            var r = Interop.DNSServiceRegister(out handle, DnsServiceFlags.Default | DnsServiceFlags.AllowRemoteQuery, 0, serviceName, regType, domain, host, netPort, txtLen, txtRecord, registerCallback, IntPtr.Zero);

            r.ThrowException("DNSServiceRegister");

            r = Interop.DNSServiceProcessResult(handle);

            r.ThrowException("DNSServiceProcessResult");
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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());
        }
Ejemplo n.º 4
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);
            }
        }