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 HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
32 ResolverEvent revent, IntPtr hostname, IntPtr address,
35 public class HostNameResolver : IDisposable
37 private IntPtr handle;
38 private Client client;
40 private Protocol proto;
41 private string hostname;
42 private Protocol aproto;
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 IntPtr hostname, Protocol aproto,
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)
98 public HostNameResolver (Client client, int iface, Protocol proto, string hostname,
101 this.client = client;
104 this.hostname = hostname;
105 this.aproto = aproto;
106 cb = OnHostNameResolverCallback;
114 public void Dispose ()
119 private void Start ()
121 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
122 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
125 IntPtr hostPtr = Utility.StringToPtr (hostname);
128 handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto,
132 Utility.Free (hostPtr);
135 private void Stop (bool force)
137 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
138 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
141 avahi_host_name_resolver_free (handle);
142 handle = IntPtr.Zero;
147 private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
148 ResolverEvent revent, IntPtr hostname, IntPtr address,
151 if (revent == ResolverEvent.Found) {
152 currentAddress = Utility.PtrToAddress (address);
153 currentHost = Utility.PtrToString (hostname);
155 foreach (HostAddressHandler handler in foundListeners)
156 handler (this, currentHost, currentAddress);
158 currentAddress = null;
161 foreach (EventHandler handler in timeoutListeners)
162 handler (this, new EventArgs ());