- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我们有一个来自 libuv
的事件循环来处理 unixsockets 和 TCP 套接字。该程序现在还必须处理 DBus,为此我们决定使用 sd-bus
。
伦纳特 wrote on his blog :
Note that our APIs, including sd-bus, integrate nicely into sd-event
event loops, but do not require it, and may be integrated into other
event loops too, as long as they support watching for time and I/O events.
所以我想,这一定是可能的。
我可以通过 sd_bus_get_fd (sd_bus *bus)
获取 dbus 套接字 fd
。但是我找不到任何明显的方法来阻止 sd-bus 使用其 bus_poll
方法在内部等待事件。
例如,当使用 sd_bus_call(...)
调用方法时 will block与 ppoll
.
那么:我如何处理 libuv 中的 dbus 事件?
最佳答案
我明白了,这里有一个关于如何结合 C++、libuv 和 sd-bus 的例子:
我建议您阅读 http://0pointer.de/blog/the-new-sd-bus-api-of-systemd.html总体上了解 sd-bus。
这些是我在 https://github.com/TheJJ/horst 上实现的代码片段
然后可以使用不阻塞的 sd_bus_call_async
完成方法调用(与 sd_bus_call
相对)。不要忘记在 sd_bus_call_async
之后调用 update_events()
,以便通过套接字发送调用!
/**
* Callback function that is invoked from libuv
* once dbus events flowed in.
*/
static void on_dbus_ready(uv_poll_t *handle, int /*status*/, int /*events*/) {
DBusConnection *connection = (DBusConnection *)handle->data;
sd_bus *bus = connection->get_bus();
// let dbus handle the available events request
while (true) {
// this will trigger the dbus vtable-registered functions
int r = sd_bus_process(bus, nullptr);
if (r < 0) {
printf("[dbus] Failed to process bus: %s", strerror(-r));
break;
}
else if (r > 0) {
// try to process another request!
continue;
}
else {
// no more progress, wait for the next callback.
break;
}
}
// update the events we watch for on the socket.
connection->update_events();
}
/**
* Convert the sdbus-returned poll flags to
* corresponding libuv flags.
*/
int poll_to_libuv_events(int pollflags) {
int ret = 0;
if (pollflags & (POLLIN | POLLPRI)) {
ret |= UV_READABLE;
}
if (pollflags & POLLOUT) {
ret |= UV_WRITABLE;
}
// we also have the non-corresponding UV_DISCONNECT
return ret;
}
class DBusConnection {
public:
DBusConnection(Satellite *sat);
virtual ~DBusConnection() = default;
/** connect to dbus */
int connect() {
int r = sd_bus_open_system(&this->bus);
if (r < 0) {
printf("[dbus] Failed to connect to bus: %s", strerror(-r));
goto clean_return;
}
r = sd_bus_add_object_vtable(
this->bus,
&this->bus_slot,
"/rofl/lol", // object path
"rofl.lol", // interface name
your_vtable,
this // this is the userdata that'll be passed
// to the dbus methods
);
if (r < 0) {
printf("[dbus] Failed to install the horst sdbus object: %s", strerror(-r));
goto clean_return;
}
// register our service name
r = sd_bus_request_name(this->bus, "moveii.horst", 0);
if (r < 0) {
printf("[dbus] Failed to acquire service name: %s", strerror(-r));
goto clean_return;
}
// register the filedescriptor from
// sd_bus_get_fd(bus) to libuv
uv_poll_init(this->loop, &this->connection, sd_bus_get_fd(this->bus));
// make `this` reachable in callbacks.
this->connection.data = this;
// init the dbus-event-timer
uv_timer_init(this->loop, &this->timer);
this->timer.data = this;
// process initial events and set up the
// events and timers for subsequent calls
on_dbus_ready(&this->connection, 0, 0);
printf("[dbus] Listener initialized");
return 0;
clean_return:
sd_bus_slot_unref(this->bus_slot);
sd_bus_unref(this->bus);
this->bus = nullptr;
return 1;
}
/** update the events watched for on the filedescriptor */
void update_events() {
sd_bus *bus = this->get_bus();
// prepare the callback for calling us the next time.
int new_events = poll_to_libuv_events(
sd_bus_get_events(bus)
);
uint64_t usec;
int r = sd_bus_get_timeout(bus, &usec);
if (not r) {
// if the timer is running already, it is stopped automatically
// inside uv_timer_start.
uv_timer_start(
&this->timer,
[] (uv_timer_t *handle) {
// yes, handle is not a poll_t, but
// we just care for its -> data member anyway.
on_dbus_ready((uv_poll_t *)handle, 0, 0);
},
usec / 1000, // time in milliseconds, sd_bus provides µseconds
0 // don't repeat
);
}
// always watch for disconnects:
new_events |= UV_DISCONNECT;
// activate the socket watching,
// and if active, invoke the callback function
uv_poll_start(&this->connection, new_events, &on_dbus_ready);
}
/** close the connections */
int close() {
// TODO: maybe this memoryerrors when the loop actually
// does the cleanup. we have to wait for the callback.
uv_close((uv_handle_t *) &this->timer, nullptr);
uv_poll_stop(&this->connection);
sd_bus_close(this->bus);
sd_bus_slot_unref(this->bus_slot);
sd_bus_unref(this->bus);
return 0;
}
/**
* Return the bus handle.
*/
sd_bus *get_bus() const {
return this->bus;
}
protected:
/**
* loop handle
*/
uv_loop_t *loop;
/**
* polling object for dbus events
*/
uv_poll_t connection;
/**
* dbus also wants to be called periodically
*/
uv_timer_t timer;
/**
* dbus bus handle
*/
sd_bus *bus;
/**
* dbus slot handle
*/
sd_bus_slot *bus_slot;
};
关于c++ - libuv 中 sd-bus 的事件循环处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40871126/
在我的代码中,我想以编程方式选择一些变量,并以硬编码方式选择和重命名其他一些变量。我知道我可以通过 setnames() 分两步实现这一点。 ,但我很好奇如何一步完成。 我想我很接近它通过 .SDco
(添加了可重现的示例。) 我对 rnorm 函数有点困惑。 我期待 mean(rnorm(100,mean=0,sd=1))为0;和 sd(rnorm(100,mean=0,sd=1))为 1。但给出
我想创建一个包含多个不同列的数据框,其中包含平均值,之后 sd 显示在括号中。举个例子: df % group_by(Species) %>% summarise_all(list(~ s
我很想知道 SD 卡是否提供类似于“SMART”信息的内容,例如硬盘和 SSD。 我有兴趣在 Raspberry PI 中检查 SD 卡的健康信息,以进行预防性更换。我的写入需求非常高,对设备的物理访
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
是否可以通过编程将 sd 卡中存在的文件夹复制到存在同一 sd 卡的另一个文件夹?? 如果是这样,该怎么做? 最佳答案 该示例的改进版本: // If targetLocation does not
Link :I worked on based on this Link 我添加了这一行来查找尺寸(内部和外部)尺寸, return availableExternalMemorySize/(1024
我正在开发一个应用程序,其中我需要从 sd 卡 中选择一个图像并在 ImageView 中显示它。现在我希望用户通过单击一个按钮来减小/增加其宽度,然后将其保存回 sd 卡。 我已经完成了图像挑选并在
在我的应用程序中,我使用以下 Intent 获得了 SD 卡写入权限。如果用户从系统文件资源管理器中选择 sd 卡文件夹,那么我就有 sd 卡写权限。 Intent intent = new Inte
给定一个data.table library(data.table) DT = data.table(x=rep(c("b","a","c"),each=3), v=c(1,1,1,2,2,1,1,2
我正在构建一个程序,该程序对 pin0 上的模拟电压进行 10 次测量,并将其打印到日志文件中。当我尝试确保文件为空时,我遇到了这个问题。我正在使用 SD.remove() 来删除以前的日志文件。当我
在 Android 的 API > 19 中是否有任何方法可以获取可移动 SD 卡的路径? 与外部 SD 卡一样,我们有 Environment.getExternalStorageDirectory
我使用以下方法检查手机是否包含 SD 卡,但如果 SD 卡不可用,问题总是返回 true,请帮助我。 Boolean isSDPresent = android.os.Environment.getE
这是我将 512 字节块写入 SD 卡的代码。代码工作正常,但是当我检查一切正常时(通过阅读 SD 的响应),我读到 0xFF . 该值应该类似于(来自 SD 引用手册): ‘010’—Data ac
我有两个线程在 epoll 上运行。一个线程尝试与服务器建立 TCP 连接,使用 EPOLL_CTL_ADD 选项将套接字添加到 epoll-fd。 另一个线程负责等待添加到 epoll-fd 的 S
我正在使用 eclipse 模拟器,我想以编程方式将一些 mp3 从 /sdcard/songs 复制到 /sdcard/backup,有什么办法吗?非常感谢任何帮助和代码 fragment !谢谢!
我正在使用 Docker Desktop for Mac 版本 2.1.0.4。我有一个 Docker 容器,它是一个 Ubuntu 18.04 Linux VM,里面有 Yocto Build 系统
好的,这个黑莓应用程序在第一次安装和运行时创建了一个数据库。它安装在 SD 卡上。 当我删除应用程序时 - 这个文件仍然存在,我在删除应用程序时找不到任何删除它的方法。 有什么建议么? 最佳答案 应用
我有一张 SD 卡(或 SDHC 卡)通过 SPI 模式连接到微 Controller 。我正在使用 Chan’s FAT图书馆。我将来自 8192 字节缓冲区的数据写入其中(由于 RAM 不足,缓冲
我想在删除该选择中的最小值和最大值后,计算数据框中每一行在该选择列上的标准差。这是一个例子: set.seed(1) dat dat X1 X2 X3 X4 X5 sd 1 27 5
我是一名优秀的程序员,十分优秀!