diff --git a/server/core/src/main/java/dev/slimevr/udp/behaviours.kt b/server/core/src/main/java/dev/slimevr/udp/behaviours.kt index a7f2a0508..e835b559c 100644 --- a/server/core/src/main/java/dev/slimevr/udp/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/udp/behaviours.kt @@ -213,7 +213,7 @@ object TimeoutBehaviour : UDPConnectionBehaviour { } val timeUntilTimeout = CONNECTION_TIMEOUT_MS - (System.currentTimeMillis() - state.lastPacket) if (timeUntilTimeout <= 0) { - AppLogger.udp.info("[${state.address}] Connection timed out for ${state.id}") + AppLogger.udp.info("[${state.address}] Connection timed out") receiver.context.dispatch(UDPConnectionActions.TimedOut) receiver.getDevice()?.context?.dispatch( DeviceActions.Update { copy(status = TrackerStatus.TIMED_OUT) }, diff --git a/server/core/src/main/java/dev/slimevr/udp/connection.kt b/server/core/src/main/java/dev/slimevr/udp/connection.kt index 04ca8ae13..126b41db2 100644 --- a/server/core/src/main/java/dev/slimevr/udp/connection.kt +++ b/server/core/src/main/java/dev/slimevr/udp/connection.kt @@ -27,13 +27,14 @@ data class SensorConfigFlags( ) data class UDPConnectionState( - val id: String, + // Pre-resolved IP string. InetSocketAddress.hostname triggers a reverse DNS lookup so we + // can't derive this on demand — it must be resolved once by the platform (see UdpServer.addressResolver) + // and cached here. remoteAddress is kept separately on UDPConnection only for sending datagrams. + val address: String, val lastPacket: Long, val lastPacketNum: Long, val lastPing: LastPing, val didHandshake: Boolean, - val address: String, - val port: Int, val deviceId: Int?, val trackerIds: List, val features: FirmwareFeatures?, @@ -84,7 +85,7 @@ class UDPConnection( companion object { fun create( - id: String, + address: String, socket: BoundDatagramSocket, remoteAddress: InetSocketAddress, appContext: AppContextProvider, @@ -109,13 +110,11 @@ class UDPConnection( val context = Context.create( initialState = UDPConnectionState( - id = id, + address = address, lastPacket = System.currentTimeMillis(), lastPacketNum = 0, lastPing = LastPing(id = 0, startTime = 0), didHandshake = false, - address = remoteAddress.hostname, - port = remoteAddress.port, deviceId = null, trackerIds = listOf(), features = null, @@ -123,7 +122,7 @@ class UDPConnection( ), scope = scope, behaviours = behaviours, - name = "UDPConnection[$id]", + name = "UDPConnection[$address]", ) val dispatcher = EventDispatcher> { it.data::class } diff --git a/server/core/src/main/java/dev/slimevr/udp/server.kt b/server/core/src/main/java/dev/slimevr/udp/server.kt index 5cb06e4df..2496d1e2d 100644 --- a/server/core/src/main/java/dev/slimevr/udp/server.kt +++ b/server/core/src/main/java/dev/slimevr/udp/server.kt @@ -21,8 +21,8 @@ data class UdpServerState( ) sealed interface UdpServerActions { - data class ConnectionAdded(val ip: String, val conn: UDPConnection) : UdpServerActions - data class ConnectionRemoved(val ip: String) : UdpServerActions + data class ConnectionAdded(val address: String, val conn: UDPConnection) : UdpServerActions + data class ConnectionRemoved(val address: String) : UdpServerActions } typealias UdpServerContext = Context @@ -30,12 +30,12 @@ typealias UdpServerBehaviour = Behaviour state.copy(connections = state.connections + (action.ip to action.conn)) - is UdpServerActions.ConnectionRemoved -> state.copy(connections = state.connections - action.ip) + is UdpServerActions.ConnectionAdded -> state.copy(connections = state.connections + (action.address to action.conn)) + is UdpServerActions.ConnectionRemoved -> state.copy(connections = state.connections - action.address) } } -class UdpServer(val context: UdpServerContext) { +class UdpServer(val context: UdpServerContext, private val addressResolver: (InetSocketAddress) -> String) { private var receiveJob: Job? = null private var socket: BoundDatagramSocket? = null private var selectorManager: SelectorManager? = null @@ -66,8 +66,8 @@ class UdpServer(val context: UdpServerContext) { val packetData = readPacket(type, src) val remoteAddress = recvPacket.address as? InetSocketAddress ?: return@measureTime - val id = addressKey(remoteAddress) - val conn = context.state.value.connections[id] + val address = addressResolver(remoteAddress) + val conn = context.state.value.connections[address] val event = PacketEvent(data = packetData, packetNumber = packetNumber) @@ -75,13 +75,13 @@ class UdpServer(val context: UdpServerContext) { conn.packetChannel.trySend(event) } else { val newConn = UDPConnection.create( - id = id, + address = address, remoteAddress = remoteAddress, socket = socket, appContext = appContext, scope = scope, ) - context.dispatch(UdpServerActions.ConnectionAdded(id, newConn)) + context.dispatch(UdpServerActions.ConnectionAdded(address, newConn)) newConn.packetChannel.trySend(event) } } @@ -112,23 +112,14 @@ class UdpServer(val context: UdpServerContext) { companion object { val INITIAL_STATE = UdpServerState(connections = emptyMap()) - private fun addressKey(address: InetSocketAddress): String { - val raw = address.resolveAddress() - return if (raw != null && raw.size == 4) { - raw.joinToString(".") { byte -> (byte.toInt() and 0xFF).toString() } - } else { - address.hostname - } - } - - fun create(scope: CoroutineScope): UdpServer { + fun create(scope: CoroutineScope, addressResolver: (InetSocketAddress) -> String): UdpServer { val context = Context.create( initialState = INITIAL_STATE, scope = scope, behaviours = listOf(UdpServerBaseBehaviour), name = "UdpServer", ) - val server = UdpServer(context) + val server = UdpServer(context, addressResolver) server.startObserving() return server } diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt index 47d02bb7b..221c95337 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt @@ -26,6 +26,7 @@ import dev.slimevr.provisioning.ProvisioningManager import dev.slimevr.resolveConfigDirectory import dev.slimevr.skeleton.Skeleton import dev.slimevr.trackingchecklist.TrackingChecklist +import dev.slimevr.desktop.udp.resolveDesktopUdpAddress import dev.slimevr.udp.UdpServer import dev.slimevr.util.safeLaunch import dev.slimevr.vmc.VMCManager @@ -52,7 +53,7 @@ fun main(args: Array) = runBlocking { val provisioningManager = ProvisioningManager.create(ctx = phase1, scope = this) val heightCalibrationManager = HeightCalibrationManager.create(ctx = phase1, scope = this) val trackingChecklist = TrackingChecklist.create(scope = this) - val udpServer = UdpServer.create(scope = this) + val udpServer = UdpServer.create(scope = this, addressResolver = ::resolveDesktopUdpAddress) val bvhManager = BVHManager.create(skeleton = skeleton, storage = storage, scope = this) val vmcManager = VMCManager.create(skeleton = skeleton, ctx = phase1, scope = this) val vrcOscManager = VRCOSCManager.create( diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/udp/address.kt b/server/desktop/src/main/java/dev/slimevr/desktop/udp/address.kt new file mode 100644 index 000000000..fafb58049 --- /dev/null +++ b/server/desktop/src/main/java/dev/slimevr/desktop/udp/address.kt @@ -0,0 +1,8 @@ +package dev.slimevr.desktop.udp + +import io.ktor.network.sockets.InetSocketAddress +import io.ktor.network.sockets.toJavaAddress + +// Prevent DNS lookup when getting the address +fun resolveDesktopUdpAddress(addr: InetSocketAddress): String = + (addr.toJavaAddress() as java.net.InetSocketAddress).hostString