当前位置: 技术问答>linux和unix
Dbus如何发送接受一个结构题数据
来源: 互联网 发布时间:2016-12-05
本文导语: 这样的一个结构题数据如果用Dbus发送和接受呢? 比如 typedef struct student { int sid; char name[30]; }ST_STUDENT; 我的Dbus封装借口可以发送char* 可是传入结构题就不能接受数据了。 问题也定位出来了, ...
这样的一个结构题数据如果用Dbus发送和接受呢?
比如
typedef struct student
{
int sid;
char name[30];
}ST_STUDENT;
我的Dbus封装借口可以发送char* 可是传入结构题就不能接受数据了。 问题也定位出来了, 望牛人解答啊。
代码如下:
#include
#include
#include
#include
#include
#include
#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 Nulln");
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 Sentn");
// 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 signalsn");
// 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 sentn");
// 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 foundn");
// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message Has No Parametersn");
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 successdn");
flag = 1;
}
// free the message
dbus_message_unref(msg);
if (flag) break;
}
return DBUS_NO_ERROR;
}
好像在发送数据时候的 dbus_message_is_signal(msg, siginterfacename, signame) 堵塞了, 相同的代码char*数据就没有堵塞。 有高人能看出问题, 或者给个传送结构题数据的示例代码吗?
比如
typedef struct student
{
int sid;
char name[30];
}ST_STUDENT;
我的Dbus封装借口可以发送char* 可是传入结构题就不能接受数据了。 问题也定位出来了, 望牛人解答啊。
代码如下:
#include
#include
#include
#include
#include
#include
#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 Nulln");
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 Sentn");
// 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 signalsn");
// 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 sentn");
// 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 foundn");
// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message Has No Parametersn");
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 successdn");
flag = 1;
}
// free the message
dbus_message_unref(msg);
if (flag) break;
}
return DBUS_NO_ERROR;
}
好像在发送数据时候的 dbus_message_is_signal(msg, siginterfacename, signame) 堵塞了, 相同的代码char*数据就没有堵塞。 有高人能看出问题, 或者给个传送结构题数据的示例代码吗?
|
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
像你的注释写的,是非阻塞方式读取消息,如果没有可用消息,应该返回空吧
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
像你的注释写的,是非阻塞方式读取消息,如果没有可用消息,应该返回空吧
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。