Simplify error handling on socket creation
On any error, all previously opened 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
ef6a3b97a7
commit
5b2ec66222
1 changed files with 21 additions and 21 deletions
|
@ -58,8 +58,9 @@ public final class DesktopConnection implements Closeable {
|
||||||
public static DesktopConnection open(int scid, boolean tunnelForward, boolean control, boolean sendDummyByte) throws IOException {
|
public static DesktopConnection open(int scid, boolean tunnelForward, boolean control, boolean sendDummyByte) throws IOException {
|
||||||
String socketName = getSocketName(scid);
|
String socketName = getSocketName(scid);
|
||||||
|
|
||||||
LocalSocket videoSocket;
|
LocalSocket videoSocket = null;
|
||||||
LocalSocket controlSocket = null;
|
LocalSocket controlSocket = null;
|
||||||
|
try {
|
||||||
if (tunnelForward) {
|
if (tunnelForward) {
|
||||||
try (LocalServerSocket localServerSocket = new LocalServerSocket(socketName)) {
|
try (LocalServerSocket localServerSocket = new LocalServerSocket(socketName)) {
|
||||||
videoSocket = localServerSocket.accept();
|
videoSocket = localServerSocket.accept();
|
||||||
|
@ -68,25 +69,24 @@ public final class DesktopConnection implements Closeable {
|
||||||
videoSocket.getOutputStream().write(0);
|
videoSocket.getOutputStream().write(0);
|
||||||
}
|
}
|
||||||
if (control) {
|
if (control) {
|
||||||
try {
|
|
||||||
controlSocket = localServerSocket.accept();
|
controlSocket = localServerSocket.accept();
|
||||||
} catch (IOException | RuntimeException e) {
|
|
||||||
videoSocket.close();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
videoSocket = connect(socketName);
|
videoSocket = connect(socketName);
|
||||||
if (control) {
|
if (control) {
|
||||||
try {
|
|
||||||
controlSocket = connect(socketName);
|
controlSocket = connect(socketName);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (IOException | RuntimeException e) {
|
} catch (IOException | RuntimeException e) {
|
||||||
|
if (videoSocket != null) {
|
||||||
videoSocket.close();
|
videoSocket.close();
|
||||||
|
}
|
||||||
|
if (controlSocket != null) {
|
||||||
|
controlSocket.close();
|
||||||
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DesktopConnection(videoSocket, controlSocket);
|
return new DesktopConnection(videoSocket, controlSocket);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue