mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
SolarXR IPC Socket (#1247)
This commit is contained in:
1 parent
b4df1d1444
commit
fcd82324a9
5 files changed
+334
-94
No files matched your search
@@ -5,10 +5,11 @@ package dev.slimevr.desktop
|
||||
import dev.slimevr.Keybinding
|
||||
import dev.slimevr.SLIMEVR_IDENTIFIER
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.bridge.ISteamVRBridge
|
||||
import dev.slimevr.bridge.Bridge
|
||||
import dev.slimevr.desktop.firmware.DesktopSerialFlashingHandler
|
||||
import dev.slimevr.desktop.platform.SteamVRBridge
|
||||
import dev.slimevr.desktop.platform.linux.UnixSocketBridge
|
||||
import dev.slimevr.desktop.platform.linux.UnixSocketRpcBridge
|
||||
import dev.slimevr.desktop.platform.windows.WindowsNamedPipeBridge
|
||||
import dev.slimevr.desktop.serial.DesktopSerialHandler
|
||||
import dev.slimevr.desktop.tracking.trackers.hid.TrackersHID
|
||||
@@ -119,8 +120,7 @@ fun main(args: Array<String>) {
|
||||
val configDir = resolveConfig()
|
||||
LogManager.info("Using config dir: $configDir")
|
||||
val vrServer = VRServer(
|
||||
::provideSteamVRBridge,
|
||||
::provideFeederBridge,
|
||||
::provideBridges,
|
||||
{ _ -> DesktopSerialHandler() },
|
||||
{ _ -> DesktopSerialFlashingHandler() },
|
||||
configPath = configDir,
|
||||
@@ -151,90 +151,89 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
fun provideSteamVRBridge(
|
||||
fun provideBridges(
|
||||
server: VRServer,
|
||||
computedTrackers: List<Tracker>,
|
||||
): ISteamVRBridge? {
|
||||
val driverBridge: SteamVRBridge?
|
||||
if (OperatingSystem.currentPlatform == OperatingSystem.WINDOWS) {
|
||||
// Create named pipe bridge for SteamVR driver
|
||||
driverBridge = WindowsNamedPipeBridge(
|
||||
server,
|
||||
"steamvr",
|
||||
"SteamVR Driver Bridge",
|
||||
"""\\.\pipe\SlimeVRDriver""",
|
||||
computedTrackers,
|
||||
)
|
||||
} else if (OperatingSystem.currentPlatform == OperatingSystem.LINUX) {
|
||||
var linuxBridge: SteamVRBridge? = null
|
||||
try {
|
||||
linuxBridge = UnixSocketBridge(
|
||||
server,
|
||||
"steamvr",
|
||||
"SteamVR Driver Bridge",
|
||||
Paths.get(OperatingSystem.socketDirectory, "SlimeVRDriver")
|
||||
.toString(),
|
||||
computedTrackers,
|
||||
)
|
||||
} catch (ex: Exception) {
|
||||
LogManager.severe(
|
||||
"Failed to initiate Unix socket, disabling driver bridge...",
|
||||
ex,
|
||||
)
|
||||
}
|
||||
driverBridge = linuxBridge
|
||||
if (driverBridge != null) {
|
||||
// Close the named socket on shutdown, or otherwise it's not going to get removed
|
||||
Runtime.getRuntime().addShutdownHook(
|
||||
Thread {
|
||||
try {
|
||||
(driverBridge as? UnixSocketBridge)?.close()
|
||||
} catch (e: Exception) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
} else {
|
||||
driverBridge = null
|
||||
}
|
||||
|
||||
return driverBridge
|
||||
}
|
||||
|
||||
fun provideFeederBridge(
|
||||
server: VRServer,
|
||||
): ISteamVRBridge? {
|
||||
val feederBridge: SteamVRBridge?
|
||||
): Sequence<Bridge> = sequence {
|
||||
when (OperatingSystem.currentPlatform) {
|
||||
OperatingSystem.WINDOWS -> {
|
||||
// Create named pipe bridge for SteamVR driver
|
||||
yield(
|
||||
WindowsNamedPipeBridge(
|
||||
server,
|
||||
"steamvr",
|
||||
"SteamVR Driver Bridge",
|
||||
"""\\.\pipe\SlimeVRDriver""",
|
||||
computedTrackers,
|
||||
),
|
||||
)
|
||||
|
||||
// Create named pipe bridge for SteamVR input
|
||||
feederBridge = WindowsNamedPipeBridge(
|
||||
server,
|
||||
"steamvr_feeder",
|
||||
"SteamVR Feeder Bridge",
|
||||
"""\\.\pipe\SlimeVRInput""",
|
||||
FastList(),
|
||||
yield(
|
||||
WindowsNamedPipeBridge(
|
||||
server,
|
||||
"steamvr_feeder",
|
||||
"SteamVR Feeder Bridge",
|
||||
"""\\.\pipe\SlimeVRInput""",
|
||||
FastList(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
OperatingSystem.LINUX -> {
|
||||
feederBridge = UnixSocketBridge(
|
||||
server,
|
||||
"steamvr_feeder",
|
||||
"SteamVR Feeder Bridge",
|
||||
Paths.get(OperatingSystem.socketDirectory, "SlimeVRInput")
|
||||
.toString(),
|
||||
FastList(),
|
||||
var linuxBridge: SteamVRBridge? = null
|
||||
try {
|
||||
linuxBridge = UnixSocketBridge(
|
||||
server,
|
||||
"steamvr",
|
||||
"SteamVR Driver Bridge",
|
||||
Paths.get(OperatingSystem.socketDirectory, "SlimeVRDriver")
|
||||
.toString(),
|
||||
computedTrackers,
|
||||
)
|
||||
} catch (ex: Exception) {
|
||||
LogManager.severe(
|
||||
"Failed to initiate Unix socket, disabling driver bridge...",
|
||||
ex,
|
||||
)
|
||||
}
|
||||
if (linuxBridge != null) {
|
||||
// Close the named socket on shutdown, or otherwise it's not going to get removed
|
||||
Runtime.getRuntime().addShutdownHook(
|
||||
Thread {
|
||||
try {
|
||||
(linuxBridge as? UnixSocketBridge)?.close()
|
||||
} catch (e: Exception) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
},
|
||||
)
|
||||
yield(linuxBridge)
|
||||
}
|
||||
|
||||
yield(
|
||||
UnixSocketBridge(
|
||||
server,
|
||||
"steamvr_feeder",
|
||||
"SteamVR Feeder Bridge",
|
||||
Paths.get(OperatingSystem.socketDirectory, "SlimeVRInput")
|
||||
.toString(),
|
||||
FastList(),
|
||||
),
|
||||
)
|
||||
|
||||
yield(
|
||||
UnixSocketRpcBridge(
|
||||
server,
|
||||
Paths.get(OperatingSystem.socketDirectory, "SlimeVRRpc")
|
||||
.toString(),
|
||||
computedTrackers,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
feederBridge = null
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
|
||||
return feederBridge
|
||||
}
|
||||
|
||||
const val CONFIG_FILENAME = "vrconfig.yml"
|
||||
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package dev.slimevr.desktop.platform.linux;
|
||||
|
||||
import dev.slimevr.protocol.ConnectionContext;
|
||||
import dev.slimevr.protocol.GenericConnection;
|
||||
import io.eiren.util.logging.LogManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class UnixSocketConnection implements GenericConnection {
|
||||
public final UUID id;
|
||||
public final ConnectionContext context;
|
||||
private final ByteBuffer dst = ByteBuffer.allocate(2048).order(ByteOrder.LITTLE_ENDIAN);
|
||||
private final SocketChannel channel;
|
||||
private int remainingBytes;
|
||||
|
||||
public UnixSocketConnection(SocketChannel channel) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.context = new ConnectionContext();
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getConnectionId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConnectionContext getContext() {
|
||||
return this.context;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return this.channel.isConnected();
|
||||
}
|
||||
|
||||
private void resetChannel() {
|
||||
try {
|
||||
this.channel.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(ByteBuffer bytes) {
|
||||
if (!this.channel.isConnected())
|
||||
return;
|
||||
try {
|
||||
ByteBuffer[] src = new ByteBuffer[] {
|
||||
ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN),
|
||||
bytes.slice(),
|
||||
};
|
||||
src[0].putInt(src[1].remaining() + 4);
|
||||
src[0].flip();
|
||||
synchronized (this) {
|
||||
while (src[1].hasRemaining()) {
|
||||
this.channel.write(src);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ByteBuffer read() {
|
||||
if (dst.position() < 4 || dst.position() < dst.getInt(0)) {
|
||||
if (!this.channel.isConnected())
|
||||
return null;
|
||||
try {
|
||||
int result = this.channel.read(dst);
|
||||
if (result == -1) {
|
||||
LogManager.info("[SolarXR Bridge] Reached end-of-stream on connection");
|
||||
this.resetChannel();
|
||||
return null;
|
||||
}
|
||||
if (dst.position() < 4) {
|
||||
return null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
this.resetChannel();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
int messageLength = dst.getInt(0);
|
||||
if (messageLength > 1024) {
|
||||
LogManager
|
||||
.severe(
|
||||
"[SolarXR Bridge] Buffer overflow on socket. Message length: " + messageLength
|
||||
);
|
||||
this.resetChannel();
|
||||
return null;
|
||||
}
|
||||
if (dst.position() < messageLength) {
|
||||
return null;
|
||||
}
|
||||
remainingBytes = dst.position() - messageLength;
|
||||
dst.position(4);
|
||||
dst.limit(messageLength);
|
||||
return dst;
|
||||
}
|
||||
|
||||
public void next() {
|
||||
dst.position(dst.limit());
|
||||
dst.limit(dst.limit() + remainingBytes);
|
||||
dst.compact();
|
||||
dst.limit(dst.capacity());
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package dev.slimevr.desktop.platform.linux;
|
||||
|
||||
import dev.slimevr.bridge.BridgeThread;
|
||||
import dev.slimevr.protocol.GenericConnection;
|
||||
import dev.slimevr.protocol.ProtocolAPI;
|
||||
import dev.slimevr.tracking.trackers.Tracker;
|
||||
import dev.slimevr.util.ann.VRServerThread;
|
||||
import dev.slimevr.VRServer;
|
||||
import io.eiren.util.logging.LogManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.StandardProtocolFamily;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class UnixSocketRpcBridge implements dev.slimevr.bridge.Bridge,
|
||||
dev.slimevr.protocol.ProtocolAPIServer, Runnable, AutoCloseable {
|
||||
private final Thread runnerThread = new Thread(this, "Named socket thread");
|
||||
private final String socketPath;
|
||||
private final ProtocolAPI protocolAPI;
|
||||
private final ServerSocketChannel socket;
|
||||
private final Selector selector;
|
||||
|
||||
public UnixSocketRpcBridge(
|
||||
VRServer server,
|
||||
String socketPath,
|
||||
List<Tracker> shareableTrackers
|
||||
) {
|
||||
this.socketPath = socketPath;
|
||||
this.protocolAPI = server.protocolAPI;
|
||||
File socketFile = new File(socketPath);
|
||||
if (socketFile.exists())
|
||||
throw new RuntimeException(socketPath + " socket already exists.");
|
||||
socketFile.deleteOnExit();
|
||||
try {
|
||||
socket = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
|
||||
selector = Selector.open();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Socket open failed.");
|
||||
}
|
||||
|
||||
server.protocolAPI.registerAPIServer(this);
|
||||
}
|
||||
|
||||
@VRServerThread
|
||||
private void disconnected() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@VRServerThread
|
||||
public void dataRead() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@VRServerThread
|
||||
public void dataWrite() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@VRServerThread
|
||||
public void addSharedTracker(Tracker tracker) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@VRServerThread
|
||||
public void removeSharedTracker(Tracker tracker) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@VRServerThread
|
||||
public void startBridge() {
|
||||
this.runnerThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
@BridgeThread
|
||||
public void run() {
|
||||
try {
|
||||
this.socket.bind(UnixDomainSocketAddress.of(this.socketPath));
|
||||
this.socket.configureBlocking(false);
|
||||
this.socket.register(this.selector, SelectionKey.OP_ACCEPT);
|
||||
LogManager.info("[SolarXR Bridge] Socket " + this.socketPath + " created");
|
||||
while (this.socket.isOpen()) {
|
||||
this.selector.select(0);
|
||||
for (SelectionKey key : this.selector.selectedKeys()) {
|
||||
UnixSocketConnection conn = (UnixSocketConnection) key.attachment();
|
||||
if (conn != null) {
|
||||
for (ByteBuffer message; (message = conn.read()) != null; conn.next())
|
||||
this.protocolAPI.onMessage(conn, message);
|
||||
} else
|
||||
for (SocketChannel channel; (channel = socket.accept()) != null;) {
|
||||
channel.configureBlocking(false);
|
||||
channel
|
||||
.register(
|
||||
this.selector,
|
||||
SelectionKey.OP_READ,
|
||||
new UnixSocketConnection(channel)
|
||||
);
|
||||
LogManager
|
||||
.info(
|
||||
"[SolarXR Bridge] Connected to "
|
||||
+ channel.getRemoteAddress().toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
this.socket.close();
|
||||
this.selector.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return this.selector.keys().stream().anyMatch(key -> key.attachment() != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public java.util.stream.Stream<GenericConnection> getAPIConnections() {
|
||||
return this.selector
|
||||
.keys()
|
||||
.stream()
|
||||
.map(key -> (GenericConnection) key.attachment())
|
||||
.filter(conn -> conn != null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user