2 using System.Collections;
4 using System.Runtime.InteropServices;
10 internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
11 ResolverEvent revent, Protocol aproto, IntPtr address,
12 IntPtr hostname, IntPtr userdata);
14 public delegate void HostAddressHandler (object o, string host, IPAddress address);
16 public class AddressResolver : IDisposable
18 private IntPtr handle;
19 private Client client;
21 private Protocol proto;
22 private IPAddress address;
24 private IPAddress currentAddress;
25 private string currentHost;
27 private ArrayList foundListeners = new ArrayList ();
28 private ArrayList timeoutListeners = new ArrayList ();
30 [DllImport ("avahi-client")]
31 private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
32 IntPtr address, AddressResolverCallback cb,
35 [DllImport ("avahi-client")]
36 private static extern void avahi_address_resolver_free (IntPtr handle);
38 public event HostAddressHandler Found
41 foundListeners.Add (value);
45 foundListeners.Remove (value);
50 public event EventHandler Timeout
53 timeoutListeners.Add (value);
57 timeoutListeners.Remove (value);
62 public IPAddress Address
64 get { return currentAddress; }
67 public string HostName
69 get { return currentHost; }
72 public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified, address)
76 public AddressResolver (Client client, int iface, Protocol proto, IPAddress address)
81 this.address = address;
89 public void Dispose ()
96 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
99 IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
100 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr,
101 OnAddressResolverCallback, IntPtr.Zero);
102 Utility.Free (addrPtr);
105 private void Stop (bool force)
107 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
108 avahi_address_resolver_free (handle);
109 handle = IntPtr.Zero;
113 private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
114 ResolverEvent revent, Protocol aproto, IntPtr address,
115 IntPtr hostname, IntPtr userdata)
117 if (revent == ResolverEvent.Found) {
118 currentAddress = Utility.PtrToAddress (address);
119 currentHost = Utility.PtrToString (hostname);
121 foreach (HostAddressHandler handler in foundListeners)
122 handler (this, currentHost, currentAddress);
124 currentAddress = null;
127 foreach (EventHandler handler in timeoutListeners)
128 handler (this, new EventArgs ());