]> git.meshlink.io Git - catta/blobdiff - avahi-sharp/ServiceResolver.cs
Install avahi bookmarks, even if no pygtk is available.
[catta] / avahi-sharp / ServiceResolver.cs
index 8e8464be10f32bf36846408fedf08931e5be8e02..a32efca8b795186bf19dd429e7dcc97944823680 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections;
 using System.Net;
 using System.Runtime.InteropServices;
 using System.Collections;
 using System.Net;
 using System.Runtime.InteropServices;
+using System.Text;
 using Mono.Unix;
 
 namespace Avahi
 using Mono.Unix;
 
 namespace Avahi
@@ -31,9 +32,10 @@ namespace Avahi
     internal delegate void ServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
                                                     ResolverEvent revent, IntPtr name, IntPtr type,
                                                     IntPtr domain, IntPtr host, IntPtr address,
     internal delegate void ServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
                                                     ResolverEvent revent, IntPtr name, IntPtr type,
                                                     IntPtr domain, IntPtr host, IntPtr address,
-                                                    UInt16 port, IntPtr txt, IntPtr userdata);
+                                                    UInt16 port, IntPtr txt, LookupResultFlags flags,
+                                                    IntPtr userdata);
 
 
-    public class ServiceResolver : IDisposable
+    public class ServiceResolver : ResolverBase, IDisposable
     {
         private IntPtr handle;
         private ServiceInfo currentInfo;
     {
         private IntPtr handle;
         private ServiceInfo currentInfo;
@@ -44,6 +46,7 @@ namespace Avahi
         private string type;
         private string domain;
         private Protocol aproto;
         private string type;
         private string domain;
         private Protocol aproto;
+        private LookupFlags flags;
         private ServiceResolverCallback cb;
 
         private ArrayList foundListeners = new ArrayList ();
         private ServiceResolverCallback cb;
 
         private ArrayList foundListeners = new ArrayList ();
@@ -51,8 +54,9 @@ namespace Avahi
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
-                                                                 IntPtr name, IntPtr type, IntPtr domain,
-                                                                 Protocol aproto, ServiceResolverCallback cb,
+                                                                 byte[] name, byte[] type, byte[] domain,
+                                                                 Protocol aproto, LookupFlags flags,
+                                                                 ServiceResolverCallback cb,
                                                                  IntPtr userdata);
 
         [DllImport ("avahi-common")]
                                                                  IntPtr userdata);
 
         [DllImport ("avahi-common")]
@@ -99,19 +103,21 @@ namespace Avahi
         public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
                                                                                                 Protocol.Unspecified,
                                                                                                 name, type, domain,
         public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
                                                                                                 Protocol.Unspecified,
                                                                                                 name, type, domain,
-                                                                                                Protocol.Unspecified)
+                                                                                                Protocol.Unspecified,
+                                                                                                LookupFlags.None)
         {
         }
 
         public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
                                                                             service.Protocol, service.Name,
                                                                             service.ServiceType, service.Domain,
         {
         }
 
         public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
                                                                             service.Protocol, service.Name,
                                                                             service.ServiceType, service.Domain,
-                                                                            Protocol.Unspecified)
+                                                                            Protocol.Unspecified,
+                                                                            GetLookupFlags (service.Flags))
         {
         }
         
         public ServiceResolver (Client client, int iface, Protocol proto, string name,
         {
         }
         
         public ServiceResolver (Client client, int iface, Protocol proto, string name,
-                                string type, string domain, Protocol aproto)
+                                string type, string domain, Protocol aproto, LookupFlags flags)
         {
             this.client = client;
             this.iface = iface;
         {
             this.client = client;
             this.iface = iface;
@@ -120,6 +126,7 @@ namespace Avahi
             this.type = type;
             this.domain = domain;
             this.aproto = aproto;
             this.type = type;
             this.domain = domain;
             this.aproto = aproto;
+            this.flags = flags;
             cb = OnServiceResolverCallback;
         }
 
             cb = OnServiceResolverCallback;
         }
 
@@ -139,18 +146,14 @@ namespace Avahi
                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
                 return;
 
                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
                 return;
 
-            IntPtr namePtr = Utility.StringToPtr (name);
-            IntPtr typePtr = Utility.StringToPtr (type);
-            IntPtr domainPtr = Utility.StringToPtr (domain);
-
             lock (client) {
             lock (client) {
-                handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
-                                                     aproto, cb, IntPtr.Zero);
+                handle = avahi_service_resolver_new (client.Handle, iface, proto,
+                                                     Utility.StringToBytes (name), Utility.StringToBytes (type),
+                                                     Utility.StringToBytes (domain), aproto, flags, cb, IntPtr.Zero);
+
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
             }
             }
-            
-            Utility.Free (namePtr);
-            Utility.Free (typePtr);
-            Utility.Free (domainPtr);
         }
 
         private void Stop (bool force)
         }
 
         private void Stop (bool force)
@@ -168,7 +171,8 @@ namespace Avahi
         private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
                                                 ResolverEvent revent, IntPtr name, IntPtr type,
                                                 IntPtr domain, IntPtr host, IntPtr address,
         private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
                                                 ResolverEvent revent, IntPtr name, IntPtr type,
                                                 IntPtr domain, IntPtr host, IntPtr address,
-                                                UInt16 port, IntPtr txt, IntPtr userdata)
+                                                UInt16 port, IntPtr txt, LookupResultFlags flags,
+                                                IntPtr userdata)
         {
             ServiceInfo info;
             info.NetworkInterface = iface;
         {
             ServiceInfo info;
             info.NetworkInterface = iface;
@@ -180,6 +184,9 @@ namespace Avahi
             info.Address = Utility.PtrToAddress (address);
             info.Port = port;
 
             info.Address = Utility.PtrToAddress (address);
             info.Port = port;
 
+            if (proto == Protocol.IPv6)
+              info.Address.ScopeId = iface;
+
             ArrayList txtlist = new ArrayList ();
             for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
                 IntPtr buf = avahi_string_list_get_text (l);
             ArrayList txtlist = new ArrayList ();
             for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
                 IntPtr buf = avahi_string_list_get_text (l);
@@ -191,18 +198,30 @@ namespace Avahi
             }
 
             info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
             }
 
             info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
-            
-            if (revent == ResolverEvent.Found) {
+            info.Flags = flags;
+
+            switch (revent) {
+            case ResolverEvent.Found:
                 currentInfo = info;
 
                 foreach (ServiceInfoHandler handler in foundListeners)
                 currentInfo = info;
 
                 foreach (ServiceInfoHandler handler in foundListeners)
-                    handler (this, info);
-            } else {
-                currentInfo = ServiceInfo.Zero;
-                
-                foreach (EventHandler handler in timeoutListeners)
-                    handler (this, new EventArgs ());
+                    handler (this, new ServiceInfoArgs (info));
+                break;
+            case ResolverEvent.Failure:
+                EmitFailure (client.LastError);
+                break;
             }
         }
             }
         }
+
+        private static LookupFlags GetLookupFlags (LookupResultFlags rflags) {
+            LookupFlags ret = LookupFlags.None;
+
+            if ((rflags & LookupResultFlags.Multicast) > 0)
+                ret |= LookupFlags.UseMulticast;
+            if ((rflags & LookupResultFlags.WideArea) > 0)
+                ret |= LookupFlags.UseWideArea;
+
+            return ret;
+        }
     }
 }
     }
 }