2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 using System.Collections;
23 using System.Runtime.InteropServices;
29 internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
30 ResolverEvent revent, Protocol aproto, IntPtr address,
31 IntPtr hostname, IntPtr userdata);
33 public delegate void HostAddressHandler (object o, string host, IPAddress address);
35 public class AddressResolver : IDisposable
37 private IntPtr handle;
38 private Client client;
40 private Protocol proto;
41 private IPAddress address;
43 private IPAddress currentAddress;
44 private string currentHost;
46 private ArrayList foundListeners = new ArrayList ();
47 private ArrayList timeoutListeners = new ArrayList ();
49 [DllImport ("avahi-client")]
50 private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
51 IntPtr address, AddressResolverCallback cb,
54 [DllImport ("avahi-client")]
55 private static extern void avahi_address_resolver_free (IntPtr handle);
57 public event HostAddressHandler Found
60 foundListeners.Add (value);
64 foundListeners.Remove (value);
69 public event EventHandler Timeout
72 timeoutListeners.Add (value);
76 timeoutListeners.Remove (value);
81 public IPAddress Address
83 get { return currentAddress; }
86 public string HostName
88 get { return currentHost; }
91 public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified, address)
95 public AddressResolver (Client client, int iface, Protocol proto, IPAddress address)
100 this.address = address;
108 public void Dispose ()
113 private void Start ()
115 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
118 IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
119 handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr,
120 OnAddressResolverCallback, IntPtr.Zero);
121 Utility.Free (addrPtr);
124 private void Stop (bool force)
126 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
127 avahi_address_resolver_free (handle);
128 handle = IntPtr.Zero;
132 private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
133 ResolverEvent revent, Protocol aproto, IntPtr address,
134 IntPtr hostname, IntPtr userdata)
136 if (revent == ResolverEvent.Found) {
137 currentAddress = Utility.PtrToAddress (address);
138 currentHost = Utility.PtrToString (hostname);
140 foreach (HostAddressHandler handler in foundListeners)
141 handler (this, currentHost, currentAddress);
143 currentAddress = null;
146 foreach (EventHandler handler in timeoutListeners)
147 handler (this, new EventArgs ());