| 1 | Title: D-Bus |
| 2 | |
| 3 | By default, integration exists for D-Bus to connect to a bus, own a name, and perform method calls, asynchronously. |
| 4 | |
| 5 | See [[email protected]_get], [[email protected]_own_name_on_connection], [[email protected]_connection_call], [[email protected]_connection_call_with_unix_fd_list], [[email protected]_connection_send_message_with_reply] and [[email protected]_connection_close]. |
| 6 | |
| 7 | # Enabling GDBus codegen |
| 8 | |
| 9 | Integration with GDBus based codegen can be enabled, by calling `gdbus-codegen` with `--extension-path=path/to/dex-gdbus-codegen-extension.py`. |
| 10 | The installed file path can be queried via `pkgconf --variable=gdbus_codegen_extension libdex-1`: |
| 11 | |
| 12 | ```meson |
| 13 | libdex_dep = dependency('libdex-1') |
| 14 | |
| 15 | ext = libdex_dep.get_pkgconfig_variable('gdbus_codegen_extension') |
| 16 | dbus_ping_pong = gnome.gdbus_codegen( |
| 17 | 'dex-dbus-ping-pong', |
| 18 | sources: ['org.example.PingPong.xml'], |
| 19 | interface_prefix: 'org.example', |
| 20 | namespace: 'DexDbus', |
| 21 | extra_args: [f'--extension-path=@ext@'], |
| 22 | ) |
| 23 | ``` |
| 24 | |
| 25 | # Proxy Futures |
| 26 | |
| 27 | With the GDBus codegen enabled, for every proxy, a `${name}_new_future` function is generated in addition to the sync and async/finish variants. The returned future resolves into the proxy object. |
| 28 | |
| 29 | ```c |
| 30 | g_autoptr(DexDbusPingPong) *pp = NULL; |
| 31 | pp = dex_await_object (dex_dbus_ping_pong_proxy_new_future (connection, |
| 32 | G_DBUS_PROXY_FLAGS_NONE, |
| 33 | "org.example.PingPong", |
| 34 | "/org/example/pingpong"), |
| 35 | &error); |
| 36 | ``` |
| 37 | |
| 38 | For every method, a `${name}_call_${method}_future` function is generated in addition to the sync and async/finish variants. The returned future resolves into the boxed type `${Name}${Method}Result` which contains the results of the call. |
| 39 | |
| 40 | ```c |
| 41 | g_autoptr(DexDbusPingPongPingResult) result = NULL; |
| 42 | |
| 43 | result = dex_await_boxed (dex_dbus_ping_pong_call_ping_future (pp, "ping"), &error); |
| 44 | g_assert (result); |
| 45 | g_print ("%s\n", result->pong); |
| 46 | ``` |
| 47 | |
| 48 | For every signal, a `${name}_wait_${signal}_future` function is generated. The future gets resolved when the signal got emitted, and the future resolves into the boxed type `${Name}${Signal}Signal` which contains the results of the signal emission. |
| 49 | |
| 50 | ```c |
| 51 | g_autoptr(DexDbusPingPongReloadingSignal) signal = NULL; |
| 52 | |
| 53 | signal = dex_await_boxed (dex_dbus_ping_pong_wait_reloading_future (pp), &error); |
| 54 | g_assert (signal); |
| 55 | g_print ("%s\n", signal->active); |
| 56 | ``` |
| 57 | |
| 58 | For every `Dex.DBusInterfaceSkeleton`, a corresponding `${Name}SignalMonitor` class is generated. |
| 59 | Objects of the class will listen to the specified signals, and a call to ``${Name}SignalMonitor::next${signal}` returns a future that will resolve either immediately when a signal was emitted previously, or when the next signal arrives. |
| 60 | This can be useful when it is important to not miss signal emissions. |
| 61 | |
| 62 | ```c |
| 63 | g_autoptr(DexDbusPingPongSignalMonitor) signal_monitor = |
| 64 | dex_dbus_ping_pong_signal_monitor_new (pp, DEX_DBUS_PING_PONG_SIGNAL_RELOADING); |
| 65 | |
| 66 | signal = dex_await_boxed (dex_dbus_ping_pong_signal_monitor_next_reloading (signal_monitor), &error); |
| 67 | g_assert (signal); |
| 68 | g_print ("%s\n", signal->active); |
| 69 | ``` |
| 70 | |
| 71 | # InterfaceSkeleton Fiber Dispatching |
| 72 | |
| 73 | With the GDBus codegen enabled, all generated `${Name}Skeleton`s that application code derives from to implement a service derive from `DexDBusInterfaceSkeleton` instead of directly from `GDBusInterfaceSkeleton`. |
| 74 | This allows application code to enable handling method invocations in fibers, by setting [[email protected]_METHOD_INVOCATIONS_IN_FIBER] with [[email protected]_flags]. |
| 75 | |
| 76 | ```c |
| 77 | static gboolean |
| 78 | handle_ping (DexDbusPingPong *object, |
| 79 | GDBusMethodInvocation *invocation, |
| 80 | const char *ping) |
| 81 | { |
| 82 | g_print ("service: %s\n", ping); |
| 83 | |
| 84 | dex_await (dex_timeout_new_seconds (1), NULL); |
| 85 | dex_dbus_ping_pong_complete_ping (object, invocation, "pong"); |
| 86 | |
| 87 | return G_DBUS_METHOD_INVOCATION_HANDLED; |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | dex_dbus_ping_pong_iface_init (DexDbusPingPongIface *iface) |
| 92 | { |
| 93 | iface->handle_ping = handle_ping; |
| 94 | } |
| 95 | |
| 96 | static DexPingPong * |
| 97 | get_ping_pong (void) |
| 98 | { |
| 99 | g_autoptr(DexPingPong) pp = NULL; |
| 100 | pp = g_object_new (DEX_TYPE_PING_PONG, NULL); |
| 101 | dex_dbus_interface_skeleton_set_flags (DEX_DBUS_INTERFACE_SKELETON (pp), |
| 102 | DEX_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_FIBER); |
| 103 | return g_steal_pointer (&pp); |
| 104 | } |
| 105 | ``` |