-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: Optimize server-side GDBus reply handling
- Don't send a reply if NO_REPLY_EXPECTED is set. This is already taken care of by g_dbus_method_invocation_return_gerror(), so we only need to do this for the non-error case, since we generate the reply message and manually send it. - Because we have to keep arguments alive for the duration of the call, and this requires us to pass a ready callback even in case of NO_REPLY_EXPECTED, there is a challenge: If the implementation is a GDBus proxy, the presence of a ready callback means we don't set NO_REPLY_EXPECTED, even if we don't want the reply. But since we know that a proxy will copy the arguments right away, we can omit the ready callback in that case.
- Loading branch information
Showing
5 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
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/TestRelay"); | ||
|
||
string[] messages = yield test.list_messages (); | ||
assert (messages.length == 0); | ||
|
||
test.post_message.begin ("fire-and-forget"); | ||
|
||
messages = yield test.list_messages (); | ||
assert (messages.length == 1); | ||
assert (messages[0] == "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")] | ||
interface Test : Object { | ||
public abstract async string[] list_messages () throws IOError; | ||
public abstract async void post_message (string message) throws IOError; | ||
} | ||
|
||
class TestImpl : Object, Test { | ||
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; | ||
|
||
async void run () { | ||
var conn = yield Bus.get (BusType.SESSION); | ||
|
||
var events = new AsyncQueue<string> (); | ||
|
||
conn.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; | ||
}); | ||
|
||
conn.register_object ("/org/example/Test", new TestImpl () as Test); | ||
|
||
var request_result = yield conn.call ("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); | ||
|
||
Test test = yield conn.get_proxy ("org.example.Test", "/org/example/Test"); | ||
conn.register_object ("/org/example/TestRelay", test); | ||
|
||
Pid client_pid; | ||
Process.spawn_async (null, { "dbus_async_no_reply_relay_client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, null, out client_pid); | ||
ChildWatch.add (client_pid, (pid, status) => { | ||
assert (status == 0); | ||
run.callback (); | ||
}); | ||
yield; | ||
|
||
for (var i = 0; i < 3; i++) | ||
assert (events.pop () == "G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED"); | ||
assert (events.try_pop () == null); | ||
|
||
main_loop.quit (); | ||
} | ||
|
||
void main () { | ||
run.begin (); | ||
|
||
main_loop = new MainLoop (null, false); | ||
main_loop.run (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
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> (); | ||
var calls = new HashTable<uint32, string> (null, null); | ||
|
||
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: | ||
calls[message.get_serial ()] = message.get_member (); | ||
break; | ||
default: | ||
assert_not_reached (); | ||
} | ||
} | ||
|
||
if (incoming && message.get_message_type () == DBusMessageType.METHOD_RETURN) { | ||
string? method_name = calls[message.get_reply_serial ()]; | ||
if (method_name != null) | ||
events.push (method_name); | ||
} | ||
|
||
return message; | ||
}); | ||
|
||
string[] messages = yield test.list_messages (); | ||
assert (messages.length == 0); | ||
assert (events.try_pop () == null); | ||
|
||
yield test.post_message ("round-trip"); | ||
assert (events.pop () == "PostMessage"); | ||
assert (events.try_pop () == null); | ||
|
||
test.post_message.begin ("fire-and-forget"); | ||
|
||
messages = yield test.list_messages (); | ||
assert (messages.length == 2); | ||
assert (messages[0] == "round-trip"); | ||
assert (messages[1] == "fire-and-forget"); | ||
|
||
assert (events.try_pop () == null); | ||
|
||
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_response_client" }, null, SpawnFlags.DO_NOT_REAP_CHILD, null, out client_pid); | ||
ChildWatch.add (client_pid, client_exit); | ||
|
||
main_loop = new MainLoop (); | ||
main_loop.run (); | ||
} |