Hi again guys,

the Connect() method doesn't work because the Client.OnClientCallback method is invoked while avahi_client_new, which means that the Client object still has an invalid Handle and Avahi.EntryGroup barfs on it.

I'm attaching the patch if you care to take a look (it prints a couple of debug messages to stdout).

You'll see that the output "OnClientCallback invoked for state Running" is printed before the "Got handle" message.

Any ideas?

Cheers,
Celso

Celso Pinto wrote:
Hi everyone,

I'm programming an application that uses Avahi's Mono bindings and I think I found an ugly problem: when a Mono app creates an instance of Avahi.Client, the constructor of this class invokes the native avahi_client_new method, which receives a parameter for the client_state callback.

While still in construction, the Avahi.Client class gets notified that the ClientState is now Running, so the Client.OnClientCallback method is invoked but the StateChanged event still doesn't contain any Handlers because this is all happening during construction of the Avahi.Client object.

This means that applications, like my own, that are waiting on the StateChanged event with a ClientState.Running value to start publishing their services never get notified, therefore never get to publish anything.

The only way to fix this problem is to create a Client.Connect() method. This allows us to first create an instance of Avahi.Client, then add a delegate to the StateChanged event and finally call the Avahi.Client.Connect() method. Only then our callbacks get notified of the running state.

The big question is: because this will break the current behaviour of the Avahi.Client class (meaning that the client connects to the daemon during construction), will you guys make the necessary changes?

Cheers,
Celso
_______________________________________________
avahi mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/avahi

--- Client.cs.old	2006-05-01 03:59:28.000000000 +0100
+++ Client.cs	2006-05-01 04:44:05.000000000 +0100
@@ -117,6 +117,7 @@
         private ClientCallback cb;
         private PollCallback pollcb;
         private IntPtr spoll;
+        private ClientFlags cflags;
 
         private Thread thread;
 
@@ -251,6 +252,16 @@
 
         public Client (ClientFlags flags)
         {
+            cflags = flags;
+        }
+
+        public Client () : this (ClientFlags.None) {
+        }
+
+        public void Connect()
+        {
+            if (handle != IntPtr.Zero) return;
+
             spoll = avahi_simple_poll_new ();
 
             pollcb = OnPollCallback;
@@ -259,18 +270,22 @@
             cb = OnClientCallback;
 
             int error;
-            handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
+            handle = avahi_client_new (poll, cflags, cb, IntPtr.Zero, out error);
+            Console.WriteLine("Got handle");
             if (error != 0)
-                throw new ClientException (error);
+            {
+                avahi_simple_poll_quit (spoll);
+                avahi_simple_poll_free (spoll);
+                handle = IntPtr.Zero;
 
+                throw new ClientException (error);
+            }
+            
             thread = new Thread (PollLoop);
             thread.IsBackground = true;
             thread.Start ();
         }
-
-        public Client () : this (ClientFlags.None) {
-        }
-
+        
         ~Client ()
         {
             Dispose ();
@@ -348,6 +363,7 @@
         
         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
         {
+            Console.WriteLine("OnClientCallback invoked for state {0}",state);
             if (StateChanged != null)
                 StateChanged (this, new ClientStateArgs (state));
         }
_______________________________________________
avahi mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/avahi

Reply via email to