C++ 如何将libusb与libevent一起使用?

C++ 如何将libusb与libevent一起使用?,c++,c,libusb,libevent,libusb-1.0,C++,C,Libusb,Libevent,Libusb 1.0,我正在使用libevent编写一个事件驱动的应用程序,我需要使用libusb-1.0进行USB传输 我想使用获取文件描述符列表(在fds)并将它们添加到libevent中,如下所示: const struct libusb_pollfd **fds = libusb_get_pollfds(device->context); const struct libusb_pollfd *it = *fds; for(;it != NULL; ++it) { cout <<

我正在使用libevent编写一个事件驱动的应用程序,我需要使用libusb-1.0进行USB传输

我想使用获取文件描述符列表(在
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);
}
const struct libusb\u pollfd**fds=libusb\u get\u pollfds(设备->上下文);
常量结构libusb_pollfd*it=*fds;
for(;it!=NULL;++it){
cout-fd,ev);
}
免费(fds);
// (...)
//和回调函数:
void回调::libusb\u cb(evutil\u socket\t fd,short what,void*arg){
服务器*s=重新解释强制转换(arg);
libusb\u处理\u事件\u超时(s->device\u->context,0);
}
此外,我还使用从
libusb\u fds\u事件中添加/删除fds

问题是我在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));
}
const struct libusb_pollfd**it=fds;
对于(;*it!=NULL;++it){
cout-fd,ev);
}