X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-sharp%2FHostNameResolver.cs;h=b9fe7f4348e8da6aac2e363efd0d70ecf36cbab6;hb=938b5f883fbaa642e3acb7720e37f95f3d72be62;hp=8b529c702b0bbda9edb0ef02a92cfe603096ef81;hpb=a9566d5dcac080d7fa91546823277c57a5d09a5f;p=catta diff --git a/avahi-sharp/HostNameResolver.cs b/avahi-sharp/HostNameResolver.cs index 8b529c7..b9fe7f4 100644 --- a/avahi-sharp/HostNameResolver.cs +++ b/avahi-sharp/HostNameResolver.cs @@ -1,5 +1,3 @@ -/* $Id$ */ - /*** This file is part of avahi. @@ -23,6 +21,7 @@ using System; using System.Collections; using System.Net; using System.Runtime.InteropServices; +using System.Text; using Mono.Unix; namespace Avahi @@ -30,9 +29,9 @@ namespace Avahi internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto, ResolverEvent revent, IntPtr hostname, IntPtr address, - IntPtr userdata); + LookupResultFlags flags, IntPtr userdata); - public class HostNameResolver : IDisposable + public class HostNameResolver : ResolverBase, IDisposable { private IntPtr handle; private Client client; @@ -40,6 +39,7 @@ namespace Avahi private Protocol proto; private string hostname; private Protocol aproto; + private LookupFlags flags; private HostNameResolverCallback cb; private IPAddress currentAddress; @@ -47,10 +47,10 @@ namespace Avahi 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, + byte[] hostname, Protocol aproto, LookupFlags flags, HostNameResolverCallback cb, IntPtr userdata); [DllImport ("avahi-client")] @@ -67,7 +67,7 @@ namespace Avahi Stop (false); } } - + public event EventHandler Timeout { add { @@ -91,18 +91,20 @@ namespace Avahi } public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified, - hostname, Protocol.Unspecified) + hostname, Protocol.Unspecified, + LookupFlags.None) { } public HostNameResolver (Client client, int iface, Protocol proto, string hostname, - Protocol aproto) + Protocol aproto, LookupFlags flags) { this.client = client; this.iface = iface; this.proto = proto; this.hostname = hostname; this.aproto = aproto; + this.flags = flags; cb = OnHostNameResolverCallback; } @@ -118,39 +120,47 @@ namespace Avahi private void Start () { - if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0)) + if (client.Handle == IntPtr.Zero || 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); + lock (client) { + handle = avahi_host_name_resolver_new (client.Handle, iface, proto, + Utility.StringToBytes (hostname), aproto, flags, + cb, IntPtr.Zero); + + if (handle == IntPtr.Zero) + client.ThrowError (); + } } 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; + if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero && + (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) { + + lock (client) { + 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) + LookupResultFlags flags, IntPtr userdata) { - if (revent == ResolverEvent.Found) { + switch (revent) { + case 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 ()); + handler (this, new HostAddressArgs (currentHost, currentAddress)); + break; + case ResolverEvent.Failure: + EmitFailure (client.LastError); + break; } } }