过滤重复的PNP事件

This commit is contained in:
gb 2022-08-06 20:43:40 +08:00
parent 9babe4b2d3
commit 888d416618
1 changed files with 26 additions and 9 deletions

View File

@ -886,8 +886,8 @@ void usb_monitor::register_monitor_wnd(const wchar_t* cls)
void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
{
std::lock_guard<std::mutex> lock(lock_);
int ev = arrive ? LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED : LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
DEVID id = dev->id();
int ev = arrive ? LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED : LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
DEVID id = dev->id();
if (arrive)
{
@ -896,12 +896,20 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
{
if (devices_[i]->id() == id)
{
dev->release();
dev = devices_[i];
dev->add_ref();
dev->set_online(true);
found = true;
break;
if (devices_[i]->is_online())
{
VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "%s is already in device queue and received ARRIVE again, discard this event.\n", dev->name().c_str());
dev->release();
return;
}
else
{
dev = devices_[i];
dev->add_ref();
dev->set_online(true);
found = true;
break;
}
}
}
if (!found)
@ -922,7 +930,14 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
dev->add_ref();
if (dev->is_open())
{
dev->set_online(false);
if (dev->is_online())
dev->set_online(false);
else
{
VLOG_MINI_1(LOG_LEVEL_DEBUG_INFO, "%s is already offline and received LEAVE again, discard this event.\n", dev->name().c_str());
dev->release();
return;
}
}
else
{
@ -953,6 +968,8 @@ int usb_monitor::on_usb_pnp(WPARAM wp, LPARAM lp)
if (wp == DBT_DEVICEQUERYREMOVE)
return cur_dev_name_ != u2utf8(dev->dbcc_name);
VLOG_MINI_2(LOG_LEVEL_DEBUG_INFO, "event '%08x' of device %s\n", wp, u2utf8(dev->dbcc_name).c_str());
usb_device* ud = new usb_device(u2utf8(dev->dbcc_name).c_str());
*ud = dev->dbcc_classguid;
if (!PostThreadMessageW(handle_msg_id_, MSG_DEVICE_PNP, wp == DBT_DEVICEARRIVAL, (LPARAM)ud))