使用 gdbus-codegen 进行 dbus 方法调用,服务器端可以工作,但客户端的响应字符串始终为 NULL。特别是调用中的缓冲区 new_cfg_gdbus_call_receive_new_config_sync(proxy, "new_cfg", buf, NULL, &error); 是 NULL,我不确定如何在服务器端填充它。
命令行:
gdbus-codegen --generate-c-code new_cfg_gen --c-namespace NewCfg --interface-prefix com.new_cfg. com.new_cfg.GDBUS.xml
XML:
<node>
<interface name="com.new_cfg.GDBUS">
<method name="ReceiveNewConfig">
<arg name="greeting" direction="in" type="s"/>
<arg name="response" direction="out" type="s"/>
</method>
</interface>
</node>
客户:
NewCfgGDBUS *proxy;
GError *error;
gchar **buf = NULL;
error = NULL;
proxy = new_cfg_gdbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
"com.new_cfg", "/com/new_cfg/GDBUS", NULL, &error);
new_cfg_gdbus_call_receive_new_config_sync(proxy, "new_cfg", buf, NULL, &error);
if(NULL != buf && NULL != *buf)
{
g_print("resp: %s\n", *buf);
}
else
{
g_print("resp is NULL");
}
g_object_unref(proxy);
服务器:
static gboolean
on_rcv_new_cfg (NewCfgGDBUS *interface, GDBusMethodInvocation *invocation,
const gchar *greeting, gpointer user_data)
{
gchar *response;
response = g_strdup_printf ("New Config %s is valid.", greeting);
new_cfg_gdbus_complete_receive_new_config (interface, invocation, response);
g_print("%s\n", response);
g_free (response);
return TRUE;
}
static void
on_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data)
{
NewCfgGDBUS *interface;
GError *error;
interface = new_cfg_gdbus_skeleton_new();
g_signal_connect (interface, "handle-receive-new-config", G_CALLBACK (on_rcv_new_cfg), NULL);
error = NULL;
(void)!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface), connection, "/com/new_cfg/GDBUS", &error);
}
服务器端循环:
GMainLoop *loop;
loop = g_main_loop_new (NULL, FALSE);
g_bus_own_name(G_BUS_TYPE_SYSTEM, "com.new_cfg", G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
on_name_acquired, NULL, NULL, NULL);
g_main_loop_run (loop);