C DBus:使用直接DBUSAPI测量数据交换的时间
我正在寻找一个示例c代码,以测量在两个简单应用程序之间发送数据而不使用任何glib绑定所花费的时间,我在许多文章中看到了一个示例,但现在不存在链接 我的代码发送时没有给出任何错误,但receive应用程序没有接收任何内容,也附加了service.conf文件。从其他一些网站获取此代码:C DBus:使用直接DBUSAPI测量数据交换的时间,c,dbus,c-api,C,Dbus,C Api,我正在寻找一个示例c代码,以测量在两个简单应用程序之间发送数据而不使用任何glib绑定所花费的时间,我在许多文章中看到了一个示例,但现在不存在链接 我的代码发送时没有给出任何错误,但receive应用程序没有接收任何内容,也附加了service.conf文件。从其他一些网站获取此代码: ////////////////////////////Sender Code #include <stdlib.h> #include <stdio.h> #include <s
////////////////////////////Sender Code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <dbus/dbus.h>
#define false 0
#define true 1
#define bool char
#define DERR_SHOW_CLEAN(error) \
if (dbus_error_is_set(&(error))) \
{ \
printf("error [%s:%d] %s\n %s\n", \
__FILE__, __LINE__, error.name, error.message); \
dbus_error_free(&(error)); \
}
int main() {
dbus_bool_t rc;
DBusError error;
DBusConnection* conn;
int ret;
char* sigvalue = "Test";
// initialise the errors
dbus_error_init(&error); //TODO
conn = dbus_connection_open("unix:path=/dev/shmem/dbus_service_socket", &error);
DERR_SHOW_CLEAN(error);
dbus_uint32_t serial = 0; // unique number to associate replies with requests
DBusMessage* msg;
DBusMessageIter args;
rc = dbus_bus_register(conn, &error);
DERR_SHOW_CLEAN(error);
// create a signal and check for errors
msg = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
"org.freedesktop.DBus", // interface name of the signal
"Test"); // name of the signal
if (NULL == msg)
{
fprintf(stderr, "Message Null\n");
}
// 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");
}
while(1)
{
// send the message and flush the connection
if (!dbus_connection_send(conn, msg, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
break;
}
else
{
sleep(5);
}
}
dbus_connection_flush(conn);
// free the message
dbus_message_unref(msg);
return 0;
}
////////////////////////////Receiver Code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <dbus/dbus.h>
#define false 0
#define true 1
#define bool char
#define DERR_SHOW_CLEAN(error) \
if (dbus_error_is_set(&(error))) \
{ \
printf("error [%s:%d] %s\n %s\n", \
__FILE__, __LINE__, error.name, error.message); \
dbus_error_free(&(error)); \
}
int main()
{
DBusError err;
DBusConnection* conn;
DBusMessage* msg;
DBusMessageIter args;
int ret;
char* sigvalue;
dbus_bool_t rc;
// initialise the errors
dbus_error_init(&err); //TODO
conn = dbus_connection_open("unix:path=/dev/shmem/dbus_service_socket", &err);
DERR_SHOW_CLEAN(err);
rc = dbus_bus_register(conn, &err);
DERR_SHOW_CLEAN(err);
while (true) {
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
printf("Got Si3\n");
// loop again if we haven't read a message
if (NULL == msg) {
sleep(5);
continue;
}
// check if the message is a signal from the correct interface
// and with the correct name
if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "Test")) {
// read the parameters
printf("Got Si4\n");
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message has no arguments!\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, &sigvalue);
printf("Got Signal with value %s\n", sigvalue);
}
}
else
{
printf("Got Signal with type %s\n", dbus_message_get_sender(msg));
}
// free the message
dbus_message_unref(msg);
}
}
/////service.conf file contents which is used while dbus-daemon starts
<!-- This configuration file controls the systemwide message bus.
Add a system-local.conf and edit that rather than changing this
file directly. -->
<!-- Note that there are any number of ways you can hose yourself
security-wise by screwing up this file; in particular, you
probably don't want to listen on any more addresses, add any more
auth mechanisms, run as a different user, etc. -->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- Our well-known bus type, do not change this -->
<type>system</type>
<!-- Run as special user -->
<user>root</user>
<!-- Fork into daemon mode -->
<fork/>
<!-- Write a pid file -->
<pidfile>/dev/shmem/dbus_service.pid</pidfile>
<!-- Enable logging to syslog -->
<syslog/>
<!-- allow everyone -->
<allow_anonymous />
<!-- Only allow socket-credentials-based authentication -->
<!-- <auth>EXTERNAL</auth>-->
<!-- Only listen on a local socket. (abstract=/path/to/socket
means use abstract namespace, don't really create filesystem
file; only Linux supports this. Use path=/whatever on other
systems.) -->
<listen>unix:path=/dev/shmem/dbus_service_socket</listen>
<policy context="default">
<!-- All users can connect to system bus -->
<allow user="*"/>
<allow group="*"/>
<allow own="*"/>
<!-- Signals and reply messages (method returns, errors) are allowed
by default -->
<allow send_type="signal"/>
<allow send_type="method_call" log="true"/>
<allow send_requested_reply="true" send_type="method_return"/>
<allow send_requested_reply="true" send_type="error"/>
<!-- All messages may be received by default -->
<allow receive_type="method_call"/>
<allow receive_type="method_return"/>
<allow receive_type="error"/>
<allow receive_type="signal"/>
<allow receive_interface="org.freedesktop.DBus.Introspectable"/>
<allow send_destination="*"/>
<allow receive_sender="*" />
<allow receive_path="/org/freedesktop/DBus"/>
<!-- Allow anyone to talk to the message bus -->
<allow send_destination="org.freedesktop.DBus"/>
</policy>
<!-- Config files are placed here that among other things, punch
holes in the above policy for specific services. -->
<includedir>service.d</includedir>
</busconfig>
//发件人代码
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义false 0
#定义真1
#定义布尔字符
#定义DERR\u SHOW\u CLEAN(错误)\
如果(dbus_error_为_集(&(error)))\
{ \
printf(“错误[%s:%d]%s\n%s\n”\
__文件“,”行“,”error.name,error.message)\
dbus_无错误(&(错误))\
}
int main(){
dbus_bool_t rc;
DBusError错误;
DBU连接*conn;
int ret;
char*sigvalue=“测试”;
//初始化错误
dbus_error_init(&error);//TODO
conn=dbus\u连接\u打开(“unix:path=/dev/shmem/dbus\u服务\u套接字”,&错误);
DERR_SHOW_CLEAN(错误);
dbus_uint32_t serial=0;//将回复与请求关联的唯一编号
DBusMessage*msg;
DBusMessageIter参数;
rc=dbus_总线_寄存器(连接和错误);
DERR_SHOW_CLEAN(错误);
//创建一个信号并检查错误
msg=dbus\u message\u new\u信号(“/org/freedesktop/dbus)”,//信号的对象名称
“org.freedesktop.DBus”,//信号的接口名称
“Test”);//信号的名称
if(NULL==msg)
{
fprintf(stderr,“消息空\n”);
}
//在信号上附加参数
dbus_消息_iter_init_append(msg和args);
如果(!dbus_message_iter_append_basic(&args,dbus_TYPE_STRING,&sigvalue)){
fprintf(stderr,“内存不足!\n”);
}
而(1)
{
//发送消息并刷新连接
if(!dbus_connection_send(连接、消息和串行)){
fprintf(stderr,“内存不足!\n”);
打破
}
其他的
{
睡眠(5);
}
}
dbus_连接_冲洗(连接);
//释放消息
dbus_message_unref(msg);
返回0;
}
////////////////////////////接收代码
#包括
#包括
#包括
#包括
#包括
#包括
#定义false 0
#定义真1
#定义布尔字符
#定义DERR\u SHOW\u CLEAN(错误)\
如果(dbus_error_为_集(&(error)))\
{ \
printf(“错误[%s:%d]%s\n%s\n”\
__文件“,”行“,”error.name,error.message)\
dbus_无错误(&(错误))\
}
int main()
{
DBusError错误;
DBU连接*conn;
DBusMessage*msg;
DBusMessageIter参数;
int ret;
char*sigvalue;
dbus_bool_t rc;
//初始化错误
dbus_error_init(&err);//TODO
conn=dbus\u连接\u打开(“unix:path=/dev/shmem/dbus\u服务\u套接字”,&err);
显示清洁(错误);
rc=dbus_总线_寄存器(连接和错误);
显示清洁(错误);
while(true){
//非阻塞读取下一条可用消息
dbus连接读写(conn,0);
msg=dbus\u连接\u弹出消息(conn);
printf(“获得Si3\n”);
//如果我们没有读到消息,请再次循环
if(NULL==msg){
睡眠(5);
继续;
}
//检查消息是否为来自正确接口的信号
//用正确的名字
if(dbus_message_是信号(msg,“org.freedesktop.dbus”,“Test”)){
//读取参数
printf(“Got Si4\n”);
if(!dbus_message_iter_init(msg和args))
fprintf(stderr,“消息没有参数!\n”);
else if(DBUS_TYPE_STRING!=DBUS_message_iter_get_arg_TYPE(&args))
fprintf(stderr,“参数不是字符串!\n”);
否则{
dbus\u message\u iter\u get\u basic(&args,&sigvalue);
printf(“获得值为%s\n的信号”,SIGVUARE);
}
}
其他的
{
printf(“获取类型为%s\n的信号”,dbus_message_get_sender(msg));
}
//释放消息
dbus_message_unref(msg);
}
}
/////dbus守护进程启动时使用的service.conf文件内容
系统
根
/dev/shmem/dbus_service.pid
unix:path=/dev/shmem/dbus\u服务\u套接字
服务.d
在进入接收主函数的while循环之前,调用dbus\u bus\u add\u match,它会工作
参考示例:at