Gdbus服务器和客户端demo
时间: 2023-10-06 08:08:28 浏览: 78
GDBus 是 GNOME 桌面环境中使用的 D-Bus 实现。D-Bus 是一种进程间通信机制,可以在不同进程之间传递消息。
下面是一个简单的 GDBus 服务器和客户端的示例代码:
## 服务器
```c
#include <gio/gio.h>
static gboolean on_handle_echo (GDBusConnection *connection,
const gchar *sender,
const gchar *object_path,
const gchar *interface_name,
const gchar *method_name,
GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
gchar *str;
g_variant_get (parameters, "(&s)", &str);
g_print ("Received: %s\n", str);
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", str));
return TRUE;
}
int main (void)
{
GMainLoop *loop;
GDBusNodeInfo *introspection_data;
GError *error = NULL;
static const GDBusInterfaceVTable interface_vtable =
{
on_handle_echo,
NULL,
NULL
};
loop = g_main_loop_new (NULL, FALSE);
introspection_data = g_dbus_node_info_new_for_xml ("<node>"
" <interface name='com.example.Echo'>"
" <method name='Echo'>"
" <arg type='s' name='input' direction='in'/>"
" <arg type='s' name='output' direction='out'/>"
" </method>"
" </interface>"
"</node>",
&error);
if (error != NULL)
{
g_printerr ("Error parsing introspection XML: %s\n", error->message);
g_error_free (error);
return 1;
}
guint registration_id;
registration_id = g_dbus_connection_register_object (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL),
"/com/example/Echo",
introspection_data->interfaces[0],
&interface_vtable,
NULL,
NULL,
&error);
if (error != NULL)
{
g_printerr ("Error registering object: %s\n", error->message);
g_error_free (error);
return 1;
}
g_print ("Listening on /com/example/Echo\n");
g_main_loop_run (loop);
g_dbus_connection_unregister_object (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL), registration_id);
g_dbus_node_info_unref (introspection_data);
g_main_loop_unref (loop);
return 0;
}
```
这个服务器注册了一个名为 `com.example.Echo` 的 D-Bus 接口,并提供了一个名为 `Echo` 的方法,该方法接受一个字符串参数并返回相同的字符串。
## 客户端
```c
#include <gio/gio.h>
int main (void)
{
GDBusProxy *proxy;
GDBusConnection *connection;
GVariant *result;
GError *error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (connection == NULL)
{
g_printerr ("Failed to get D-Bus connection: %s\n", error->message);
g_error_free (error);
return 1;
}
proxy = g_dbus_proxy_new_sync (connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"com.example.Echo",
"/com/example/Echo",
"com.example.Echo",
NULL,
&error);
if (proxy == NULL)
{
g_printerr ("Failed to create D-Bus proxy: %s\n", error->message);
g_error_free (error);
return 1;
}
const gchar *input = "Hello, world!";
result = g_dbus_proxy_call_sync (proxy,
"Echo",
g_variant_new ("(s)", input),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (result == NULL)
{
g_printerr ("Failed to call Echo method: %s\n", error->message);
g_error_free (error);
return 1;
}
gchar *output;
g_variant_get (result, "(s)", &output);
g_print ("Echo response: %s\n", output);
g_variant_unref (result);
g_object_unref (proxy);
g_object_unref (connection);
return 0;
}
```
这个客户端连接到 D-Bus 会话总线上的 `com.example.Echo` 接口,并调用其 `Echo` 方法,将字符串 "Hello, world!" 传递给它并输出响应。
需要注意的是,服务器和客户端代码都需要链接 `gio-2.0` 库。可以使用以下命令编译并链接它们:
```bash
gcc -o server server.c $(pkg-config --cflags --libs gio-2.0)
gcc -o client client.c $(pkg-config --cflags --libs gio-2.0)
```
阅读全文