mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-04-22 04:25:01 +00:00
Simplify error handling on socket creation
On any error, all previously closed sockets must be closed. Handle these errors in a single catch-block. Currently, there are only 2 sockets, but this will simplify even more with more sockets. Note: this commit is better displayed with --ignore-space-change (-b).
This commit is contained in:
parent
30b8d140e8
commit
8cdae30b01
1 changed files with 21 additions and 21 deletions
|
@ -58,34 +58,34 @@ public final class DesktopConnection implements Closeable {
|
|||
public static DesktopConnection open(int uid, boolean tunnelForward, boolean control, boolean sendDummyByte) throws IOException {
|
||||
String socketName = getSocketName(uid);
|
||||
|
||||
LocalSocket videoSocket;
|
||||
LocalSocket videoSocket = null;
|
||||
LocalSocket controlSocket = null;
|
||||
if (tunnelForward) {
|
||||
try (LocalServerSocket localServerSocket = new LocalServerSocket(socketName)) {
|
||||
videoSocket = localServerSocket.accept();
|
||||
if (sendDummyByte) {
|
||||
// send one byte so the client may read() to detect a connection error
|
||||
videoSocket.getOutputStream().write(0);
|
||||
}
|
||||
if (control) {
|
||||
try {
|
||||
try {
|
||||
if (tunnelForward) {
|
||||
try (LocalServerSocket localServerSocket = new LocalServerSocket(socketName)) {
|
||||
videoSocket = localServerSocket.accept();
|
||||
if (sendDummyByte) {
|
||||
// send one byte so the client may read() to detect a connection error
|
||||
videoSocket.getOutputStream().write(0);
|
||||
}
|
||||
if (control) {
|
||||
controlSocket = localServerSocket.accept();
|
||||
} catch (IOException | RuntimeException e) {
|
||||
videoSocket.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
videoSocket = connect(socketName);
|
||||
if (control) {
|
||||
try {
|
||||
} else {
|
||||
videoSocket = connect(socketName);
|
||||
if (control) {
|
||||
controlSocket = connect(socketName);
|
||||
} catch (IOException | RuntimeException e) {
|
||||
videoSocket.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} catch (IOException | RuntimeException e) {
|
||||
if (videoSocket != null) {
|
||||
videoSocket.close();
|
||||
}
|
||||
if (controlSocket != null) {
|
||||
controlSocket.close();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
return new DesktopConnection(videoSocket, controlSocket);
|
||||
|
|
Loading…
Add table
Reference in a new issue