]> git.meshlink.io Git - catta/blobdiff - avahi-sharp/HostNameResolver.cs
* add option to not enable SO_REUSEADDR for multicast sockets, effectively disallow...
[catta] / avahi-sharp / HostNameResolver.cs
index 62f4149e0572063f079b1e4db5523a4e92690435..44febcdf4d1a8f4d2bc4703978bdb936deb4f501 100644 (file)
@@ -1,3 +1,24 @@
+/* $Id$ */
+
+/***
+  This file is part of avahi.
+
+  avahi is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as
+  published by the Free Software Foundation; either version 2.1 of the
+  License, or (at your option) any later version.
+
+  avahi is distributed in the hope that it will be useful, but WITHOUT
+  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+  Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with avahi; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
 using System;
 using System.Collections;
 using System.Net;
 using System;
 using System.Collections;
 using System.Net;
@@ -9,9 +30,9 @@ namespace Avahi
 
     internal delegate void HostNameResolverCallback (IntPtr resolver, int iface, Protocol proto,
                                                      ResolverEvent revent, IntPtr hostname, IntPtr address,
 
     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;
     {
         private IntPtr handle;
         private Client client;
@@ -19,6 +40,8 @@ namespace Avahi
         private Protocol proto;
         private string hostname;
         private Protocol aproto;
         private Protocol proto;
         private string hostname;
         private Protocol aproto;
+        private LookupFlags flags;
+        private HostNameResolverCallback cb;
 
         private IPAddress currentAddress;
         private string currentHost;
 
         private IPAddress currentAddress;
         private string currentHost;
@@ -28,7 +51,7 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto,
-                                                                   IntPtr hostname, Protocol aproto,
+                                                                   IntPtr hostname, Protocol aproto, LookupFlags flags,
                                                                    HostNameResolverCallback cb, IntPtr userdata);
 
         [DllImport ("avahi-client")]
                                                                    HostNameResolverCallback cb, IntPtr userdata);
 
         [DllImport ("avahi-client")]
@@ -69,18 +92,21 @@ namespace Avahi
         }
 
         public HostNameResolver (Client client, string hostname) : this (client, -1, Protocol.Unspecified,
         }
 
         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,
         {
         }
 
         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.client = client;
             this.iface = iface;
             this.proto = proto;
             this.hostname = hostname;
             this.aproto = aproto;
+            this.flags = flags;
+            cb = OnHostNameResolverCallback;
         }
 
         ~HostNameResolver ()
         }
 
         ~HostNameResolver ()
@@ -95,39 +121,54 @@ namespace Avahi
 
         private void Start ()
         {
 
         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);
                 return;
 
             IntPtr hostPtr = Utility.StringToPtr (hostname);
-            handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto,
-                                                   OnHostNameResolverCallback, IntPtr.Zero);
+
+            lock (client) {
+                handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto, flags,
+                                                       cb, IntPtr.Zero);
+            }
+            
             Utility.Free (hostPtr);
         }
 
         private void Stop (bool force)
         {
             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;
+            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,
             }
         }
 
         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);
                 currentAddress = Utility.PtrToAddress (address);
                 currentHost = Utility.PtrToString (hostname);
 
                 foreach (HostAddressHandler handler in foundListeners)
                     handler (this, currentHost, currentAddress);
-            } else {
+                break;
+            case ResolverEvent.Timeout:
                 currentAddress = null;
                 currentHost = null;
                 
                 foreach (EventHandler handler in timeoutListeners)
                     handler (this, new EventArgs ());
                 currentAddress = null;
                 currentHost = null;
                 
                 foreach (EventHandler handler in timeoutListeners)
                     handler (this, new EventArgs ());
+                break;
+            default:
+                EmitResolverEvent (revent);
+                break;
             }
         }
     }
             }
         }
     }