public static void ThrowException(this DnsServiceErrorType error, string context) { if (error == DnsServiceErrorType.NoError) { return; } throw new Exception("Bonjour error from " + context + ": " + error); }
private void RegisterCallback(IntPtr handle, DnsServiceFlags flags, DnsServiceErrorType errorCode, string name, string regType, string domain, IntPtr context) { Console.WriteLine("Register result: " + errorCode); if (errorCode == DnsServiceErrorType.NoError) { string key = name.Trim('.') + "." + regType.Trim('.') + "." + domain.Trim('.'); lock (m_handles) m_handles[key] = handle; } }
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); } }
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); } }
private static void QueryPtrCallback(IntPtr h, DnsServiceFlags flags, int ifIndex, DnsServiceErrorType errorCode, string fullName, DnsServiceType rrType, DnsServiceClass rrClass, ushort dataLen, byte[] data, uint ttl, IntPtr context) { Dictionary<string, BonjourDevice> deviceList; lock (lookups) if (!lookups.TryGetValue(context, out deviceList)) return; BonjourDevice device; lock (deviceList) if (!deviceList.TryGetValue(fullName, out device)) return; if (errorCode != DnsServiceErrorType.NoError) { device.Error = errorCode; return; } device.Host.Address = new IPAddress(data); device.IsComplete = true; }
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); } }
private static void QueryPtrCallback(IntPtr h, DnsServiceFlags flags, int ifIndex, DnsServiceErrorType errorCode, string fullName, DnsServiceType rrType, DnsServiceClass rrClass, ushort dataLen, byte[] data, uint ttl, IntPtr context) { Dictionary <string, BonjourDevice> deviceList; lock (lookups) if (!lookups.TryGetValue(context, out deviceList)) { return; } BonjourDevice device; lock (deviceList) if (!deviceList.TryGetValue(fullName, out device)) { return; } if (errorCode != DnsServiceErrorType.NoError) { device.Error = errorCode; return; } device.Host.Address = new IPAddress(data); device.IsComplete = true; }
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); } }