mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-24 11:01:20 -08:00
Driver status and enable
This commit is contained in:
1 parent
aa3ab3942a
commit
bbcfc99061
18 files changed
+317
-81
No files matched your search
@@ -114,7 +114,7 @@ fun main(args: Array<String>) = runBlocking<Unit>(appCoroutineExceptionHandler +
|
||||
val heightCalibrationManager = HeightCalibrationManager.create(ctx = phase1, scope = this)
|
||||
val trackingChecklist = TrackingChecklist.create(scope = this, extraBehaviours = { appContext ->
|
||||
buildList {
|
||||
add(SteamVRCheckBehaviour(appContext.server))
|
||||
add(SteamVRCheckBehaviour(appContext.server, appContext.config.settings))
|
||||
}
|
||||
})
|
||||
val udpServer = UdpServer.create(scope = this, addressResolver = ::resolveDesktopUdpAddress)
|
||||
|
||||
@@ -4,6 +4,9 @@ import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.CURRENT_PLATFORM
|
||||
import dev.slimevr.Platform
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
const val DRIVER_SOCKET_NAME = "SlimeVRDriver"
|
||||
@@ -15,25 +18,38 @@ const val FEEDER_PIPE = "\\\\.\\pipe\\SlimeVRInput"
|
||||
const val SOLARXR_PIPE = "\\\\.\\pipe\\SlimeVRRpc"
|
||||
|
||||
suspend fun createIpcServers(appContext: AppContextProvider) = coroutineScope {
|
||||
val driver = appContext.featureFlags.supportsDriver
|
||||
if (appContext.featureFlags.supportsDriver) {
|
||||
launch { createDriverServers(appContext) }
|
||||
}
|
||||
|
||||
when (CURRENT_PLATFORM) {
|
||||
Platform.LINUX, Platform.OSX -> {
|
||||
if (driver) {
|
||||
launch { createUnixDriverSocket(appContext) }
|
||||
launch { createUnixFeederSocket(appContext) }
|
||||
}
|
||||
launch { createUnixSolarXRSocket(appContext) }
|
||||
}
|
||||
|
||||
Platform.WINDOWS -> {
|
||||
if (driver) {
|
||||
launch { createWindowsDriverPipe(appContext) }
|
||||
launch { createWindowsFeederPipe(appContext) }
|
||||
}
|
||||
launch { createWindowsSolarXRPipe(appContext) }
|
||||
}
|
||||
|
||||
Platform.LINUX, Platform.OSX -> launch { createUnixSolarXRSocket(appContext) }
|
||||
Platform.WINDOWS -> launch { createWindowsSolarXRPipe(appContext) }
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun createDriverServers(appContext: AppContextProvider) {
|
||||
appContext.config.settings.context.state
|
||||
.map { it.data.driverConfig.enabled }
|
||||
.distinctUntilChanged()
|
||||
.collectLatest { enabled ->
|
||||
if (!enabled) return@collectLatest
|
||||
|
||||
coroutineScope {
|
||||
when (CURRENT_PLATFORM) {
|
||||
Platform.LINUX, Platform.OSX -> {
|
||||
launch { createUnixDriverSocket(appContext) }
|
||||
launch { createUnixFeederSocket(appContext) }
|
||||
}
|
||||
|
||||
Platform.WINDOWS -> {
|
||||
launch { createWindowsDriverPipe(appContext) }
|
||||
launch { createWindowsFeederPipe(appContext) }
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.slimevr.desktop.ipc
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.getSocketDirectory
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import kotlinx.coroutines.CancellationException
|
||||
@@ -9,6 +10,7 @@ import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.IOException
|
||||
@@ -17,6 +19,7 @@ import java.net.StandardProtocolFamily
|
||||
import java.net.UnixDomainSocketAddress
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.nio.channels.ClosedByInterruptException
|
||||
import java.nio.channels.ServerSocketChannel
|
||||
import java.nio.channels.SocketChannel
|
||||
import kotlin.io.path.Path
|
||||
@@ -24,6 +27,7 @@ import kotlin.io.path.Path
|
||||
suspend fun createUnixDriverSocket(appContext: AppContextProvider) = acceptUnixClients(DRIVER_SOCKET_NAME) { channel ->
|
||||
handleDriverConnection(
|
||||
appContext = appContext,
|
||||
source = DriverBridgeSource.DRIVER,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
@@ -32,6 +36,7 @@ suspend fun createUnixDriverSocket(appContext: AppContextProvider) = acceptUnixC
|
||||
suspend fun createUnixFeederSocket(appContext: AppContextProvider) = acceptUnixClients(FEEDER_SOCKET_NAME) { channel ->
|
||||
handleDriverConnection(
|
||||
appContext = appContext,
|
||||
source = DriverBridgeSource.FEEDER,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
@@ -60,17 +65,19 @@ private fun readFramedMessages(channel: SocketChannel) = flow {
|
||||
try {
|
||||
while (true) {
|
||||
lenBuf.clear()
|
||||
if (channel.read(lenBuf) == -1) break
|
||||
if (runInterruptible { channel.read(lenBuf) } == -1) break
|
||||
lenBuf.flip()
|
||||
|
||||
val dataBuf = ByteBuffer.allocate(lenBuf.int - 4)
|
||||
while (dataBuf.hasRemaining()) {
|
||||
if (channel.read(dataBuf) == -1) break
|
||||
if (runInterruptible { channel.read(dataBuf) } == -1) break
|
||||
}
|
||||
emit(dataBuf.array())
|
||||
}
|
||||
} catch (e: SocketException) {
|
||||
AppLogger.ipc.warn("Exception on socket: ${e.message}")
|
||||
} catch (e: ClosedByInterruptException) {
|
||||
AppLogger.ipc.info("Socket read interrupted, dropping connection")
|
||||
}
|
||||
}.flowOn(Dispatchers.IO)
|
||||
|
||||
@@ -98,7 +105,11 @@ private suspend fun acceptUnixClients(
|
||||
server.bind(UnixDomainSocketAddress.of(path))
|
||||
supervisorScope {
|
||||
while (isActive) {
|
||||
val client = server.accept()
|
||||
val client = try {
|
||||
runInterruptible { server.accept() }
|
||||
} catch (e: ClosedByInterruptException) {
|
||||
break
|
||||
}
|
||||
AppLogger.ipc.info("$name client connected")
|
||||
launch {
|
||||
try {
|
||||
|
||||
@@ -9,6 +9,9 @@ import com.sun.jna.platform.win32.WinError
|
||||
import com.sun.jna.platform.win32.WinNT
|
||||
import com.sun.jna.win32.StdCallLibrary
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.isActive
|
||||
import java.io.IOException
|
||||
|
||||
private val k32 = Kernel32.INSTANCE
|
||||
@@ -16,6 +19,9 @@ private val k32 = Kernel32.INSTANCE
|
||||
private const val HEADER_SIZE = 4
|
||||
private const val MAX_FRAME_SIZE = 256 * 1024
|
||||
|
||||
private const val WAIT_SLICE_MILLIS = 200
|
||||
private const val WAIT_TIMEOUT = 0x102
|
||||
|
||||
@Suppress("FunctionName")
|
||||
private object Kernel32IO : StdCallLibrary {
|
||||
init {
|
||||
@@ -29,6 +35,8 @@ private object Kernel32IO : StdCallLibrary {
|
||||
external fun GetOverlappedResult(handle: Pointer, overlapped: Pointer, moved: Pointer, wait: Boolean): Boolean
|
||||
|
||||
external fun WaitForSingleObject(handle: Pointer, millis: Int): Int
|
||||
|
||||
external fun CancelIoEx(handle: Pointer, overlapped: Pointer?): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +64,7 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
private val overlappedPointer = overlapped.pointer
|
||||
private val transferred = Memory(4)
|
||||
|
||||
fun awaitClient(): Boolean {
|
||||
suspend fun awaitClient(): Boolean {
|
||||
// On an overlapped handle ConnectNamedPipe returns straight away, so we do the waiting
|
||||
val connected = k32.ConnectNamedPipe(handle, overlapped)
|
||||
val err = Native.getLastError()
|
||||
@@ -122,8 +130,14 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
return awaitCompletion()
|
||||
}
|
||||
|
||||
private fun awaitCompletion(): Int {
|
||||
Kernel32IO.WaitForSingleObject(eventPointer, WinBase.INFINITE)
|
||||
private suspend fun awaitCompletion(): Int {
|
||||
while (Kernel32IO.WaitForSingleObject(eventPointer, WAIT_SLICE_MILLIS) == WAIT_TIMEOUT) {
|
||||
if (currentCoroutineContext().isActive) continue
|
||||
|
||||
Kernel32IO.CancelIoEx(handlePointer, overlappedPointer)
|
||||
Kernel32IO.GetOverlappedResult(handlePointer, overlappedPointer, transferred, true)
|
||||
currentCoroutineContext().ensureActive()
|
||||
}
|
||||
if (!Kernel32IO.GetOverlappedResult(handlePointer, overlappedPointer, transferred, true)) return -1
|
||||
return transferred.getInt(0)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import dev.slimevr.desktop.platform.TrackerStatus
|
||||
import dev.slimevr.desktop.platform.Version
|
||||
import dev.slimevr.driver.DriverBridge
|
||||
import dev.slimevr.driver.DriverBridgeInbound
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.driver.DriverBridgeOutbound
|
||||
import dev.slimevr.driver.TrackerRole
|
||||
import dev.slimevr.driver.bodyPartToRole
|
||||
@@ -93,6 +94,7 @@ suspend fun startBindingProvider() = withContext(Dispatchers.IO) {
|
||||
|
||||
suspend fun handleDriverConnection(
|
||||
appContext: AppContextProvider,
|
||||
source: DriverBridgeSource,
|
||||
messages: Flow<ByteArray>,
|
||||
send: suspend (ByteArray) -> Unit,
|
||||
) = coroutineScope {
|
||||
@@ -108,6 +110,7 @@ suspend fun handleDriverConnection(
|
||||
|
||||
val bridge = DriverBridge.create(
|
||||
id = appContext.server.nextHandle(),
|
||||
source = source,
|
||||
appContext = appContext,
|
||||
scope = this,
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.sun.jna.platform.win32.Kernel32
|
||||
import com.sun.jna.platform.win32.WinBase
|
||||
import com.sun.jna.platform.win32.WinNT
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -25,11 +26,11 @@ private const val PIPE_BUFFER_SIZE = 64 * 1024
|
||||
private typealias ConnectionHandler = suspend (messages: Flow<ByteArray>, send: suspend (ByteArray) -> Unit) -> Unit
|
||||
|
||||
suspend fun createWindowsDriverPipe(appContext: AppContextProvider) = acceptWindowsClients(DRIVER_PIPE) { messages, send ->
|
||||
handleDriverConnection(appContext, messages, send)
|
||||
handleDriverConnection(appContext, DriverBridgeSource.DRIVER, messages, send)
|
||||
}
|
||||
|
||||
suspend fun createWindowsFeederPipe(appContext: AppContextProvider) = acceptWindowsClients(FEEDER_PIPE) { messages, send ->
|
||||
handleDriverConnection(appContext, messages, send)
|
||||
handleDriverConnection(appContext, DriverBridgeSource.FEEDER, messages, send)
|
||||
}
|
||||
|
||||
suspend fun createWindowsSolarXRPipe(appContext: AppContextProvider) = acceptWindowsClients(SOLARXR_PIPE) { messages, send ->
|
||||
@@ -75,7 +76,16 @@ private suspend fun acceptWindowsClients(
|
||||
while (isActive) {
|
||||
val pipe = createSecurePipe(pipeName)
|
||||
val writer = PipeSlot(pipe)
|
||||
if (!writer.awaitClient()) {
|
||||
val connected = try {
|
||||
writer.awaitClient()
|
||||
} catch (e: CancellationException) {
|
||||
// The pipe goes down with the driver output, so the one waiting for a client has to
|
||||
// be released here: no handler owns it yet
|
||||
writer.close()
|
||||
k32.CloseHandle(pipe)
|
||||
throw e
|
||||
}
|
||||
if (!connected) {
|
||||
writer.close()
|
||||
k32.CloseHandle(pipe)
|
||||
continue
|
||||
|
||||
@@ -9,7 +9,9 @@ import com.sun.jna.platform.win32.WinDef
|
||||
import dev.slimevr.CURRENT_PLATFORM
|
||||
import dev.slimevr.Platform
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.desktop.getSteamVRDriversList
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import dev.slimevr.trackingchecklist.TrackingChecklist
|
||||
import dev.slimevr.trackingchecklist.TrackingChecklistActions
|
||||
@@ -20,6 +22,7 @@ import io.ktor.client.engine.cio.CIO
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
@@ -92,7 +95,7 @@ private fun Throwable.isConnectionRefused(): Boolean = generateSequence(this, Th
|
||||
|
||||
data class SteamVRDriverState(val connected: Boolean = false, val installed: Boolean = true, val blocked: Boolean = false, val enabled: Boolean = true)
|
||||
|
||||
class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBehaviourType {
|
||||
class SteamVRCheckBehaviour(private val server: VRServer, private val settings: Settings) : TrackingChecklistBehaviourType {
|
||||
private val steamVRProcName = when (CURRENT_PLATFORM) {
|
||||
Platform.WINDOWS -> "vrserver.exe"
|
||||
else -> "vrserver"
|
||||
@@ -100,6 +103,8 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
private val driverState = MutableStateFlow(SteamVRDriverState())
|
||||
private val standableState = MutableStateFlow(false)
|
||||
|
||||
private fun driverEnabled() = settings.context.state.value.data.driverConfig.enabled
|
||||
|
||||
override fun observe(receiver: TrackingChecklist) {
|
||||
receiver.context.scope.launch {
|
||||
HttpClient(CIO).use { client ->
|
||||
@@ -107,7 +112,16 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
var lastScan: TimeMark? = null
|
||||
|
||||
while (isActive) {
|
||||
val connected = server.context.state.value.drivers.isNotEmpty()
|
||||
// Nothing to look at while the output is off: no driver can connect, and the step
|
||||
// below is switched off anyway. Reset so switching it back on does not report
|
||||
// whatever SteamVR was doing beforehand.
|
||||
if (!driverEnabled()) {
|
||||
driverState.value = SteamVRDriverState()
|
||||
delay(3000)
|
||||
continue
|
||||
}
|
||||
|
||||
val connected = server.context.state.value.drivers.values.any { it.source == DriverBridgeSource.DRIVER }
|
||||
|
||||
val drivers = try {
|
||||
getSteamVRDriversList(client)
|
||||
@@ -149,11 +163,15 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
}
|
||||
}
|
||||
|
||||
driverState
|
||||
.map { state ->
|
||||
combine(
|
||||
driverState,
|
||||
settings.context.state.map { it.data.driverConfig.enabled }.distinctUntilChanged(),
|
||||
::Pair,
|
||||
)
|
||||
.map { (state, outputEnabled) ->
|
||||
TrackingChecklistStep(
|
||||
valid = state.connected,
|
||||
enabled = true,
|
||||
enabled = outputEnabled,
|
||||
ignorable = true,
|
||||
extraData = if (!state.connected) {
|
||||
TrackingChecklistSteamVRDisconnected(
|
||||
|
||||
Reference in new issue
Block a user