4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 using System.Collections;
25 using System.Runtime.InteropServices;
31 internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
32 ResolverEvent revent, Protocol aproto, IntPtr address,
33 IntPtr hostname, IntPtr userdata);
35 public delegate void HostAddressHandler (object o, string host, IPAddress address);
37 public class AddressResolver : IDisposable
39 private IntPtr handle;
40 private Client client;
42 private Protocol proto;
43 private IPAddress address;
45 private IPAddress currentAddress;
46 private string currentHost;
48 private ArrayList foundListeners = new ArrayList ();
49 private ArrayList timeoutListeners = new ArrayList ();
51 [DllImport ("avahi-client")]
52 private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
53 IntPtr address, AddressResolverCallback cb,
56 [DllImport ("avahi-client")]
57 private static extern void avahi_address_resolver_free (IntPtr handle);
59 public event HostAddressHandler Found
62 foundListeners.Add (value);
66 foundListeners.Remove (value);
71 public event EventHandler Timeout
74 timeoutListeners.Add (value);
78 timeoutListeners.Remove (value);
83 public IPAddress Address
85 get { return currentAddress; }
88 public string HostName
90 get { return currentHost; }
93 public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified, address)
97 public AddressResolver (Client client, int iface, Protocol proto, IPAddress address)
102 this.address = address;
110 public void Dispose ()
115 private void Start ()
117 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
120 IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
121 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr,
122 OnAddressResolverCallback, IntPtr.Zero);
123 Utility.Free (addrPtr);
126 private void Stop (bool force)
128 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
129 avahi_address_resolver_free (handle);
130 handle = IntPtr.Zero;
134 private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
135 ResolverEvent revent, Protocol aproto, IntPtr address,
136 IntPtr hostname, IntPtr userdata)
138 if (revent == ResolverEvent.Found) {
139 currentAddress = Utility.PtrToAddress (address);
140 currentHost = Utility.PtrToString (hostname);
142 foreach (HostAddressHandler handler in foundListeners)
143 handler (this, currentHost, currentAddress);
145 currentAddress = null;
148 foreach (EventHandler handler in timeoutListeners)
149 handler (this, new EventArgs ());