2 using System.Collections;
4 using System.Runtime.InteropServices;
10 internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
11 ResolverEvent revent, IntPtr hostname, IntPtr address,
14 public class HostNameResolver : IDisposable
16 private IntPtr handle;
17 private Client client;
19 private Protocol proto;
20 private string hostname;
21 private Protocol aproto;
23 private IPAddress currentAddress;
24 private string currentHost;
26 private ArrayList foundListeners = new ArrayList ();
27 private ArrayList timeoutListeners = new ArrayList ();
29 [DllImport ("avahi-client")]
30 private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
31 IntPtr hostname, Protocol aproto,
32 HostNameResolverCallback cb, IntPtr userdata);
34 [DllImport ("avahi-client")]
35 private static extern void avahi_host_name_resolver_free (IntPtr handle);
37 public event HostAddressHandler Found
40 foundListeners.Add (value);
44 foundListeners.Remove (value);
49 public event EventHandler Timeout
52 timeoutListeners.Add (value);
56 timeoutListeners.Remove (value);
61 public IPAddress Address
63 get { return currentAddress; }
66 public string HostName
68 get { return currentHost; }
71 public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
72 hostname, Protocol.Unspecified)
76 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
82 this.hostname = hostname;
91 public void Dispose ()
98 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
101 IntPtr hostPtr = Utility.StringToPtr (hostname);
102 handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto,
103 OnHostNameResolverCallback, IntPtr.Zero);
104 Utility.Free (hostPtr);
107 private void Stop (bool force)
109 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
110 avahi_host_name_resolver_free (handle);
111 handle = IntPtr.Zero;
115 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
116 ResolverEvent revent, IntPtr hostname, IntPtr address,
119 if (revent == ResolverEvent.Found) {
120 currentAddress = Utility.PtrToAddress (address);
121 currentHost = Utility.PtrToString (hostname);
123 foreach (HostAddressHandler handler in foundListeners)
124 handler (this, currentHost, currentAddress);
126 currentAddress = null;
129 foreach (EventHandler handler in timeoutListeners)
130 handler (this, new EventArgs ());