[vala/staging] codegen: Skip GDBus reply handling when async callback is NULL



commit d23c4d03744e55454992b4956a27c84484af0114
Author: Ole André Vadla Ravnås <oleavr gmail com>
Date:   Fri May 7 14:14:00 2021 +0200

    codegen: Skip GDBus reply handling when async callback is NULL
    
    To optimize `proxy.method.begin ()` to be a simple fire and forget all
    the way down to the protocol level.

 codegen/valagdbusclientmodule.vala | 20 ++++++++
 tests/Makefile.am                  |  1 +
 tests/dbus/async-no-reply.test     | 95 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+)
---
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index af833f749..51c83b67b 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -694,6 +694,9 @@ public class Vala.GDBusClientModule : GDBusModule {
                                ccall.add_argument (error_argument);
                                ccode.add_expression (ccall);
                        } else if (call_type == CallType.ASYNC) {
+                               var callback_specified = new CCodeBinaryExpression 
(CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("_callback_"), new CCodeConstant ("NULL"));
+                               ccode.open_if (callback_specified);
+
                                ccall = new CCodeFunctionCall (new CCodeIdentifier 
("g_dbus_connection_send_message_with_reply"));
                                ccall.add_argument (connection);
                                ccall.add_argument (new CCodeIdentifier ("_message"));
@@ -714,6 +717,23 @@ public class Vala.GDBusClientModule : GDBusModule {
                                ccall.add_argument (res_wrapper);
 
                                ccode.add_expression (ccall);
+
+                               ccode.add_else ();
+
+                               var set_flags = new CCodeFunctionCall (new CCodeIdentifier 
("g_dbus_message_set_flags"));
+                               set_flags.add_argument (new CCodeIdentifier ("_message"));
+                               set_flags.add_argument (new CCodeConstant 
("G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED"));
+                               ccode.add_expression (set_flags);
+
+                               ccall = new CCodeFunctionCall (new CCodeIdentifier 
("g_dbus_connection_send_message"));
+                               ccall.add_argument (connection);
+                               ccall.add_argument (new CCodeIdentifier ("_message"));
+                               ccall.add_argument (new CCodeConstant ("G_DBUS_SEND_MESSAGE_FLAGS_NONE"));
+                               ccall.add_argument (new CCodeConstant ("NULL"));
+                               ccall.add_argument (new CCodeConstant ("NULL"));
+                               ccode.add_expression (ccall);
+
+                               ccode.close ();
                        }
 
                        // free D-Bus message
diff --git a/tests/Makefile.am b/tests/Makefile.am
index fae0ebe81..681f17c79 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -714,6 +714,7 @@ TESTS = \
        dbus/async-bus.test \
        dbus/async-connection.test \
        dbus/async-errors.test \
+       dbus/async-no-reply.test \
        dbus/connection.test \
        dbus/dbus-name-missing.test \
        dbus/dynamic-method.test \
diff --git a/tests/dbus/async-no-reply.test b/tests/dbus/async-no-reply.test
new file mode 100644
index 000000000..8cca84a68
--- /dev/null
+++ b/tests/dbus/async-no-reply.test
@@ -0,0 +1,95 @@
+Packages: gio-2.0
+D-Bus
+
+Program: client
+
+[DBus (name = "org.example.Test")]
+interface Test : Object {
+       public abstract async string[] list_messages () throws IOError;
+       public abstract async void post_message (string message) throws IOError;
+}
+
+MainLoop main_loop;
+
+async void run () {
+       Test test = yield Bus.get_proxy (BusType.SESSION, "org.example.Test", "/org/example/Test");
+
+       var events = new AsyncQueue<string> ();
+
+       DBusConnection connection = ((DBusProxy) test).g_connection;
+       connection.add_filter ((conn, message, incoming) => {
+               if (message.get_interface () == "org.example.Test" && message.get_member () != 
"ListMessages") {
+                       switch (message.get_message_type ()) {
+                       case DBusMessageType.METHOD_CALL:
+                               events.push (message.get_flags ().to_string ());
+                               break;
+                       default:
+                               assert_not_reached ();
+                       }
+               }
+               return message;
+       });
+
+       string[] messages = yield test.list_messages ();
+       assert (messages.length == 0);
+
+       yield test.post_message ("round-trip");
+       assert (events.pop () == "G_DBUS_MESSAGE_FLAGS_NONE");
+       assert (events.try_pop () == null);
+
+       test.post_message.begin ("fire-and-forget");
+       assert (events.pop () == "G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED");
+       assert (events.try_pop () == null);
+
+       messages = yield test.list_messages ();
+       assert (messages.length == 2);
+       assert (messages[0] == "round-trip");
+       assert (messages[1] == "fire-and-forget");
+
+       main_loop.quit ();
+}
+
+void main () {
+       run.begin ();
+
+       main_loop = new MainLoop (null, false);
+       main_loop.run ();
+}
+
+Program: server
+
+[DBus (name = "org.example.Test")]
+class Test : Object {
+       private string[] messages = new string[0];
+
+       public async string[] list_messages () {
+               return messages;
+       }
+
+       public async void post_message (string message) {
+               messages += message;
+       }
+}
+
+MainLoop main_loop;
+
+void client_exit (Pid pid, int status) {
+       assert (status == 0);
+       main_loop.quit ();
+}
+
+void main () {
+       var conn = Bus.get_sync (BusType.SESSION);
+       conn.register_object ("/org/example/Test", new Test ());
+
+       var request_result = conn.call_sync ("org.freedesktop.DBus", "/org/freedesktop/DBus", 
"org.freedesktop.DBus", "RequestName",
+                                             new Variant ("(su)", "org.example.Test", 0x4), null, 0, -1);
+       assert ((uint) request_result.get_child_value (0) == 1);
+
+       Pid client_pid;
+       Process.spawn_async (null, { "dbus_async_no_reply_client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, 
null, out client_pid);
+       ChildWatch.add (client_pid, client_exit);
+
+       main_loop = new MainLoop ();
+       main_loop.run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]