过滤重复的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

@ -896,7 +896,14 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
{ {
if (devices_[i]->id() == id) if (devices_[i]->id() == id)
{ {
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(); dev->release();
return;
}
else
{
dev = devices_[i]; dev = devices_[i];
dev->add_ref(); dev->add_ref();
dev->set_online(true); dev->set_online(true);
@ -904,6 +911,7 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
break; break;
} }
} }
}
if (!found) if (!found)
{ {
devices_.push_back(dev); devices_.push_back(dev);
@ -922,7 +930,14 @@ void usb_monitor::notify_usb_event(usb_device*& dev, bool arrive)
dev->add_ref(); dev->add_ref();
if (dev->is_open()) if (dev->is_open())
{ {
if (dev->is_online())
dev->set_online(false); 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 else
{ {
@ -953,6 +968,8 @@ int usb_monitor::on_usb_pnp(WPARAM wp, LPARAM lp)
if (wp == DBT_DEVICEQUERYREMOVE) if (wp == DBT_DEVICEQUERYREMOVE)
return cur_dev_name_ != u2utf8(dev->dbcc_name); 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()); usb_device* ud = new usb_device(u2utf8(dev->dbcc_name).c_str());
*ud = dev->dbcc_classguid; *ud = dev->dbcc_classguid;
if (!PostThreadMessageW(handle_msg_id_, MSG_DEVICE_PNP, wp == DBT_DEVICEARRIVAL, (LPARAM)ud)) if (!PostThreadMessageW(handle_msg_id_, MSG_DEVICE_PNP, wp == DBT_DEVICEARRIVAL, (LPARAM)ud))