@@ -440,13 +440,17 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
440
440
441
441
UsbDevice dev = getDevice (msg .devId );
442
442
if (dev == null ) {
443
- sendReply (s , reply , ProtoDefs .ST_NA );
443
+ // The device is gone, so terminate the client
444
+ cleanupDetachedDevice (msg .devId );
445
+ server .killClient (s );
444
446
return ;
445
447
}
446
448
447
449
AttachedDeviceContext context = connections .get (msg .devId );
448
450
if (context == null ) {
449
- sendReply (s , reply , ProtoDefs .ST_NA );
451
+ // This should never happen, but kill the connection if it does
452
+ cleanupDetachedDevice (msg .devId );
453
+ server .killClient (s );
450
454
return ;
451
455
}
452
456
@@ -609,6 +613,7 @@ public boolean attachToDevice(String busId) {
609
613
// Create a context for this attachment
610
614
AttachedDeviceContext context = new AttachedDeviceContext ();
611
615
context .devConn = devConn ;
616
+ context .device = dev ;
612
617
613
618
// Count all endpoints on all intefaces
614
619
int endpointCount = 0 ;
@@ -626,40 +631,45 @@ public boolean attachToDevice(String busId) {
626
631
updateNotification ();
627
632
return true ;
628
633
}
629
-
630
- @ Override
631
- public void detachFromDevice (String busId ) {
632
- UsbDevice dev = getDevice (busId );
633
- if (dev == null ) {
634
- return ;
635
- }
636
-
637
- AttachedDeviceContext context = connections .get (dev .getDeviceId ());
634
+
635
+ private void cleanupDetachedDevice (int deviceId ) {
636
+ AttachedDeviceContext context = connections .get (deviceId );
638
637
if (context == null ) {
639
638
return ;
640
639
}
641
640
642
641
// Clear the this attachment's context
643
- connections .remove (dev . getDeviceId () );
642
+ connections .remove (deviceId );
644
643
645
644
// Release our claim to the interfaces
646
- for (int i = 0 ; i < dev .getInterfaceCount (); i ++) {
647
- context .devConn .releaseInterface (dev .getInterface (i ));
645
+ for (int i = 0 ; i < context . device .getInterfaceCount (); i ++) {
646
+ context .devConn .releaseInterface (context . device .getInterface (i ));
648
647
}
649
-
648
+
650
649
// Close the connection
651
650
context .devConn .close ();
652
-
651
+
653
652
updateNotification ();
654
653
}
655
654
655
+ @ Override
656
+ public void detachFromDevice (String busId ) {
657
+ UsbDevice dev = getDevice (busId );
658
+ if (dev == null ) {
659
+ return ;
660
+ }
661
+
662
+ cleanupDetachedDevice (dev .getDeviceId ());
663
+ }
664
+
656
665
class UrbContext {
657
666
public UsbIpSubmitUrb originalMsg ;
658
667
public UsbIpSubmitUrbReply replyMsg ;
659
668
public ByteBuffer buffer ;
660
669
}
661
670
662
671
class AttachedDeviceContext {
672
+ public UsbDevice device ;
663
673
public UsbDeviceConnection devConn ;
664
674
public ThreadPoolExecutor requestPool ;
665
675
}
0 commit comments