]> git.meshlink.io Git - catta/blobdiff - avahi-sharp/EntryGroup.cs
ignore some more files
[catta] / avahi-sharp / EntryGroup.cs
index 74a127cfe64b0c94558beca6e2ee1b04e8bc8b9b..3f8a94cd98f3abfe44a2a06bbba2aa310754df23 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.Runtime.InteropServices;
 
@@ -5,11 +26,24 @@ using System.Runtime.InteropServices;
 namespace Avahi
 {
 
+    [Flags]
+    public enum PublishFlags {
+        None = 0,
+        Unique = 1,
+        NoProbe = 2,
+        NoAnnounce = 4,
+        AllowMultiple = 8,
+        NoReverse = 16,
+        NoCookie = 32,
+        Update = 64
+    }
+    
     public enum EntryGroupState {
         Uncommited,
         Registering,
         Established,
-        Collision
+        Collision,
+        Failure
     }
 
     internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
@@ -19,15 +53,16 @@ namespace Avahi
     {
         private Client client;
         private IntPtr handle;
+        private EntryGroupCallback cb;
         
         [DllImport ("avahi-client")]
         private static extern IntPtr avahi_entry_group_new (IntPtr client, EntryGroupCallback cb, IntPtr userdata);
 
         [DllImport ("avahi-client")]
-        private static extern void avahi_entry_group_commit (IntPtr group);
+        private static extern int avahi_entry_group_commit (IntPtr group);
 
         [DllImport ("avahi-client")]
-        private static extern void avahi_entry_group_reset (IntPtr group);
+        private static extern int avahi_entry_group_reset (IntPtr group);
 
         [DllImport ("avahi-client")]
         private static extern EntryGroupState avahi_entry_group_get_state (IntPtr group);
@@ -36,9 +71,10 @@ namespace Avahi
         private static extern bool avahi_entry_group_is_empty (IntPtr group);
 
         [DllImport ("avahi-client")]
-        private static extern void avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
-                                                                         IntPtr name, IntPtr type, IntPtr domain,
-                                                                         IntPtr host, UInt16 port, IntPtr strlst);
+        private static extern int avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
+                                                                        PublishFlags flags, IntPtr name, IntPtr type,
+                                                                        IntPtr domain, IntPtr host, UInt16 port,
+                                                                        IntPtr strlst);
         
         [DllImport ("avahi-client")]
         private static extern void avahi_entry_group_free (IntPtr group);
@@ -47,28 +83,44 @@ namespace Avahi
         private static extern IntPtr avahi_string_list_new (IntPtr txt);
 
         [DllImport ("avahi-common")]
-        private static extern void avahi_string_list_add (IntPtr list, IntPtr txt);
+        private static extern IntPtr avahi_string_list_add (IntPtr list, IntPtr txt);
 
         [DllImport ("avahi-common")]
         private static extern void avahi_string_list_free (IntPtr list);
 
+        [DllImport ("avahi-common")]
+        private static extern IntPtr avahi_alternative_service_name (IntPtr name);
+
         public event EntryGroupStateHandler StateChanged;
         
         public EntryGroupState State
         {
-            get { return avahi_entry_group_get_state (handle); }
+            get {
+                lock (client) {
+                    return avahi_entry_group_get_state (handle);
+                }
+            }
         }
 
         public bool IsEmpty
         {
-            get { return avahi_entry_group_is_empty (handle); }
+            get {
+                lock (client) {
+                    return avahi_entry_group_is_empty (handle);
+                }
+            }
         }
         
         public EntryGroup (Client client)
         {
             this.client = client;
-            handle = avahi_entry_group_new (client.Handle, OnEntryGroupCallback, IntPtr.Zero);
-            client.CheckError ();
+            cb = OnEntryGroupCallback;
+
+            lock (client) {
+                handle = avahi_entry_group_new (client.Handle, cb, IntPtr.Zero);
+                if (handle == IntPtr.Zero)
+                    client.ThrowError ();
+            }
         }
 
         ~EntryGroup ()
@@ -78,39 +130,51 @@ namespace Avahi
 
         public void Dispose ()
         {
-            if (handle != IntPtr.Zero) {
-                avahi_entry_group_free (handle);
-                handle = IntPtr.Zero;
+            if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero) {
+                lock (client) {
+                    avahi_entry_group_free (handle);
+                    handle = IntPtr.Zero;
+                }
             }
         }
 
         public void Commit ()
         {
-            avahi_entry_group_commit (handle);
-            client.CheckError ();
+            lock (client) {
+                if (avahi_entry_group_commit (handle) < 0)
+                    client.ThrowError ();
+            }
         }
 
         public void Reset ()
         {
-            avahi_entry_group_reset (handle);
-            client.CheckError ();
+            lock (client) {
+                if (avahi_entry_group_reset (handle) < 0)
+                    client.ThrowError ();
+            }
         }
 
         public void AddService (string name, string type, string domain,
-                                UInt16 port, string[] txt)
+                                UInt16 port, params string[] txt)
         {
-            AddService (-1, Protocol.Unspecified, name, type, domain, null, port, txt);
+            AddService (PublishFlags.None, name, type, domain, port, txt);
         }
 
-        public void AddService (int iface, Protocol proto, string name, string type, string domain,
-                                string host, UInt16 port, string[] txt)
+        public void AddService (PublishFlags flags, string name, string type, string domain,
+                                UInt16 port, params string[] txt)
+        {
+            AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);
+        }
+
+        public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,
+                                string host, UInt16 port, params string[] txt)
         {
             IntPtr list = avahi_string_list_new (IntPtr.Zero);
 
             if (txt != null) {
                 foreach (string item in txt) {
                     IntPtr itemPtr = Utility.StringToPtr (item);
-                    avahi_string_list_add (list, itemPtr);
+                    list = avahi_string_list_add (list, itemPtr);
                     Utility.Free (itemPtr);
                 }
             }
@@ -119,13 +183,24 @@ namespace Avahi
             IntPtr typePtr = Utility.StringToPtr (type);
             IntPtr domainPtr = Utility.StringToPtr (domain);
             IntPtr hostPtr = Utility.StringToPtr (host);
-            avahi_entry_group_add_service_strlst (handle, iface, proto, namePtr, typePtr, domainPtr,
-                                                  hostPtr, port, list);
-            avahi_string_list_free (list);
 
-            client.CheckError ();
+            lock (client) {
+                int ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr,
+                                                                hostPtr, port, list);
+                if (ret < 0) {
+                    client.ThrowError ();
+                }
+            }
             
-            Console.WriteLine ("Added service: {0}, {1}, {2}, {3}, {4}", name, type, domain, host, port);
+            avahi_string_list_free (list);
+        }
+
+        public static string GetAlternativeServiceName (string name) {
+            IntPtr namePtr = Utility.StringToPtr (name);
+            IntPtr result = avahi_alternative_service_name (namePtr);
+            Utility.Free (namePtr);
+
+            return Utility.PtrToStringFree (result);
         }
 
         private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)