mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-21 17:41:28 -08:00
Speeeeeeeeed
This commit is contained in:
1 parent
b0232ae783
commit
e207abbc31
14 files changed
+308
-203
No files matched your search
@@ -150,7 +150,7 @@ fun createAndroidHIDManager(context: Context, appContext: AppContextProvider, sc
|
||||
}
|
||||
when {
|
||||
read < 0 -> return@launch
|
||||
read > 0 -> parseHIDPackets(buffer.copyOf(read)).forEach { packet -> receiver.packetEvents.emit(packet) }
|
||||
read > 0 -> parseHIDPackets(buffer, read).forEach { packet -> receiver.packetEvents.emit(packet) }
|
||||
else -> delay(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class LoggingMiddleware<S, A : Any>(
|
||||
contextName = context.scope.coroutineContext[CoroutineName]?.name ?: "Unknown"
|
||||
}
|
||||
|
||||
private fun isAllowed(action: A): Boolean {
|
||||
override fun isAllowed(action: A): Boolean {
|
||||
val klass = action::class
|
||||
if (klass in block) return false
|
||||
if (allow != null && klass !in allow) return false
|
||||
|
||||
@@ -12,6 +12,7 @@ import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.launch
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
import kotlin.time.TimeSource
|
||||
|
||||
class HIDRegistrationBehaviour : HIDReceiverBehaviour {
|
||||
override fun reduce(state: HIDReceiverState, action: HIDReceiverActions) = when (action) {
|
||||
@@ -176,8 +177,10 @@ private const val HID_TIMEOUT_MS = 2_000L
|
||||
|
||||
class HIDSleepBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
val startedAt = TimeSource.Monotonic.markNow()
|
||||
val sleepJobs = mutableMapOf<Int, Job>()
|
||||
val idleJobs = mutableMapOf<Int, Job>()
|
||||
val lastSeen = mutableMapOf<Int, Long>()
|
||||
|
||||
fun scheduleSleep(hidId: Int, timeoutMs: Int) {
|
||||
if (timeoutMs == 0) return
|
||||
@@ -193,9 +196,14 @@ class HIDSleepBehaviour : HIDReceiverBehaviour {
|
||||
}
|
||||
|
||||
fun armIdleTimeout(hidId: Int) {
|
||||
idleJobs[hidId]?.cancel()
|
||||
lastSeen[hidId] = startedAt.elapsedNow().inWholeMilliseconds
|
||||
if (idleJobs[hidId]?.isActive == true) return
|
||||
idleJobs[hidId] = receiver.context.scope.launch {
|
||||
delay(HID_TIMEOUT_MS)
|
||||
var remaining = HID_TIMEOUT_MS
|
||||
while (remaining > 0) {
|
||||
delay(remaining)
|
||||
remaining = (lastSeen[hidId] ?: 0L) + HID_TIMEOUT_MS - startedAt.elapsedNow().inWholeMilliseconds
|
||||
}
|
||||
receiver.getTracker(hidId)?.context?.dispatch(TrackerActions.SetStatus(TrackerStatus.TIMED_OUT))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ private fun parseSingleHIDPacket(data: ByteArray, i: Int): HIDPacket? {
|
||||
}
|
||||
}
|
||||
|
||||
fun parseHIDPackets(data: ByteArray): List<HIDPacket> {
|
||||
if (data.size % HID_PACKET_SIZE != 0) return emptyList()
|
||||
return (0 until data.size / HID_PACKET_SIZE).mapNotNull { parseSingleHIDPacket(data, it * HID_PACKET_SIZE) }
|
||||
fun parseHIDPackets(data: ByteArray, length: Int = data.size): List<HIDPacket> {
|
||||
if (length % HID_PACKET_SIZE != 0) return emptyList()
|
||||
return (0 until length / HID_PACKET_SIZE).mapNotNull { parseSingleHIDPacket(data, it * HID_PACKET_SIZE) }
|
||||
}
|
||||
@@ -1,125 +1,109 @@
|
||||
package dev.slimevr.osc
|
||||
|
||||
import kotlinx.io.Buffer
|
||||
import kotlinx.io.Sink
|
||||
import kotlinx.io.Source
|
||||
import kotlinx.io.indexOf
|
||||
import kotlinx.io.readByteArray
|
||||
import kotlinx.io.readDouble
|
||||
import kotlinx.io.readFloat
|
||||
import kotlinx.io.readString
|
||||
import kotlinx.io.write
|
||||
import kotlinx.io.writeDouble
|
||||
import kotlinx.io.writeFloat
|
||||
|
||||
private val BUNDLE_PREFIX = "#bundle".encodeToByteArray() + byteArrayOf(0)
|
||||
|
||||
private fun pad4(n: Int) = (n + 3) and 3.inv()
|
||||
|
||||
private class Writer(capacity: Int = 65536) {
|
||||
private val buf = ByteArray(capacity)
|
||||
var pos = 0
|
||||
/** OSC aligns strings and blobs to a four-byte boundary with trailing nulls. */
|
||||
private fun Sink.writePadding(unpadded: Int) = repeat(pad4(unpadded) - unpadded) { writeByte(0) }
|
||||
|
||||
fun byte(v: Byte) {
|
||||
buf[pos++] = v
|
||||
}
|
||||
fun bytes(v: ByteArray) {
|
||||
v.copyInto(buf, pos)
|
||||
pos += v.size
|
||||
}
|
||||
fun int(v: Int) {
|
||||
byte((v ushr 24).toByte())
|
||||
byte((v ushr 16).toByte())
|
||||
byte((v ushr 8).toByte())
|
||||
byte(v.toByte())
|
||||
}
|
||||
fun long(v: Long) {
|
||||
int((v ushr 32).toInt())
|
||||
int(v.toInt())
|
||||
}
|
||||
fun float(v: Float) = int(v.toBits())
|
||||
fun double(v: Double) = long(v.toBits())
|
||||
fun string(s: String) {
|
||||
val b = s.encodeToByteArray()
|
||||
bytes(b)
|
||||
byte(0)
|
||||
repeat(pad4(b.size + 1) - b.size - 1) { byte(0) }
|
||||
}
|
||||
fun result() = buf.copyOfRange(0, pos)
|
||||
private fun Sink.writeOscString(value: String) {
|
||||
val bytes = value.encodeToByteArray()
|
||||
write(bytes)
|
||||
writeByte(0)
|
||||
writePadding(bytes.size + 1)
|
||||
}
|
||||
|
||||
private class Reader(private val bytes: ByteArray, var pos: Int = 0) {
|
||||
private fun byte() = bytes[pos++].toInt()
|
||||
fun int() = (byte() and 0xFF shl 24) or (byte() and 0xFF shl 16) or (byte() and 0xFF shl 8) or (byte() and 0xFF)
|
||||
fun long() = (int().toLong() and 0xFFFFFFFFL shl 32) or (int().toLong() and 0xFFFFFFFFL)
|
||||
fun float() = Float.fromBits(int())
|
||||
fun double() = Double.fromBits(long())
|
||||
fun string(): String {
|
||||
val start = pos
|
||||
while (pos < bytes.size && bytes[pos] != 0.toByte()) pos++
|
||||
val s = bytes.decodeToString(start, pos)
|
||||
pos++ // skip null
|
||||
pos = pad4(pos)
|
||||
return s
|
||||
}
|
||||
fun bytes(n: Int) = bytes.copyOfRange(pos, pos + n).also { pos += n }
|
||||
fun hasPrefix(prefix: ByteArray) = pos + prefix.size <= bytes.size &&
|
||||
bytes.copyOfRange(pos, pos + prefix.size).contentEquals(prefix)
|
||||
private fun Source.readOscString(): String {
|
||||
val length = indexOf(0)
|
||||
require(length >= 0) { "Unterminated OSC string" }
|
||||
val value = readString(length)
|
||||
// The null terminator counts towards the alignment it pads out to
|
||||
skip(pad4(length.toInt() + 1) - length)
|
||||
return value
|
||||
}
|
||||
|
||||
fun encodeMessage(msg: OscMessage): ByteArray {
|
||||
val w = Writer()
|
||||
w.string(msg.address)
|
||||
w.string("," + msg.args.joinToString("") { it.typeTag.toString() })
|
||||
private fun Source.startsWithBundlePrefix(): Boolean {
|
||||
val peeked = peek()
|
||||
return peeked.request(BUNDLE_PREFIX.size.toLong()) &&
|
||||
peeked.readByteArray(BUNDLE_PREFIX.size).contentEquals(BUNDLE_PREFIX)
|
||||
}
|
||||
|
||||
fun writeMessage(dst: Sink, msg: OscMessage) {
|
||||
dst.writeOscString(msg.address)
|
||||
dst.writeOscString("," + msg.args.joinToString("") { it.typeTag.toString() })
|
||||
for (arg in msg.args) {
|
||||
when (arg) {
|
||||
is OscArg.Int -> w.int(arg.value)
|
||||
is OscArg.Int -> dst.writeInt(arg.value)
|
||||
|
||||
is OscArg.Long -> w.long(arg.value)
|
||||
is OscArg.Long -> dst.writeLong(arg.value)
|
||||
|
||||
is OscArg.Float -> w.float(arg.value)
|
||||
is OscArg.Float -> dst.writeFloat(arg.value)
|
||||
|
||||
is OscArg.Double -> w.double(arg.value)
|
||||
is OscArg.Double -> dst.writeDouble(arg.value)
|
||||
|
||||
is OscArg.String -> w.string(arg.value)
|
||||
is OscArg.String -> dst.writeOscString(arg.value)
|
||||
|
||||
is OscArg.Blob -> {
|
||||
w.int(arg.value.size)
|
||||
w.bytes(arg.value)
|
||||
repeat(pad4(arg.value.size) - arg.value.size) { w.byte(0) }
|
||||
dst.writeInt(arg.value.size)
|
||||
dst.write(arg.value)
|
||||
dst.writePadding(arg.value.size)
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
return w.result()
|
||||
}
|
||||
|
||||
fun encodeBundle(bundle: OscBundle): ByteArray {
|
||||
val w = Writer()
|
||||
w.bytes(BUNDLE_PREFIX)
|
||||
w.long(bundle.timetag)
|
||||
fun writeBundle(dst: Sink, bundle: OscBundle) {
|
||||
dst.write(BUNDLE_PREFIX)
|
||||
dst.writeLong(bundle.timetag)
|
||||
// Each element is length-prefixed, so it has to be built before its size is known. Handing it over
|
||||
// between two Buffers moves segments rather than copying the bytes, and drains it for the next one.
|
||||
val element = Buffer()
|
||||
for (content in bundle.contents) {
|
||||
val encoded = when (content) {
|
||||
is OscContent.Message -> encodeMessage(content.msg)
|
||||
is OscContent.Bundle -> encodeBundle(content.bundle)
|
||||
when (content) {
|
||||
is OscContent.Message -> writeMessage(element, content.msg)
|
||||
is OscContent.Bundle -> writeBundle(element, content.bundle)
|
||||
}
|
||||
w.int(encoded.size)
|
||||
w.bytes(encoded)
|
||||
dst.writeInt(element.size.toInt())
|
||||
dst.transferFrom(element)
|
||||
}
|
||||
return w.result()
|
||||
}
|
||||
|
||||
fun decodeMessage(bytes: ByteArray, offset: Int = 0): Pair<OscMessage, Int> {
|
||||
val r = Reader(bytes, offset)
|
||||
val address = r.string()
|
||||
val typeTag = r.string()
|
||||
fun readMessage(src: Source): OscMessage {
|
||||
val address = src.readOscString()
|
||||
val typeTag = src.readOscString()
|
||||
val args = mutableListOf<OscArg>()
|
||||
if (typeTag.startsWith(",")) {
|
||||
for (c in typeTag.drop(1)) {
|
||||
when (c) {
|
||||
'i' -> args += OscArg.Int(r.int())
|
||||
'i' -> args += OscArg.Int(src.readInt())
|
||||
|
||||
'h' -> args += OscArg.Long(r.long())
|
||||
'h' -> args += OscArg.Long(src.readLong())
|
||||
|
||||
'f' -> args += OscArg.Float(r.float())
|
||||
'f' -> args += OscArg.Float(src.readFloat())
|
||||
|
||||
'd' -> args += OscArg.Double(r.double())
|
||||
'd' -> args += OscArg.Double(src.readDouble())
|
||||
|
||||
's' -> args += OscArg.String(r.string())
|
||||
's' -> args += OscArg.String(src.readOscString())
|
||||
|
||||
'b' -> {
|
||||
val size = r.int()
|
||||
args += OscArg.Blob(r.bytes(size))
|
||||
r.pos += pad4(size) - size
|
||||
val size = src.readInt()
|
||||
args += OscArg.Blob(src.readByteArray(size))
|
||||
src.skip((pad4(size) - size).toLong())
|
||||
}
|
||||
|
||||
'I' -> args += OscArg.Impulse
|
||||
@@ -132,29 +116,28 @@ fun decodeMessage(bytes: ByteArray, offset: Int = 0): Pair<OscMessage, Int> {
|
||||
}
|
||||
}
|
||||
}
|
||||
return OscMessage(address, args) to r.pos
|
||||
return OscMessage(address, args)
|
||||
}
|
||||
|
||||
fun decodeBundle(bytes: ByteArray, offset: Int = 0): Pair<OscBundle, Int> {
|
||||
val r = Reader(bytes, offset)
|
||||
if (!r.hasPrefix(BUNDLE_PREFIX)) throw IllegalArgumentException("Invalid bundle prefix")
|
||||
r.pos += 8
|
||||
val timetag = r.long()
|
||||
fun readBundle(src: Source): OscBundle {
|
||||
require(src.startsWithBundlePrefix()) { "Invalid bundle prefix" }
|
||||
src.skip(BUNDLE_PREFIX.size.toLong())
|
||||
val timetag = src.readLong()
|
||||
val contents = mutableListOf<OscContent>()
|
||||
while (r.pos + 4 <= bytes.size) {
|
||||
val elementSize = r.int()
|
||||
if (elementSize <= 0 || r.pos + elementSize > bytes.size) break
|
||||
val elemStart = r.pos
|
||||
try {
|
||||
contents += if (Reader(bytes, elemStart).hasPrefix(BUNDLE_PREFIX)) {
|
||||
OscContent.Bundle(decodeBundle(bytes, elemStart).first)
|
||||
while (src.request(4)) {
|
||||
val size = src.readInt()
|
||||
if (size <= 0 || !src.request(size.toLong())) break
|
||||
val element = Buffer()
|
||||
src.readTo(element, size.toLong())
|
||||
contents += try {
|
||||
if (element.startsWithBundlePrefix()) {
|
||||
OscContent.Bundle(readBundle(element))
|
||||
} else {
|
||||
OscContent.Message(decodeMessage(bytes, elemStart).first)
|
||||
OscContent.Message(readMessage(element))
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
break
|
||||
}
|
||||
r.pos = elemStart + elementSize
|
||||
}
|
||||
return OscBundle(timetag, contents) to r.pos
|
||||
return OscBundle(timetag, contents)
|
||||
}
|
||||
@@ -12,8 +12,6 @@ import kotlinx.io.readByteArray
|
||||
|
||||
private val selectorManager = SelectorManager(Dispatchers.IO)
|
||||
|
||||
private fun ByteArray.toBuffer() = Buffer().apply { write(this@toBuffer) }
|
||||
|
||||
class OscSender(private val address: String, private val port: Int) {
|
||||
private var socket: ConnectedDatagramSocket? = null
|
||||
|
||||
@@ -22,8 +20,17 @@ class OscSender(private val address: String, private val port: Int) {
|
||||
return s
|
||||
}
|
||||
|
||||
suspend fun send(message: OscMessage) = socket().send(Datagram(encodeMessage(message).toBuffer(), InetSocketAddress(address, port)))
|
||||
suspend fun send(bundle: OscBundle) = socket().send(Datagram(encodeBundle(bundle).toBuffer(), InetSocketAddress(address, port)))
|
||||
suspend fun send(message: OscMessage) {
|
||||
val buf = Buffer()
|
||||
writeMessage(buf, message)
|
||||
socket().send(Datagram(buf, InetSocketAddress(address, port)))
|
||||
}
|
||||
|
||||
suspend fun send(bundle: OscBundle) {
|
||||
val buf = Buffer()
|
||||
writeBundle(buf, bundle)
|
||||
socket().send(Datagram(buf, InetSocketAddress(address, port)))
|
||||
}
|
||||
|
||||
fun close() {
|
||||
socket?.close()
|
||||
@@ -45,11 +52,11 @@ class OscReceiver(private val port: Int) {
|
||||
running = true
|
||||
while (running) {
|
||||
try {
|
||||
val bytes = s.receive().packet.readByteArray()
|
||||
val packet = s.receive().packet
|
||||
val bundle = try {
|
||||
decodeBundle(bytes).first
|
||||
readBundle(packet)
|
||||
} catch (_: IllegalArgumentException) {
|
||||
OscBundle(1, listOf(OscContent.Message(decodeMessage(bytes).first)))
|
||||
OscBundle(1, listOf(OscContent.Message(readMessage(packet))))
|
||||
}
|
||||
onBundle(bundle)
|
||||
} catch (_: Exception) {
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
package dev.slimevr.osc
|
||||
|
||||
import kotlinx.io.Buffer
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private fun encode(msg: OscMessage) = Buffer().also { writeMessage(it, msg) }
|
||||
|
||||
private fun encode(bundle: OscBundle) = Buffer().also { writeBundle(it, bundle) }
|
||||
|
||||
class CodecTest {
|
||||
@Test
|
||||
fun testEncodeDecodeIntMessage() {
|
||||
val msg = OscMessage("/test", listOf(OscArg.Int(42)))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/test", decoded.address)
|
||||
assertEquals(1, decoded.args.size)
|
||||
@@ -19,8 +26,8 @@ class CodecTest {
|
||||
@Test
|
||||
fun testEncodeDecodeFloatMessage() {
|
||||
val msg = OscMessage("/test/float", listOf(OscArg.Float(3.14f)))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/test/float", decoded.address)
|
||||
assertEquals(1, decoded.args.size)
|
||||
@@ -31,8 +38,8 @@ class CodecTest {
|
||||
@Test
|
||||
fun testEncodeDecodeStringMessage() {
|
||||
val msg = OscMessage("/address", listOf(OscArg.String("hello")))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/address", decoded.address)
|
||||
assertEquals(1, decoded.args.size)
|
||||
@@ -43,8 +50,8 @@ class CodecTest {
|
||||
fun testEncodeDecodeBlobMessage() {
|
||||
val blobData = byteArrayOf(1, 2, 3, 4, 5)
|
||||
val msg = OscMessage("/blob", listOf(OscArg.Blob(blobData)))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/blob", decoded.address)
|
||||
assertEquals(1, decoded.args.size)
|
||||
@@ -55,8 +62,8 @@ class CodecTest {
|
||||
@Test
|
||||
fun testEncodeDecodeLongMessage() {
|
||||
val msg = OscMessage("/long", listOf(OscArg.Long(9876543210L)))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/long", decoded.address)
|
||||
assertEquals(1, decoded.args.size)
|
||||
@@ -66,8 +73,8 @@ class CodecTest {
|
||||
@Test
|
||||
fun testEncodeDecodeDoubleMessage() {
|
||||
val msg = OscMessage("/double", listOf(OscArg.Double(2.718281828)))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/double", decoded.address)
|
||||
assertEquals(1, decoded.args.size)
|
||||
@@ -90,8 +97,8 @@ class CodecTest {
|
||||
OscArg.Float(0.707f),
|
||||
),
|
||||
)
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/VMC/Ext/Bone/Pos", decoded.address)
|
||||
assertEquals(8, decoded.args.size)
|
||||
@@ -110,8 +117,8 @@ class CodecTest {
|
||||
OscArg.Long(4L),
|
||||
),
|
||||
)
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals(4, decoded.args.size)
|
||||
assertEquals(1, (decoded.args[0] as OscArg.Int).value)
|
||||
@@ -124,8 +131,8 @@ class CodecTest {
|
||||
fun testSimpleBundle() {
|
||||
// Test with just one message to isolate the issue
|
||||
val msg = OscMessage("/test", listOf(OscArg.Int(42)))
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
assertEquals(42, (decoded.args[0] as OscArg.Int).value)
|
||||
}
|
||||
|
||||
@@ -133,9 +140,9 @@ class CodecTest {
|
||||
fun testBundleWithOneMessage() {
|
||||
val msg = OscMessage("/single", listOf(OscArg.Int(99)))
|
||||
val bundle = OscBundle(5, listOf(OscContent.Message(msg)))
|
||||
val encoded = encodeBundle(bundle)
|
||||
val encoded = encode(bundle)
|
||||
|
||||
val (decoded, _) = decodeBundle(encoded)
|
||||
val decoded = readBundle(encoded)
|
||||
|
||||
assertEquals(5, decoded.timetag)
|
||||
assertEquals(1, decoded.contents.size)
|
||||
@@ -157,8 +164,8 @@ class CodecTest {
|
||||
OscContent.Message(OscMessage("/msg2", listOf(OscArg.String("hello")))),
|
||||
),
|
||||
)
|
||||
val encoded = encodeBundle(bundle)
|
||||
val (decoded, _) = decodeBundle(encoded)
|
||||
val encoded = encode(bundle)
|
||||
val decoded = readBundle(encoded)
|
||||
|
||||
assertEquals(1, decoded.timetag)
|
||||
assertEquals(2, decoded.contents.size)
|
||||
@@ -186,8 +193,8 @@ class CodecTest {
|
||||
),
|
||||
)
|
||||
|
||||
val encoded = encodeBundle(outerBundle)
|
||||
val (decoded, _) = decodeBundle(encoded)
|
||||
val encoded = encode(outerBundle)
|
||||
val decoded = readBundle(encoded)
|
||||
|
||||
assertEquals(1, decoded.timetag)
|
||||
assertEquals(2, decoded.contents.size)
|
||||
@@ -205,8 +212,8 @@ class CodecTest {
|
||||
@Test
|
||||
fun testEmptyMessage() {
|
||||
val msg = OscMessage("/empty")
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals("/empty", decoded.address)
|
||||
assertEquals(0, decoded.args.size)
|
||||
@@ -218,8 +225,8 @@ class CodecTest {
|
||||
"/special",
|
||||
listOf(OscArg.True, OscArg.False, OscArg.Null, OscArg.Impulse),
|
||||
)
|
||||
val encoded = encodeMessage(msg)
|
||||
val (decoded, _) = decodeMessage(encoded)
|
||||
val encoded = encode(msg)
|
||||
val decoded = readMessage(encoded)
|
||||
|
||||
assertEquals(4, decoded.args.size)
|
||||
assertEquals(OscArg.True, decoded.args[0])
|
||||
@@ -234,12 +241,12 @@ class CodecTest {
|
||||
val short = OscMessage("/a", listOf(OscArg.Int(1)))
|
||||
val long = OscMessage("/this/is/a/very/long/address", listOf(OscArg.Int(1)))
|
||||
|
||||
val encodedShort = encodeMessage(short)
|
||||
val encodedLong = encodeMessage(long)
|
||||
val encodedShort = encode(short)
|
||||
val encodedLong = encode(long)
|
||||
|
||||
// Both should encode/decode correctly regardless of alignment
|
||||
val (decodedShort, _) = decodeMessage(encodedShort)
|
||||
val (decodedLong, _) = decodeMessage(encodedLong)
|
||||
val decodedShort = readMessage(encodedShort)
|
||||
val decodedLong = readMessage(encodedLong)
|
||||
|
||||
assertEquals("/a", decodedShort.address)
|
||||
assertEquals("/this/is/a/very/long/address", decodedLong.address)
|
||||
@@ -250,16 +257,76 @@ class CodecTest {
|
||||
val msg1 = OscMessage("/first", listOf(OscArg.Int(1)))
|
||||
val msg2 = OscMessage("/second", listOf(OscArg.Int(2)))
|
||||
|
||||
val encoded1 = encodeMessage(msg1)
|
||||
val encoded2 = encodeMessage(msg2)
|
||||
val combined = encoded1 + encoded2
|
||||
// Both written back to back into one source: reading the first has to leave the second aligned
|
||||
val combined = Buffer()
|
||||
writeMessage(combined, msg1)
|
||||
writeMessage(combined, msg2)
|
||||
|
||||
// Parse first message from combined
|
||||
val (decoded1, offset) = decodeMessage(combined, 0)
|
||||
assertEquals("/first", decoded1.address)
|
||||
assertEquals("/first", readMessage(combined).address)
|
||||
assertEquals("/second", readMessage(combined).address)
|
||||
assertTrue(combined.exhausted(), "Reading both messages should consume the whole source")
|
||||
}
|
||||
|
||||
// Parse second message from offset
|
||||
val (decoded2, _) = decodeMessage(combined, offset)
|
||||
assertEquals("/second", decoded2.address)
|
||||
@Test
|
||||
fun testStringAndBlobPaddingBoundaries() {
|
||||
// Every length either side of a four-byte boundary, where the alignment maths is easiest to get wrong
|
||||
for (n in 0..9) {
|
||||
val text = "z".repeat(n)
|
||||
val decoded = readMessage(encode(OscMessage("/s", listOf(OscArg.String(text)))))
|
||||
assertEquals(text, (decoded.args[0] as OscArg.String).value, "string of length $n")
|
||||
|
||||
val blob = ByteArray(n) { it.toByte() }
|
||||
val decodedBlob = readMessage(encode(OscMessage("/b", listOf(OscArg.Blob(blob)))))
|
||||
assertContentEquals(blob, (decodedBlob.args[0] as OscArg.Blob).value, "blob of length $n")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddressLengthsAcrossBoundaries() {
|
||||
for (n in 1..12) {
|
||||
val address = "/" + "a".repeat(n)
|
||||
val decoded = readMessage(encode(OscMessage(address, listOf(OscArg.Int(7)))))
|
||||
assertEquals(address, decoded.address, "address of length ${address.length}")
|
||||
assertEquals(7, (decoded.args[0] as OscArg.Int).value)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLargeBundleRoundTrips() {
|
||||
// A VMC-shaped frame: many small messages, far past any single-segment buffer
|
||||
val contents = (0 until 55).map { i ->
|
||||
OscContent.Message(
|
||||
OscMessage(
|
||||
"/VMC/Ext/Bone/Pos",
|
||||
listOf(OscArg.String("BoneNameNumber$i")) + (0 until 7).map { OscArg.Float(i * 0.125f + it) },
|
||||
),
|
||||
)
|
||||
}
|
||||
val decoded = readBundle(encode(OscBundle(1, contents)))
|
||||
|
||||
assertEquals(contents.size, decoded.contents.size)
|
||||
assertEquals(contents, decoded.contents)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOversizedBlobRoundTrips() {
|
||||
val blob = ByteArray(9000) { (it * 31).toByte() }
|
||||
val msg = OscMessage("/big", listOf(OscArg.Blob(blob), OscArg.String("tail"), OscArg.Int(42)))
|
||||
val decoded = readMessage(encode(msg))
|
||||
|
||||
assertContentEquals(blob, (decoded.args[0] as OscArg.Blob).value)
|
||||
assertEquals("tail", (decoded.args[1] as OscArg.String).value)
|
||||
assertEquals(42, (decoded.args[2] as OscArg.Int).value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNonBundleIsRejectedWithoutConsuming() {
|
||||
// listenBundles relies on this to fall back to reading the same source as a plain message
|
||||
val source = encode(OscMessage("/plain", listOf(OscArg.Int(1))))
|
||||
val before = source.size
|
||||
|
||||
assertFailsWith<IllegalArgumentException> { readBundle(source) }
|
||||
assertEquals(before, source.size, "A rejected bundle must not consume any bytes")
|
||||
assertEquals("/plain", readMessage(source).address)
|
||||
}
|
||||
}
|
||||
@@ -101,8 +101,9 @@ fun createDesktopHIDManager(appContext: AppContextProvider, scope: CoroutineScop
|
||||
|
||||
deviceScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
// Reused across reads: parseHIDPackets is told how much of it is live
|
||||
val buffer = ByteArray(HID_READ_BUFFER_SIZE)
|
||||
while (isActive) {
|
||||
val buffer = ByteArray(HID_READ_BUFFER_SIZE)
|
||||
val read = try {
|
||||
hidDevice.read(buffer, HID_READ_TIMEOUT_MS)
|
||||
} catch (_: Exception) {
|
||||
@@ -112,7 +113,7 @@ fun createDesktopHIDManager(appContext: AppContextProvider, scope: CoroutineScop
|
||||
// read error, device gone
|
||||
read < 0 -> return@launch
|
||||
|
||||
read > 0 -> parseHIDPackets(buffer).forEach { receiver.packetEvents.emit(it) }
|
||||
read > 0 -> parseHIDPackets(buffer, read).forEach { receiver.packetEvents.emit(it) }
|
||||
|
||||
// 0 is a timeout with no data: the read already blocked, so just go again
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ suspend fun createUnixDriverSocket(appContext: AppContextProvider) = acceptUnixC
|
||||
handleDriverConnection(
|
||||
appContext = appContext,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ suspend fun createUnixFeederSocket(appContext: AppContextProvider) = acceptUnixC
|
||||
handleDriverConnection(
|
||||
appContext = appContext,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ suspend fun createUnixSolarXRSocket(appContext: AppContextProvider) = acceptUnix
|
||||
handleSolarXRBridge(
|
||||
appContext = appContext,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> withContext(Dispatchers.IO) { writeFramed(channel, bytes) } },
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,28 +7,28 @@ import com.sun.jna.platform.win32.Kernel32
|
||||
import com.sun.jna.platform.win32.WinBase
|
||||
import com.sun.jna.platform.win32.WinError
|
||||
import com.sun.jna.platform.win32.WinNT
|
||||
import com.sun.jna.ptr.IntByReference
|
||||
import com.sun.jna.win32.StdCallLibrary
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import java.io.IOException
|
||||
|
||||
private val k32 = Kernel32.INSTANCE
|
||||
private val k32io = Kernel32IO.INSTANCE
|
||||
|
||||
private const val HEADER_SIZE = 4
|
||||
private const val MAX_FRAME_SIZE = 256 * 1024
|
||||
|
||||
@Suppress("FunctionName")
|
||||
private interface Kernel32IO : StdCallLibrary {
|
||||
fun ReadFile(h: WinNT.HANDLE, buf: Pointer, len: Int, read: IntByReference?, ov: WinBase.OVERLAPPED): Boolean
|
||||
|
||||
fun WriteFile(h: WinNT.HANDLE, buf: Pointer, len: Int, written: IntByReference?, ov: WinBase.OVERLAPPED): Boolean
|
||||
|
||||
fun GetOverlappedResult(h: WinNT.HANDLE, ov: WinBase.OVERLAPPED, moved: IntByReference, wait: Boolean): Boolean
|
||||
|
||||
companion object {
|
||||
val INSTANCE: Kernel32IO = Native.load("kernel32", Kernel32IO::class.java)
|
||||
private object Kernel32IO : StdCallLibrary {
|
||||
init {
|
||||
Native.register(Kernel32IO::class.java, "kernel32")
|
||||
}
|
||||
|
||||
external fun ReadFile(handle: Pointer, buffer: Pointer, len: Int, read: Pointer?, overlapped: Pointer): Boolean
|
||||
|
||||
external fun WriteFile(handle: Pointer, buffer: Pointer, len: Int, written: Pointer?, overlapped: Pointer): Boolean
|
||||
|
||||
external fun GetOverlappedResult(handle: Pointer, overlapped: Pointer, moved: Pointer, wait: Boolean): Boolean
|
||||
|
||||
external fun WaitForSingleObject(handle: Pointer, millis: Int): Int
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,6 +48,14 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
|
||||
private val buffer = Memory(MAX_FRAME_SIZE.toLong())
|
||||
|
||||
// Resolved once. The wrappers have to stay referenced to keep the native memory alive anyway, so
|
||||
// unwrapping them per call would only re-do work. `transferred` replaces an IntByReference that
|
||||
// was allocated on every completion.
|
||||
private val handlePointer = handle.pointer
|
||||
private val eventPointer = event.pointer
|
||||
private val overlappedPointer = overlapped.pointer
|
||||
private val transferred = Memory(4)
|
||||
|
||||
fun awaitClient(): Boolean {
|
||||
// On an overlapped handle ConnectNamedPipe returns straight away, so we do the waiting
|
||||
val connected = k32.ConnectNamedPipe(handle, overlapped)
|
||||
@@ -76,12 +84,13 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
buffer.setInt(0, total)
|
||||
buffer.write(HEADER_SIZE.toLong(), payload, 0, payload.size)
|
||||
|
||||
val written = transfer { k32io.WriteFile(handle, buffer, total, null, overlapped) }
|
||||
val written = transfer { Kernel32IO.WriteFile(handlePointer, buffer, total, null, overlappedPointer) }
|
||||
if (written != total) throw IOException("Pipe write incomplete: $written of $total bytes")
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
buffer.close()
|
||||
transferred.close()
|
||||
k32.CloseHandle(event)
|
||||
}
|
||||
|
||||
@@ -90,7 +99,7 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
while (done < len) {
|
||||
val remaining = len - done
|
||||
val moved = transfer {
|
||||
k32io.ReadFile(handle, buffer.share((at + done).toLong(), remaining.toLong()), remaining, null, overlapped)
|
||||
Kernel32IO.ReadFile(handlePointer, buffer.share((at + done).toLong(), remaining.toLong()), remaining, null, overlappedPointer)
|
||||
}
|
||||
if (moved <= 0) return false
|
||||
done += moved
|
||||
@@ -114,9 +123,8 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
}
|
||||
|
||||
private fun awaitCompletion(): Int {
|
||||
k32.WaitForSingleObject(event, WinBase.INFINITE)
|
||||
val moved = IntByReference()
|
||||
if (!k32io.GetOverlappedResult(handle, overlapped, moved, true)) return -1
|
||||
return moved.value
|
||||
Kernel32IO.WaitForSingleObject(eventPointer, WinBase.INFINITE)
|
||||
if (!Kernel32IO.GetOverlappedResult(handlePointer, overlappedPointer, transferred, true)) return -1
|
||||
return transferred.getInt(0)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.slimevr.desktop.ipc
|
||||
|
||||
import com.squareup.wire.ProtoWriter
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.CURRENT_PLATFORM
|
||||
import dev.slimevr.Platform
|
||||
@@ -24,6 +25,7 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.Buffer
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
@@ -130,8 +132,12 @@ suspend fun handleDriverConnection(
|
||||
) = coroutineScope {
|
||||
val sendMutex = Mutex()
|
||||
|
||||
val encodeBuffer = Buffer()
|
||||
val encodeWriter = ProtoWriter(encodeBuffer)
|
||||
|
||||
suspend fun sendMsg(msg: ProtobufMessage) = sendMutex.withLock {
|
||||
send(ProtobufMessage.ADAPTER.encode(msg))
|
||||
ProtobufMessage.ADAPTER.encode(encodeWriter, msg)
|
||||
send(encodeBuffer.readByteArray())
|
||||
}
|
||||
|
||||
val bridge = DriverBridge.create(
|
||||
|
||||
@@ -75,8 +75,13 @@ suspend fun handleSolarXRBridge(
|
||||
|
||||
appContext.server.context.dispatch(VRServerActions.SolarXRConnected(bridge))
|
||||
|
||||
// One builder for the life of the connection. clear() keeps the buffer it has already grown into,
|
||||
// so a datafeed frame stops re-growing from 256 bytes every time. Safe to share: this collector
|
||||
// handles one bundle at a time, and moveToByteArray copies the bytes out before send suspends.
|
||||
val fbb = FlatBufferBuilder(256)
|
||||
|
||||
bridge.outbound.on<MessageBundle> { bundle ->
|
||||
val fbb = FlatBufferBuilder(256)
|
||||
fbb.clear()
|
||||
fbb.finish(bundle.encode(JvmFlatBufferWriter(fbb)))
|
||||
send(fbb.dataBuffer().moveToByteArray())
|
||||
}.launchIn(this)
|
||||
|
||||
@@ -84,7 +84,7 @@ private suspend fun acceptWindowsClients(
|
||||
|
||||
launch {
|
||||
try {
|
||||
handle(readFrames(pipe)) { bytes -> withContext(Dispatchers.IO) { writer.writeFrame(bytes) } }
|
||||
handle(readFrames(pipe)) { bytes -> writer.writeFrame(bytes) }
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -32,6 +32,10 @@ import solarxr_protocol.rpc.TrackingChecklistStep
|
||||
import solarxr_protocol.rpc.TrackingChecklistStepId
|
||||
import solarxr_protocol.rpc.TrackingChecklistStepVisibility
|
||||
import java.io.IOException
|
||||
import java.net.ConnectException
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.TimeMark
|
||||
import kotlin.time.TimeSource
|
||||
|
||||
data class Process(val pid: ULong, val name: String)
|
||||
|
||||
@@ -80,6 +84,12 @@ private suspend fun getRunningProcesses(): List<Process> = when (CURRENT_PLATFOR
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
// Enumerating every process is orders of magnitude costlier than the loopback request it guards, so it
|
||||
// only runs to disambiguate a refused connection, and only this often.
|
||||
private val PROCESS_SCAN_INTERVAL = 10.seconds
|
||||
|
||||
private fun Throwable.isConnectionRefused(): Boolean = generateSequence(this, Throwable::cause).any { it is ConnectException }
|
||||
|
||||
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 {
|
||||
@@ -92,41 +102,51 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
|
||||
override fun observe(receiver: TrackingChecklist) {
|
||||
receiver.context.scope.launch {
|
||||
val client = HttpClient(CIO)
|
||||
while (isActive) {
|
||||
val connected = server.context.state.value.drivers.isNotEmpty()
|
||||
val running = connected ||
|
||||
getRunningProcesses().any { proc ->
|
||||
proc.name == steamVRProcName
|
||||
}
|
||||
HttpClient(CIO).use { client ->
|
||||
var scannedRunning = false
|
||||
var lastScan: TimeMark? = null
|
||||
|
||||
val drivers = if (running) {
|
||||
try {
|
||||
while (isActive) {
|
||||
val connected = server.context.state.value.drivers.isNotEmpty()
|
||||
|
||||
val drivers = try {
|
||||
getSteamVRDriversList(client)
|
||||
} catch (e: Exception) {
|
||||
AppLogger.checklist.warn(e, "Failed to get SteamVR drivers list")
|
||||
if (!e.isConnectionRefused()) {
|
||||
AppLogger.checklist.warn(e, "Failed to get SteamVR drivers list")
|
||||
}
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val driver = drivers?.firstOrNull {
|
||||
it.manifest.name == "slimevr"
|
||||
}
|
||||
val standableDriver = drivers?.firstOrNull {
|
||||
it.manifest.name == "standable"
|
||||
}
|
||||
val scan = lastScan
|
||||
if (drivers != null) {
|
||||
scannedRunning = false
|
||||
lastScan = null
|
||||
} else if (scan == null || scan.elapsedNow() >= PROCESS_SCAN_INTERVAL) {
|
||||
scannedRunning = getRunningProcesses().any { proc ->
|
||||
proc.name == steamVRProcName
|
||||
}
|
||||
lastScan = TimeSource.Monotonic.markNow()
|
||||
}
|
||||
|
||||
driverState.update {
|
||||
SteamVRDriverState(connected = connected, installed = !running || driver != null, driver?.blockedBySafeMode ?: false, enabled = driver?.enabled ?: true)
|
||||
val running = connected || drivers != null || scannedRunning
|
||||
|
||||
val driver = drivers?.firstOrNull {
|
||||
it.manifest.name == "slimevr"
|
||||
}
|
||||
val standableDriver = drivers?.firstOrNull {
|
||||
it.manifest.name == "standable"
|
||||
}
|
||||
|
||||
driverState.update {
|
||||
SteamVRDriverState(connected = connected, installed = !running || driver != null, driver?.blockedBySafeMode ?: false, enabled = driver?.enabled ?: true)
|
||||
}
|
||||
standableState.update {
|
||||
standableDriver != null
|
||||
}
|
||||
delay(3000)
|
||||
}
|
||||
standableState.update {
|
||||
standableDriver != null
|
||||
}
|
||||
delay(3000)
|
||||
}
|
||||
client.close()
|
||||
}
|
||||
|
||||
driverState
|
||||
|
||||
Reference in new issue
Block a user