Async DBusMessage reads using the C API

Matthew Johnson dbus at matthew.ath.cx
Tue Nov 1 02:26:23 PST 2005


I'm trying to write an application that uses DBUS using the C API
directly (its an application I've already written which I want to use
DBUS in, but I don't want to have to retrofit glib). I've been
discussing this with the people in #dbus, and it seems that there is
missing an API function that calls _dbus_connection_do_iteration to read
any pending messages onto the incoming queue before using
dbus_connection_pop_message to return NULL or the first queued message
(this is also not obvious in the API docs). I've attached a patch
containing a new dbus_connection method which runs a connection
iteration and then calls dbus_connection_pop_message for the return
value.

The function signature is:

DBusMessage* dbus_connection_read_message_async(DBusConnection* conn)

and would be used in an application like this:

msg = dbus_connection_read_message_async(conn);
if (NULL != msg) {
     // do stuf with msg
}
// do other stuff

as part of an existing loop.

I've tested this briefly in my application, I'll do some more thorough
testing soon, but I wanted to see what you guys thought.

Matt


-- 
Matthew Johnson
http://www.matthew.ath.cx/
-------------- next part --------------
--- dbus-0.50/dbus/dbus-connection.c	2005-08-26 18:34:59.000000000 +0100
+++ dbus-modified/dbus/dbus-connection.c	2005-10-31 16:17:19.888497416 +0000
@@ -3111,6 +3111,28 @@
                  dbus_message_get_signature (message_link->data),
                  connection, connection->n_incoming);
 }
+/**
+ * Reads pending data from the socket and then returns the 
+ * first-received message from the incoming message queue,
+ * removing it from the queue. The caller owns a reference to the
+ * returned message. If the queue is empty, returns #NULL.
+ * 
+ * See the caveats in dbus_connection_pop_message() before use.
+ *
+ * @param connection the connection.
+ * @returns next message in the incoming queue.
+*/ 
+DBusMessage* 
+dbus_connection_read_message_async(DBusConnection* conn)
+{
+   _dbus_connection_lock (conn);
+   _dbus_connection_do_iteration_unlocked (conn,
+         DBUS_ITERATION_DO_READING |
+         DBUS_ITERATION_DO_WRITING,
+         0);
+   _dbus_connection_unlock (conn);
+   return dbus_connection_pop_message(conn);
+}
 
 /**
  * Returns the first-received message from the incoming message queue,
--- dbus-0.50/dbus/dbus-connection.h	2005-06-06 19:55:22.000000000 +0100
+++ dbus-modified/dbus/dbus-connection.h	2005-10-31 16:18:52.780375704 +0000
@@ -108,6 +108,7 @@
 void               dbus_connection_steal_borrowed_message       (DBusConnection             *connection,
                                                                  DBusMessage                *message);
 DBusMessage*       dbus_connection_pop_message                  (DBusConnection             *connection);
+DBusMessage*       dbus_connection_read_message_async           (DBusConnection             *connection);
 DBusDispatchStatus dbus_connection_get_dispatch_status          (DBusConnection             *connection);
 DBusDispatchStatus dbus_connection_dispatch                     (DBusConnection             *connection);
 dbus_bool_t        dbus_connection_has_messages_to_send         (DBusConnection *connection);


More information about the dbus mailing list