From ba12decc4413dedf22c06545d1ec5938efa8954a Mon Sep 17 00:00:00 2001 From: James Willcox Date: Fri, 9 Sep 2005 17:12:57 +0000 Subject: add initial mono bindings git-svn-id: file:///home/lennart/svn/public/avahi/trunk@538 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-sharp/HostNameResolver.cs | 134 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 avahi-sharp/HostNameResolver.cs (limited to 'avahi-sharp/HostNameResolver.cs') diff --git a/avahi-sharp/HostNameResolver.cs b/avahi-sharp/HostNameResolver.cs new file mode 100644 index 0000000..62f4149 --- /dev/null +++ b/avahi-sharp/HostNameResolver.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections; +using System.Net; +using System.Runtime.InteropServices; +using Mono.Unix; + +namespace Avahi +{ + + internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto, + ResolverEvent revent, IntPtr hostname, IntPtr address, + IntPtr userdata); + + public class HostNameResolver : IDisposable + { + private IntPtr handle; + private Client client; + private int iface; + private Protocol proto; + private string hostname; + private Protocol aproto; + + private IPAddress currentAddress; + private string currentHost; + + private ArrayList foundListeners = new ArrayList (); + private ArrayList timeoutListeners = new ArrayList (); + + [DllImport ("avahi-client")] + private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto, + IntPtr hostname, Protocol aproto, + HostNameResolverCallback cb, IntPtr userdata); + + [DllImport ("avahi-client")] + private static extern void avahi_host_name_resolver_free (IntPtr handle); + + public event HostAddressHandler Found + { + add { + foundListeners.Add (value); + Start (); + } + remove { + foundListeners.Remove (value); + Stop (false); + } + } + + public event EventHandler Timeout + { + add { + timeoutListeners.Add (value); + Start (); + } + remove { + timeoutListeners.Remove (value); + Stop (false); + } + } + + public IPAddress Address + { + get { return currentAddress; } + } + + public string HostName + { + get { return currentHost; } + } + + public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified, + hostname, Protocol.Unspecified) + { + } + + public HostNameResolver (Client client, int iface, Protocol proto, string hostname, + Protocol aproto) + { + this.client = client; + this.iface = iface; + this.proto = proto; + this.hostname = hostname; + this.aproto = aproto; + } + + ~HostNameResolver () + { + Dispose (); + } + + public void Dispose () + { + Stop (true); + } + + private void Start () + { + if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0)) + return; + + IntPtr hostPtr = Utility.StringToPtr (hostname); + handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto, + OnHostNameResolverCallback, IntPtr.Zero); + Utility.Free (hostPtr); + } + + private void Stop (bool force) + { + if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) { + avahi_host_name_resolver_free (handle); + handle = IntPtr.Zero; + } + } + + private void OnHostNameResolverCallback (IntPtr resolver, int iface, Protocol proto, + ResolverEvent revent, IntPtr hostname, IntPtr address, + IntPtr userdata) + { + if (revent == ResolverEvent.Found) { + currentAddress = Utility.PtrToAddress (address); + currentHost = Utility.PtrToString (hostname); + + foreach (HostAddressHandler handler in foundListeners) + handler (this, currentHost, currentAddress); + } else { + currentAddress = null; + currentHost = null; + + foreach (EventHandler handler in timeoutListeners) + handler (this, new EventArgs ()); + } + } + } +} -- cgit