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;
30 internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
31 ResolverEvent revent, IntPtr hostname, IntPtr address,
32 LookupResultFlags flags, IntPtr userdata);
34 public class HostNameResolver : ResolverBase, IDisposable
36 private IntPtr handle;
37 private Client client;
39 private Protocol proto;
40 private string hostname;
41 private Protocol aproto;
42 private LookupFlags flags;
43 private HostNameResolverCallback cb;
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_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
53 byte[] hostname, Protocol aproto, LookupFlags flags,
54 HostNameResolverCallback cb, IntPtr userdata);
56 [DllImport ("avahi-client")]
57 private static extern void avahi_host_name_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 HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
94 hostname, Protocol.Unspecified,
99 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
100 Protocol aproto, LookupFlags flags)
102 this.client = client;
105 this.hostname = hostname;
106 this.aproto = aproto;
108 cb = OnHostNameResolverCallback;
116 public void Dispose ()
121 private void Start ()
123 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
124 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
128 handle = avahi_host_name_resolver_new (client.Handle, iface, proto,
129 Utility.StringToBytes (hostname), aproto, flags,
132 if (handle == IntPtr.Zero)
133 client.ThrowError ();
137 private void Stop (bool force)
139 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
140 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
143 avahi_host_name_resolver_free (handle);
144 handle = IntPtr.Zero;
149 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
150 ResolverEvent revent, IntPtr hostname, IntPtr address,
151 LookupResultFlags flags, IntPtr userdata)
154 case ResolverEvent.Found:
155 currentAddress = Utility.PtrToAddress (address);
156 currentHost = Utility.PtrToString (hostname);
158 foreach (HostAddressHandler handler in foundListeners)
159 handler (this, new HostAddressArgs (currentHost, currentAddress));
161 case ResolverEvent.Failure:
162 EmitFailure (client.LastError);