D-Bus 也是一个用于进程间通信的框架。
简单介绍一下如何编写一个helloworld通信例子。
0. 下载源码
http://www.freedesktop.org/wiki/Software/dbus#Download.
1. ./configure
这时候可能会出现问题:
configure: error: Could not find expat.h, check config.log for failed attempts
可以通过
sudo apt-get install libexpat1-dev
来解决
2. make
3. make install
其中可能遇到的编译问题
1. fatal error: dbus/dbus.h: No such file or directory
把/usr/local/include/dbus-1.0/dbus/拷贝到/usr/include/dbus
2. fatal error: dbus/dbus-arch-deps.h: No such file or directory
把/usr/local/lib/dbus-1.0/include/dbus/dbus-arch-deps.h拷贝到/usr/include/dbus
3. 链接错误 collect2: ld returned 1 exit status
添加链接选项 -l dbus-1
测试代码
d-bus服务端接收消息
- /* 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)
- {
- dbus_bool_t handled = false;
- char *word = NULL;
- DBusError dberr;
- if (dbus_message_is_signal(message, "client.signal.Type", "Test")) {
- dbus_error_init(&dberr);
- dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING,
- &word, DBUS_TYPE_INVALID);
- if (dbus_error_is_set(&dberr)) {
- dbus_error_free(&dberr);
- } else {
- printf("receive message %s\n", word);
- handled = true;
- }
- }
- 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_error_is_set(&dberr)) {
- dbus_error_free(&dberr);
- return -1;
- }
- if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL)) {
- return -1;
- }
- dbus_bus_add_match(dbconn, "type='signal',interface='client.signal.Type'", &dberr);
- if (dbus_error_is_set(&dberr)) {
- dbus_error_free(&dberr);
- return -1;
- }
- while(dbus_connection_read_write_dispatch(dbconn, -1)) {
- /* loop */
- }
- return 0;
- }
客户端发送消息
- #include <dbus/dbus.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- int db_send(DBusConnection *dbconn)
- {
- DBusMessage *dbmsg;
- char *word;
- int i;
- dbmsg = dbus_message_new_signal("/client/signal/Object", // object name of the signal
- "client.signal.Type", // interface name of the signal
- "Test"); // name of the signal
- if (!dbmsg) {
- return -1;
- }
- word = "hello world";
- 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;
- int i;
- dbus_error_init(&dberr);
- dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
- if (dbus_error_is_set(&dberr)) {
- return -1;
- }
- db_send(dbconn);
- dbus_connection_unref(dbconn);
- return 0;
- }
测试结果如下
root@ubuntu:~/dbus# gcc -o client client.c -l dbus-1
root@ubuntu:~/dbus# gcc -o server server.c -l dbus-1
root@ubuntu:~/dbus# ./listen
receive message hello world
root@ubuntu:~/dbus# ./send
send message hello world
很多API和具体实现还没搞清楚,有待深入研究。
D-Bus的性能会比linux自带的send,recv高么?
来源:CSDN
作者:nuptxiaoli0518
链接:https://blog.csdn.net/u014338577/article/details/53502553