diff --git a/server/core/src/main/java/dev/slimevr/solarxr/solarxr.kt b/server/core/src/main/java/dev/slimevr/solarxr/solarxr.kt index b8817736a..857d7a3a9 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/solarxr.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/solarxr.kt @@ -6,7 +6,6 @@ import dev.slimevr.context.CustomModule import dev.slimevr.context.createContext import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job -import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import solarxr_protocol.data_feed.DataFeedConfig @@ -35,19 +34,14 @@ class PacketDispatcher { @Suppress("UNCHECKED_CAST") inline fun on(crossinline callback: suspend (P) -> Unit) { - runBlocking { - mutex.withLock { - val list = - listeners.getOrPut(P::class as KClass) { mutableListOf() } - list.add { callback(it as P) } - } + synchronized(this) { + listeners.getOrPut(P::class as KClass) { mutableListOf() } + .add { callback(it as P) } } } fun onAny(callback: suspend (T) -> Unit) { - runBlocking { - mutex.withLock { globalListeners.add(callback) } - } + synchronized(this) { globalListeners.add(callback) } } suspend fun emit(event: T) { diff --git a/server/core/src/main/java/dev/slimevr/vrserver.kt b/server/core/src/main/java/dev/slimevr/vrserver.kt index 503b9597b..18e98301d 100644 --- a/server/core/src/main/java/dev/slimevr/vrserver.kt +++ b/server/core/src/main/java/dev/slimevr/vrserver.kt @@ -9,9 +9,9 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import java.util.concurrent.atomic.AtomicInteger data class VRServerState( - val handleId: Int, val trackers: Map, val devices: Map, ) @@ -27,15 +27,8 @@ typealias VRServerModule = BasicModule val BaseModule = VRServerModule( reducer = { s, a -> when (a) { - is VRServerActions.NewTracker -> s.copy( - trackers = s.trackers + (a.trackerId to a.context), - handleId = a.trackerId, - ) - - is VRServerActions.NewDevice -> s.copy( - devices = s.devices + (a.deviceId to a.context), - handleId = a.deviceId, - ) + is VRServerActions.NewTracker -> s.copy(trackers = s.trackers + (a.trackerId to a.context)) + is VRServerActions.NewDevice -> s.copy(devices = s.devices + (a.deviceId to a.context)) } }, observer = { context -> @@ -47,15 +40,16 @@ val BaseModule = VRServerModule( data class VRServer( val context: VRServerContext, + // Moved this outside of the context to make this faster and safer to use + private val handleCounter: AtomicInteger, ) { - fun nextHandle() = context.state.value.handleId + 1 + fun nextHandle() = handleCounter.incrementAndGet() fun getTracker(id: Int) = context.state.value.trackers[id] fun getDevice(id: Int) = context.state.value.devices[id] companion object { fun create(scope: CoroutineScope): VRServer { val server = VRServerState( - handleId = 0, trackers = mapOf(), devices = mapOf(), ) @@ -72,6 +66,7 @@ data class VRServer( return VRServer( context = context, + handleCounter = AtomicInteger(0), ) } } diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/ipc/protocol.kt b/server/desktop/src/main/java/dev/slimevr/desktop/ipc/protocol.kt index 04e6c1c57..fedb11bb0 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/ipc/protocol.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/ipc/protocol.kt @@ -15,6 +15,9 @@ import dev.slimevr.tracker.createTracker import io.github.axisangles.ktmath.Quaternion import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.drop +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.runningFold import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -36,6 +39,8 @@ suspend fun handleDriverConnection( sendMsg(ProtobufMessage(version = Version(protocol_version = PROTOCOL_VERSION))) + // Should be safe, only accessed inside state.collect below. StateFlow never delivers + // two emissions concurrently to the same collector. val subscribedTrackers = mutableSetOf() val observerJob = launch { @@ -114,7 +119,7 @@ suspend fun handleFeederConnection( scope = this, id = trackerId, deviceId = deviceId, - sensorType = ImuType.MPU9250, + sensorType = ImuType.MPU9250, // TODO: prob need to make sensor type optional hardwareId = msg.tracker_added.tracker_serial, origin = DeviceOrigin.FEEDER, serverContext = server, @@ -132,6 +137,7 @@ suspend fun handleFeederConnection( y = msg.position.qy, z = msg.position.qz, ), + // TODO: add position data ) }, ) @@ -153,4 +159,4 @@ suspend fun handleSolarXRConnection( messages.collect { bytes -> onSolarXRMessage(ByteBuffer.wrap(bytes), connection) } -} \ No newline at end of file +}