<div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">The general idea is to separate the message building/parsing from the transport mechanism. The proposed patch gives a client of libdbus more freedom in this regard without the need to duplicate the code.</div>
<div style="font-family:arial,sans-serif;font-size:13px">In my specific case I request the name asynchronously. The two functions allow me to build the message outside of libdbus, send it through my transport and then later parse the reply in the callback.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Signed-off-by: Mat Berchtold <<a href="mailto:mberchtold@gmail.com" target="_blank">mberchtold@gmail.com</a>></div>
<div style="font-family:arial,sans-serif;font-size:13px">---</div><div style="font-family:arial,sans-serif;font-size:13px"> dbus/dbus-bus.c | 83 ++++++++++++++++++++++++++++++++++++++++++++-------------</div><div style="font-family:arial,sans-serif;font-size:13px">
dbus/dbus-bus.h | 8 ++++++</div><div style="font-family:arial,sans-serif;font-size:13px"> 2 files changed, 73 insertions(+), 18 deletions(-)</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c</div><div style="font-family:arial,sans-serif;font-size:13px">index 0fd4831..9fd2d9e 100644</div><div style="font-family:arial,sans-serif;font-size:13px">--- a/dbus/dbus-bus.c</div>
<div style="font-family:arial,sans-serif;font-size:13px">+++ b/dbus/dbus-bus.c</div><div style="font-family:arial,sans-serif;font-size:13px">@@ -1007,6 +1007,69 @@ dbus_bus_get_id (DBusConnection *connection,</div><div style="font-family:arial,sans-serif;font-size:13px">
}</div><div style="font-family:arial,sans-serif;font-size:13px"> </div><div style="font-family:arial,sans-serif;font-size:13px"> /**</div><div style="font-family:arial,sans-serif;font-size:13px">+ * Creates a request name message. For a detailed discussion refer to</div>
<div style="font-family:arial,sans-serif;font-size:13px">+ * dbus_bus_get_result_from_request_name_reply.</div><div style="font-family:arial,sans-serif;font-size:13px">+ *</div><div style="font-family:arial,sans-serif;font-size:13px">
+ * @param name the name to request</div><div style="font-family:arial,sans-serif;font-size:13px">+ * @param flags flags</div><div style="font-family:arial,sans-serif;font-size:13px">+ * @param error location to store the error</div>
<div style="font-family:arial,sans-serif;font-size:13px">+ * @returns the message, NULL if error is set</div><div style="font-family:arial,sans-serif;font-size:13px">+ */ </div><div style="font-family:arial,sans-serif;font-size:13px">
+DBusMessage *</div><div style="font-family:arial,sans-serif;font-size:13px">+dbus_bus_create_request_name_message(const char *name,</div><div style="font-family:arial,sans-serif;font-size:13px">+unsigned int flags,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+DBusError *error)</div><div style="font-family:arial,sans-serif;font-size:13px">+{</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBusMessage *message;</div>
<div style="font-family:arial,sans-serif;font-size:13px">+</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>_dbus_return_val_if_fail(name != NULL, 0);</div><div style="font-family:arial,sans-serif;font-size:13px">
+<span style="white-space:pre-wrap"> </span>_dbus_return_val_if_fail(_dbus_check_is_valid_bus_name(name), 0);</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>_dbus_return_val_if_error_is_set(error, 0);</div>
<div style="font-family:arial,sans-serif;font-size:13px">+</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_PATH_DBUS,</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_INTERFACE_DBUS,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>"RequestName");</div><div style="font-family:arial,sans-serif;font-size:13px">+</div><div style="font-family:arial,sans-serif;font-size:13px">
+<span style="white-space:pre-wrap"> </span>if (message == NULL)</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>{</div><div style="font-family:arial,sans-serif;font-size:13px">
+<span style="white-space:pre-wrap"> </span>_DBUS_SET_OOM(error);</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>return NULL;</div><div style="font-family:arial,sans-serif;font-size:13px">
+<span style="white-space:pre-wrap"> </span>}</div><div style="font-family:arial,sans-serif;font-size:13px">+</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>if (!dbus_message_append_args(message,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_TYPE_STRING, &name,</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_TYPE_UINT32, &flags,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_TYPE_INVALID))</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>{</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>dbus_message_unref(message);</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>_DBUS_SET_OOM(error);</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>return NULL;</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>}</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>return message;</div><div style="font-family:arial,sans-serif;font-size:13px">+}</div><div style="font-family:arial,sans-serif;font-size:13px">
+</div><div style="font-family:arial,sans-serif;font-size:13px">+/**</div><div style="font-family:arial,sans-serif;font-size:13px">+ * Gets the result from a request name reply.</div><div style="font-family:arial,sans-serif;font-size:13px">
+ * The result codes are discussed in dbus_bus_request_name.</div><div style="font-family:arial,sans-serif;font-size:13px">+ *</div><div style="font-family:arial,sans-serif;font-size:13px">+ * @param message the message</div>
<div style="font-family:arial,sans-serif;font-size:13px">+ * @param error error to be filled in on failure</div><div style="font-family:arial,sans-serif;font-size:13px">+ * @param result the result code</div><div style="font-family:arial,sans-serif;font-size:13px">
+ * @returns #FALSE if the error was set</div><div style="font-family:arial,sans-serif;font-size:13px">+ */</div><div style="font-family:arial,sans-serif;font-size:13px">+dbus_bool_t</div><div style="font-family:arial,sans-serif;font-size:13px">
+dbus_bus_get_result_from_request_name_reply(DBusMessage *reply,</div><div style="font-family:arial,sans-serif;font-size:13px">+DBusError *error,</div><div style="font-family:arial,sans-serif;font-size:13px">+dbus_uint32_t *result</div>
<div style="font-family:arial,sans-serif;font-size:13px">+)</div><div style="font-family:arial,sans-serif;font-size:13px">+{</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>return dbus_message_get_args(reply, error,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_TYPE_UINT32, result,</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span>DBUS_TYPE_INVALID);</div>
<div style="font-family:arial,sans-serif;font-size:13px">+}</div><div style="font-family:arial,sans-serif;font-size:13px">+</div><div style="font-family:arial,sans-serif;font-size:13px">+/**</div><div style="font-family:arial,sans-serif;font-size:13px">
* Asks the bus to assign the given name to this connection by invoking</div><div style="font-family:arial,sans-serif;font-size:13px"> * the RequestName method on the bus. This method is fully documented</div><div style="font-family:arial,sans-serif;font-size:13px">
* in the D-Bus specification. For quick reference, the flags and</div><div style="font-family:arial,sans-serif;font-size:13px">@@ -1122,26 +1185,12 @@ dbus_bus_request_name (DBusConnection *connection,</div><div style="font-family:arial,sans-serif;font-size:13px">
_dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);</div><div style="font-family:arial,sans-serif;font-size:13px"> _dbus_return_val_if_error_is_set (error, 0);</div><div style="font-family:arial,sans-serif;font-size:13px">
</div><div style="font-family:arial,sans-serif;font-size:13px">- message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,</div><div style="font-family:arial,sans-serif;font-size:13px">- DBUS_PATH_DBUS,</div>
<div style="font-family:arial,sans-serif;font-size:13px">- DBUS_INTERFACE_DBUS,</div><div style="font-family:arial,sans-serif;font-size:13px">- "RequestName");</div>
<div style="font-family:arial,sans-serif;font-size:13px">-</div><div style="font-family:arial,sans-serif;font-size:13px">+ message = dbus_bus_create_request_name_message(name, flags, error);</div><div style="font-family:arial,sans-serif;font-size:13px">
if (message == NULL)</div><div style="font-family:arial,sans-serif;font-size:13px"> {</div><div style="font-family:arial,sans-serif;font-size:13px"> _DBUS_SET_OOM (error);</div><div style="font-family:arial,sans-serif;font-size:13px">
return -1;</div><div style="font-family:arial,sans-serif;font-size:13px"> }</div><div style="font-family:arial,sans-serif;font-size:13px">- </div><div style="font-family:arial,sans-serif;font-size:13px">- if (!dbus_message_append_args (message,</div>
<div style="font-family:arial,sans-serif;font-size:13px">-<span style="white-space:pre-wrap"> </span> DBUS_TYPE_STRING, &name,</div><div style="font-family:arial,sans-serif;font-size:13px">-<span style="white-space:pre-wrap"> </span> DBUS_TYPE_UINT32, &flags,</div>
<div style="font-family:arial,sans-serif;font-size:13px">-<span style="white-space:pre-wrap"> </span> DBUS_TYPE_INVALID))</div><div style="font-family:arial,sans-serif;font-size:13px">- {</div><div style="font-family:arial,sans-serif;font-size:13px">
- dbus_message_unref (message);</div><div style="font-family:arial,sans-serif;font-size:13px">- _DBUS_SET_OOM (error);</div><div style="font-family:arial,sans-serif;font-size:13px">- return -1;</div><div style="font-family:arial,sans-serif;font-size:13px">
- }</div><div style="font-family:arial,sans-serif;font-size:13px"> </div><div style="font-family:arial,sans-serif;font-size:13px"> reply = dbus_connection_send_with_reply_and_block (connection, message, -1,</div><div style="font-family:arial,sans-serif;font-size:13px">
error);</div><div style="font-family:arial,sans-serif;font-size:13px">@@ -1161,9 +1210,7 @@ dbus_bus_request_name (DBusConnection *connection,</div><div style="font-family:arial,sans-serif;font-size:13px">
return -1;</div><div style="font-family:arial,sans-serif;font-size:13px"> }</div><div style="font-family:arial,sans-serif;font-size:13px"> </div><div style="font-family:arial,sans-serif;font-size:13px">- if (!dbus_message_get_args (reply, error,</div>
<div style="font-family:arial,sans-serif;font-size:13px">- DBUS_TYPE_UINT32, &result,</div><div style="font-family:arial,sans-serif;font-size:13px">- DBUS_TYPE_INVALID))</div>
<div style="font-family:arial,sans-serif;font-size:13px">+ if (!dbus_bus_get_result_from_request_name_reply(reply, error, &result))</div><div style="font-family:arial,sans-serif;font-size:13px"> {</div><div style="font-family:arial,sans-serif;font-size:13px">
_DBUS_ASSERT_ERROR_IS_SET (error);</div><div style="font-family:arial,sans-serif;font-size:13px"> dbus_message_unref (reply);</div><div style="font-family:arial,sans-serif;font-size:13px">diff --git a/dbus/dbus-bus.h b/dbus/dbus-bus.h</div>
<div style="font-family:arial,sans-serif;font-size:13px">index 02a9571..6bad442 100644</div><div style="font-family:arial,sans-serif;font-size:13px">--- a/dbus/dbus-bus.h</div><div style="font-family:arial,sans-serif;font-size:13px">
+++ b/dbus/dbus-bus.h</div><div style="font-family:arial,sans-serif;font-size:13px">@@ -64,6 +64,14 @@ int dbus_bus_request_name (DBusConnection *connection,</div><div style="font-family:arial,sans-serif;font-size:13px">
<span style="white-space:pre-wrap"> </span> unsigned int flags,</div><div style="font-family:arial,sans-serif;font-size:13px"> <span style="white-space:pre-wrap"> </span> DBusError *error);</div><div style="font-family:arial,sans-serif;font-size:13px">
DBUS_EXPORT</div><div style="font-family:arial,sans-serif;font-size:13px">+DBusMessage *dbus_bus_create_request_name_message(const char *name,</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span> unsigned int flags,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span> DBusError *error);</div><div style="font-family:arial,sans-serif;font-size:13px">+DBUS_EXPORT</div><div style="font-family:arial,sans-serif;font-size:13px">
+dbus_bool_t dbus_bus_get_result_from_request_name_reply(DBusMessage *reply,</div><div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span> DBusError *error,</div>
<div style="font-family:arial,sans-serif;font-size:13px">+<span style="white-space:pre-wrap"> </span> dbus_uint32_t *result);</div><div style="font-family:arial,sans-serif;font-size:13px">+DBUS_EXPORT</div><div style="font-family:arial,sans-serif;font-size:13px">
int dbus_bus_release_name (DBusConnection *connection,</div><div style="font-family:arial,sans-serif;font-size:13px"> <span style="white-space:pre-wrap"> </span> const char *name,</div><div style="font-family:arial,sans-serif;font-size:13px">
<span style="white-space:pre-wrap"> </span> DBusError *error);</div></div>