mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-04-05 18:01:56 +02:00
Make socket recover if slimevr crashed (#1604)
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package dev.slimevr.desktop.platform.linux;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
|
||||
public class SocketUtils {
|
||||
|
||||
static boolean isSocketInUse(String socketPath) {
|
||||
try (SocketChannel testChannel = SocketChannel.open(StandardProtocolFamily.UNIX)) {
|
||||
testChannel.connect(UnixDomainSocketAddress.of(socketPath));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,7 +46,16 @@ public class UnixSocketBridge extends SteamVRBridge implements AutoCloseable {
|
||||
|
||||
File socketFile = new File(socketPath);
|
||||
if (socketFile.exists()) {
|
||||
throw new RuntimeException(socketPath + " socket already exists.");
|
||||
if (SocketUtils.isSocketInUse(socketPath)) {
|
||||
throw new RuntimeException(
|
||||
socketPath + " socket is already in use by another process."
|
||||
);
|
||||
} else {
|
||||
LogManager.warning("[" + bridgeName + "] Cleaning up stale socket: " + socketPath);
|
||||
if (!socketFile.delete()) {
|
||||
throw new RuntimeException("Failed to delete stale socket: " + socketPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
socketFile.deleteOnExit();
|
||||
}
|
||||
|
||||
@@ -35,10 +35,22 @@ public class UnixSocketRpcBridge implements dev.slimevr.bridge.Bridge,
|
||||
) {
|
||||
this.socketPath = socketPath;
|
||||
this.protocolAPI = server.protocolAPI;
|
||||
|
||||
File socketFile = new File(socketPath);
|
||||
if (socketFile.exists())
|
||||
throw new RuntimeException(socketPath + " socket already exists.");
|
||||
if (socketFile.exists()) {
|
||||
if (SocketUtils.isSocketInUse(socketPath)) {
|
||||
throw new RuntimeException(
|
||||
socketPath + " socket is already in use by another process."
|
||||
);
|
||||
} else {
|
||||
LogManager.warning("[SolarXR Bridge] Cleaning up stale socket: " + socketPath);
|
||||
if (!socketFile.delete()) {
|
||||
throw new RuntimeException("Failed to delete stale socket: " + socketPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
socketFile.deleteOnExit();
|
||||
|
||||
try {
|
||||
socket = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
|
||||
selector = Selector.open();
|
||||
|
||||
Reference in New Issue
Block a user