mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-27 04:16:32 -08:00
Fix address resolvig / avoid dns lookup
This commit is contained in:
1 parent
feb3eae3d3
commit
5a4ba1f73b
5 files changed
+29
-30
No files matched your search
@@ -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) },
|
||||
|
||||
@@ -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<TrackerIdNum>,
|
||||
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<PacketEvent<UDPPacket>> { it.data::class }
|
||||
|
||||
@@ -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<UdpServerState, UdpServerActions>
|
||||
@@ -30,12 +30,12 @@ typealias UdpServerBehaviour = Behaviour<UdpServerState, UdpServerActions, UdpSe
|
||||
|
||||
object UdpServerBaseBehaviour : UdpServerBehaviour {
|
||||
override fun reduce(state: UdpServerState, action: UdpServerActions) = when (action) {
|
||||
is UdpServerActions.ConnectionAdded -> 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
|
||||
}
|
||||
|
||||
@@ -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<String>) = runBlocking<Unit> {
|
||||
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(
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user