如何回复 D-Bus 消息
Posted
技术标签:
【中文标题】如何回复 D-Bus 消息【英文标题】:How to reply a D-Bus message 【发布时间】:2013-11-14 01:19:43 【问题描述】:我得到了 D-Bus server.c 和 client.c 代码,并做了一些修改。 我想要从 client.c 输入例如“hi”的结果 服务器将打印“receive message hi”,并回复“reply_content!!!!!!”到client.c
但现在client.c似乎无法收到回复消息。 有人知道吗?
提前致谢。
"server.c"
/* server.c */
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
DBusMessage *reply;
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message: %s\n", word);
handled = true;
reply = dbus_message_new_method_return(message);
char * reply_content = "reply_content!!!!!!";
dbus_message_append_args(reply, DBUS_TYPE_STRING, &reply_content, DBUS_TYPE_INVALID);
dbus_connection_send(connection, reply, NULL);
dbus_connection_flush(connection);
dbus_message_unref(reply);
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
int main(int argc, char *argv[])
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL))
return -1;
dbus_bus_add_match(dbconn, "type='signal',interface='client.signal.Type'", &dberr);
while(dbus_connection_read_write_dispatch(dbconn, -1))
/* loop */
return 0;
这里是client.c
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message %s\n", word);
handled = true;
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
int db_send(DBusConnection *dbconn)
DBusMessage *dbmsg;
char *word = (char *)malloc(sizeof(char));
int i;
dbmsg = dbus_message_new_signal("/client/signal/Object", "client.signal.Type", "Test");
scanf("%s", word);
if (!dbus_message_append_args(dbmsg, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID))
return -1;
if (!dbus_connection_send(dbconn, dbmsg, NULL))
return -1;
dbus_connection_flush(dbconn);
printf("send message %s\n", word);
dbus_message_unref(dbmsg);
return 0;
int main(int argc, char *argv[])
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL))
return -1;
db_send(dbconn);
while(dbus_connection_read_write_dispatch(dbconn, -1))
db_send(dbconn);
dbus_connection_unref(dbconn);
return 0;
【问题讨论】:
【参考方案1】:您目前仅与没有返回值的信号进行通信。
在client.c中,使用dbus_message_new_method_call
而不是dbus_message_new_signal
,在server.c中你可能需要将type='signal'
改为type='method_call'
。
【讨论】:
以上是关于如何回复 D-Bus 消息的主要内容,如果未能解决你的问题,请参考以下文章