Dbus如何发送接受一个结构题数据

hulunizai 2011-06-07 09:26:30
这样的一个结构题数据如果用Dbus发送和接受呢?

比如
typedef struct student
{
int sid;
char name[30];
}ST_STUDENT;


我的Dbus封装借口可以发送char* 可是传入结构题就不能接受数据了。 问题也定位出来了, 望牛人解答啊。
代码如下:


#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mydbus.h"
//#include "debug.h"

/*
* Connect to the DBUS bus and send a broadcast signal
*/
int sendsignal(const char* siginterfacename, const char* signame, const void* sigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
dbus_uint32_t serial = 0;

printf("Sending signal...\n");

// initialise the error value
dbus_error_init(&err);

// connect to the DBUS system bus, and check for errors
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
return DBUS_GET_CONNECTION_ERROR;
}

// register our name on the bus, and check for errors
ret = dbus_bus_request_name(conn, "test.signal.source", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
return DBUS_REQUSET_NAME_ERROR;
}

// create a signal & check for errors
msg = dbus_message_new_signal("/test/signal/Object", // object name of the signal
siginterfacename, // interface name of the signal
signame); // name of the signal
if (NULL == msg)
{
fprintf(stderr, "Message Null\n");
return DBUS_MESSAGE_NEW_SIGNAL_ERROR;
}

// append arguments onto signal
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) {
fprintf(stderr, "Out Of Memory!\n");
return DBUS_MESSAGE_ITER_APPEND_BASIC_ERROR;
}

// send the message and flush the connection
if (!dbus_connection_send(conn, msg, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
return DBUS_SEND_MESSAGE_ERROR;
}
dbus_connection_flush(conn);

printf("Signal Sent\n");

// free the message
dbus_message_unref(msg);

return DBUS_NO_ERROR;
}

/*
* Listens for signals on the bus
*/
int receive(const char* siginterfacename, const char* signame, void** recvsigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
char dbus_match[BUFSIZE];
int ret;
//char *sigvalue;
int flag = 0;

printf("Listening for signals\n");

// initialise the errors
dbus_error_init(&err);

// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
return DBUS_GET_CONNECTION_ERROR;
}

// request our name on the bus and check for errors
ret = dbus_bus_request_name(conn, "test.signal.sink", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
return DBUS_REQUSET_NAME_ERROR;
}

// add a rule for which messages we want to see
sprintf(dbus_match, "type='signal',interface='%s'", siginterfacename);
dbus_bus_add_match(conn, dbus_match, &err); // see signals from the given interface
dbus_connection_flush(conn);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Match Error (%s)\n", err.message);
return DBUS_ADD_MATCH_ERROR;
}
printf("Match rule sent\n");

// loop listening for signals being emmitted
while (true) {

// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);

// loop again if we haven't read a message
if (NULL == msg) {
sleep(1);
continue;
}

printf("listen...\n");
// check if the message is a signal from the correct interface and with the correct name
if (dbus_message_is_signal(msg, siginterfacename, signame)) {

printf("signal has found\n");
// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message Has No Parameters\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not string!\n");
else
dbus_message_iter_get_basic(&args, recvsigvalue);

printf("Got Signal with value successd\n");

flag = 1;
}

// free the message
dbus_message_unref(msg);
if (flag) break;
}

return DBUS_NO_ERROR;
}


好像在发送数据时候的 dbus_message_is_signal(msg, siginterfacename, signame) 堵塞了, 相同的代码char*数据就没有堵塞。 有高人能看出问题, 或者给个传送结构题数据的示例代码吗?
...全文
245 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hulunizai 2011-06-09
  • 打赏
  • 举报
回复
dbus_connection_read_write()函数是阻塞的,只要连接没有断开返回true,
dbus_connection_pop_message(conn) 函数是取该连接的最后一个消息, 取不到就返回NULL。
可是sendsignal()传参为char*数据时 dbus_connection_pop_message(conn) 返回不为空.
sendsignal()传参为int struct数据时 dbus_connection_pop_message(conn) 返回为空.


下面是2个接口的介绍
dbus_connection_read_write()
--------------------------------------
As long as the connection is open, this function will block until it can read or write, then read or write, then return #TRUE.
If the connection is closed, the function returns #FALSE.
dbus_connection_pop_message()
--------------------------------------
Returns the first-received message from the incoming message queue, removing it from the queue. The caller owns a reference to the returned message. If the queue is empty, returns #NULL.
justkk 2011-06-09
  • 打赏
  • 举报
回复
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);

像你的注释写的,是非阻塞方式读取消息,如果没有可用消息,应该返回空吧
hulunizai 2011-06-09
  • 打赏
  • 举报
回复
怎么没有人回复呢, 各路神仙帮帮忙啊
hulunizai 2011-06-07
  • 打赏
  • 举报
回复
我刚定位了下 是
msg = dbus_connection_pop_message(conn);

// loop again if we haven't read a message
if (NULL == msg) {
sleep(1);
continue;
}
陷入了死循环 dbus_connection_pop_message借口总是返回空值。 不是dbus_message_is_signal堵塞了

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧