Skip to content

Commit

Permalink
Log controller handling errors
Browse files Browse the repository at this point in the history
On close, the controller is expected to throw an IOException because the
socket is closed, so the exception was ignored.

However, message handling actions may also throw IOException, and they
must not be silently ignored.

PR #4473 <#4473>
  • Loading branch information
rom1v committed Feb 29, 2024
1 parent 604e59a commit 4d5b67c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions server/src/main/java/com/genymobile/scrcpy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ private void control() throws IOException {
SystemClock.sleep(500);
}

while (!Thread.currentThread().isInterrupted()) {
handleEvent();
boolean alive = true;
while (!Thread.currentThread().isInterrupted() && alive) {
alive = handleEvent();
}
}

Expand All @@ -92,7 +93,7 @@ public void start(TerminationListener listener) {
try {
control();
} catch (IOException e) {
// this is expected on close
Ln.e("Controller error", e);
} finally {
Ln.d("Controller stopped");
listener.onTerminated(true);
Expand Down Expand Up @@ -122,8 +123,15 @@ public DeviceMessageSender getSender() {
return sender;
}

private void handleEvent() throws IOException {
ControlMessage msg = controlChannel.recv();
private boolean handleEvent() throws IOException {
ControlMessage msg;
try {
msg = controlChannel.recv();
} catch (IOException e) {
// this is expected on close
return false;
}

switch (msg.getType()) {
case ControlMessage.TYPE_INJECT_KEYCODE:
if (device.supportsInputEvents()) {
Expand Down Expand Up @@ -185,6 +193,8 @@ private void handleEvent() throws IOException {
default:
// do nothing
}

return true;
}

private boolean injectKeycode(int action, int keycode, int repeat, int metaState) {
Expand Down

0 comments on commit 4d5b67c

Please sign in to comment.