2

我得到了 D-Bus server.c 和 client.c 代码,并做了一些修改。我想要的结果是,当从 client.c 服务器键入例如“hi”时,将打印“receive message hi”,并回复“reply_content !!!!!!” 到客户端.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;
}
4

1 回答 1

0

您目前仅与没有返回值的信号进行通信。

在 client.c 中,使用dbus_message_new_method_call代替dbus_message_new_signal,而在 server.c 中,您可能必须更改type='signal'type='method_call'.

于 2013-11-04T22:20:40.960 回答