Skip to content

Commit b6c3164

Browse files
committed
Get device lookup out of the hot URB submission path
1 parent d5e9d8d commit b6c3164

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

app/src/main/java/org/cgutman/usbip/service/UsbIpService.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ private static void sendReply(Socket s, UsbIpUnlinkUrbReply reply, int status) {
444444

445445
// FIXME: This dispatching could use some refactoring so we don't have to pass
446446
// a million parameters to this guy
447-
private void dispatchRequest(final AttachedDeviceContext context, final Socket s,
447+
private void dispatchRequest(final AttachedDeviceContext context, final int deviceId, final Socket s,
448448
final UsbEndpoint selectedEndpoint, final ByteBuffer buff, final UsbIpSubmitUrb msg) {
449449
context.requestPool.submit(new Runnable() {
450450
@Override
@@ -485,6 +485,14 @@ public void run() {
485485
}
486486

487487
if (res < 0) {
488+
// If the request failed, let's see if the device is still around
489+
UsbDevice dev = getDevice(deviceId);
490+
if (dev == null) {
491+
// The device is gone, so terminate the client
492+
server.killClient(s);
493+
return;
494+
}
495+
488496
reply.status = res;
489497
}
490498
else {
@@ -524,6 +532,14 @@ else if (selectedEndpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
524532

525533
if (res < 0) {
526534
reply.status = res;
535+
536+
// If the request failed, let's see if the device is still around
537+
UsbDevice dev = getDevice(deviceId);
538+
if (dev == null) {
539+
// The device is gone, so terminate the client
540+
server.killClient(s);
541+
return;
542+
}
527543
}
528544
else {
529545
reply.actualLength = res;
@@ -549,13 +565,6 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
549565

550566
int deviceId = devIdToDeviceId(msg.devId);
551567

552-
UsbDevice dev = getDevice(deviceId);
553-
if (dev == null) {
554-
// The device is gone, so terminate the client
555-
server.killClient(s);
556-
return;
557-
}
558-
559568
AttachedDeviceContext context = connections.get(deviceId);
560569
if (context == null) {
561570
// This should never happen, but kill the connection if it does
@@ -609,6 +618,14 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
609618
}
610619

611620
if (res < 0) {
621+
// If the request failed, let's see if the device is still around
622+
UsbDevice dev = getDevice(deviceId);
623+
if (dev == null) {
624+
// The device is gone, so terminate the client
625+
server.killClient(s);
626+
return;
627+
}
628+
612629
reply.status = res;
613630
}
614631
else {
@@ -618,7 +635,6 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
618635

619636
context.activeMessages.remove(msg);
620637
sendReply(s, reply, reply.status);
621-
return;
622638
}
623639
else {
624640
// Find the correct endpoint
@@ -651,7 +667,7 @@ public void submitUrbRequest(Socket s, UsbIpSubmitUrb msg) {
651667
context.activeMessages.add(msg);
652668

653669
// Dispatch this request asynchronously
654-
dispatchRequest(context, s, selectedEndpoint, buff, msg);
670+
dispatchRequest(context, deviceId, s, selectedEndpoint, buff, msg);
655671
}
656672
}
657673

0 commit comments

Comments
 (0)