Paw*_*cki 3 c c++ libusb libevent libusb-1.0
我正在使用 libevent 编写一个事件驱动的应用程序,并且需要使用 libusb-1.0 进行 USB 传输。
我想使用libusb_get_pollfds获取文件描述符列表(在 中fds)并将它们添加到 libevent,如下所示:
const struct libusb_pollfd **fds = libusb_get_pollfds(device->context);
const struct libusb_pollfd *it = *fds;
for(;it != NULL; ++it) {
cout << "Adding fd: " << it->fd << ", " << it->events << endl;
struct event * ev = event_new(base_,
it->fd, it->events | EV_PERSIST,
callbacks::libusb_cb, this);
event_add(ev, 0);
libusb_fds_events.insert(std::make_pair(it->fd, ev));
}
free(fds);
// (...)
// And the callback function:
void callbacks::libusb_cb(evutil_socket_t fd, short what, void *arg) {
Server *s = reinterpret_cast<Server*>(arg);
libusb_handle_events_timeout(s->device_->context, 0);
}
Run Code Online (Sandbox Code Playgroud)
另外,我使用libusb_set_pollfd_notifiers从libusb_fds_events.
问题是我在 libusb 返回的列表上得到了许多奇怪的 fd(例如,我stdin多次得到 (!) 事件等于 0)。
我是否以正确的方式使用它?
我发现代码中有错误。本来应该是:
const struct libusb_pollfd **it = fds;
for(;*it != NULL; ++it) {
cout << "Adding fd: " << (*it)->fd << ", " << (*it)->events << endl;
struct event * ev = event_new(base_,
(*it)->fd, (*it)->events | EV_PERSIST,
callbacks::libusb_cb, this);
event_add(ev, 0);
libusb_fds_events.insert(std::make_pair((*it)->fd, ev));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2954 次 |
| 最近记录: |