]> git.meshlink.io Git - catta/blobdiff - avahi-sharp/Client.cs
fix avahi_netlink_new to allow multiple netlinks per process
[catta] / avahi-sharp / Client.cs
index 4e19fabfcb2678f4e1f950da39d9326542da70bb..b3815b32cb5d3120bacf2b83bad89c82ae5819bf 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
 
@@ -25,6 +23,9 @@ using System.Threading;
 using System.Collections;
 using System.Runtime.InteropServices;
 using Mono.Unix;
+using Mono.Unix.Native;
+
+using Stdlib = Mono.Unix.Native.Stdlib;
 
 namespace Avahi
 {
@@ -32,7 +33,7 @@ namespace Avahi
         Found,
         Failure
     }
-    
+
     internal enum BrowserEvent {
         Added,
         Removed,
@@ -49,18 +50,25 @@ namespace Avahi
     public class ClientStateArgs : EventArgs
     {
         private ClientState state;
+        private ErrorCode error;
 
         public ClientState State
         {
             get { return state; }
         }
 
-        public ClientStateArgs (ClientState state)
+        public ErrorCode Error
+        {
+            get { return error; }
+        }
+
+        public ClientStateArgs (ClientState state, ErrorCode error)
         {
             this.state = state;
+            this.error = error;
         }
     }
-    
+
     public enum Protocol {
         Unspecified = -1,
         IPv4 = 0,
@@ -73,7 +81,7 @@ namespace Avahi
         Running,
         Collision
     }
-    
+
     public enum ClientState {
         Registering = ServerState.Registering,
         Running = ServerState.Running,
@@ -107,7 +115,7 @@ namespace Avahi
         IgnoreUserConfig = 1,
         NoFail = 2
     }
-    
+
     public class Client : IDisposable
     {
         private IntPtr handle;
@@ -141,7 +149,7 @@ namespace Avahi
 
         [DllImport ("avahi-client")]
         private static extern int avahi_client_errno (IntPtr handle);
-        
+
         [DllImport ("avahi-common")]
         private static extern IntPtr avahi_simple_poll_new ();
 
@@ -182,7 +190,7 @@ namespace Avahi
         {
             get { return handle; }
         }
-        
+
         public string Version
         {
             get {
@@ -276,10 +284,15 @@ namespace Avahi
         public void Dispose ()
         {
             if (handle != IntPtr.Zero) {
-                avahi_client_free (handle);
-                avahi_simple_poll_quit (spoll);
-                avahi_simple_poll_free (spoll);
-                handle = IntPtr.Zero;
+                lock (this) {
+                    avahi_client_free (handle);
+                    handle = IntPtr.Zero;
+
+                    avahi_simple_poll_quit (spoll);
+                    Monitor.Wait (this);
+
+                    avahi_simple_poll_free (spoll);
+                }
             }
         }
 
@@ -311,7 +324,7 @@ namespace Avahi
             IntPtr namePtr = Stdlib.malloc ((ulong) len);
             IntPtr typePtr = Stdlib.malloc ((ulong) len);
             IntPtr domainPtr = Stdlib.malloc ((ulong) len);
-            
+
             int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
                                                 domainPtr, len);
 
@@ -319,7 +332,7 @@ namespace Avahi
                 Utility.Free (namePtr);
                 Utility.Free (typePtr);
                 Utility.Free (domainPtr);
-                
+
                 name = null;
                 type = null;
                 domain = null;
@@ -342,11 +355,11 @@ namespace Avahi
             if (error != ErrorCode.Ok)
                 throw new ClientException (error);
         }
-        
+
         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
         {
             if (StateChanged != null)
-                StateChanged (this, new ClientStateArgs (state));
+                StateChanged (this, new ClientStateArgs (state, LastError));
         }
 
         private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
@@ -360,6 +373,7 @@ namespace Avahi
             try {
                 lock (this) {
                     avahi_simple_poll_loop (spoll);
+                    Monitor.Pulse (this);
                 }
             } catch (Exception e) {
                 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);