diff --git a/src/dorkbox/network/rmi/ResponseManager.kt b/src/dorkbox/network/rmi/ResponseManager.kt index 5d55fde8..aa74e3bf 100644 --- a/src/dorkbox/network/rmi/ResponseManager.kt +++ b/src/dorkbox/network/rmi/ResponseManager.kt @@ -35,7 +35,6 @@ import kotlin.concurrent.write internal class ResponseManager(private val logger: KLogger, private val actionDispatch: CoroutineScope) { companion object { val TIMEOUT_EXCEPTION = Exception() - val ASYNC_WAITER = ResponseWaiter(RemoteObjectStorage.ASYNC_RMI) // this is never waited on, we just need this to optimize how we assigned waiters. } @@ -109,24 +108,20 @@ internal class ResponseManager(private val logger: KLogger, private val actionDi * * We ONLY care about the ID to get the correct response info. If there is no response, the ID can be ignored. */ - internal suspend fun prep(isAsync: Boolean): ResponseWaiter { - return if (isAsync) { - ASYNC_WAITER - } else { - val responseRmi = waiterCache.receive() - rmiWaitersInUse.getAndIncrement() - logger.trace { "RMI count: ${rmiWaitersInUse.value}" } + internal suspend fun prep(): ResponseWaiter { + val responseRmi = waiterCache.receive() + rmiWaitersInUse.getAndIncrement() + logger.trace { "RMI count: ${rmiWaitersInUse.value}" } - // this will replace the waiter if it was cancelled (waiters are not valid if cancelled) - responseRmi.prep() + // this will replace the waiter if it was cancelled (waiters are not valid if cancelled) + responseRmi.prep() - pendingLock.write { - // this just does a .toUShort().toInt() conversion. This is cleaner than doing it manually - pending[RmiUtils.unpackUnsignedRight(responseRmi.id)] = responseRmi - } - - responseRmi + pendingLock.write { + // this just does a .toUShort().toInt() conversion. This is cleaner than doing it manually + pending[RmiUtils.unpackUnsignedRight(responseRmi.id)] = responseRmi } + + return responseRmi } /** diff --git a/src/dorkbox/network/rmi/RmiClient.kt b/src/dorkbox/network/rmi/RmiClient.kt index 50d0d24d..eabdd3bb 100644 --- a/src/dorkbox/network/rmi/RmiClient.kt +++ b/src/dorkbox/network/rmi/RmiClient.kt @@ -99,22 +99,23 @@ internal class RmiClient(val isGlobal: Boolean, // 'timeout > 0' -> WAIT w/ TIMEOUT // 'timeout == 0' -> WAIT FOREVER - - // If we are async, we ignore the response.... - // The response, even if there is NOT one (ie: not void) will always return a thing (so our code excution is in lockstep - val rmiWaiter = responseManager.prep(isAsync) - invokeMethod.isGlobal = isGlobal - invokeMethod.packedId = RmiUtils.packShorts(rmiObjectId, rmiWaiter.id) + if (isAsync) { + // If we are async, we ignore the response.... - connection.send(invokeMethod) + invokeMethod.packedId = RmiUtils.packShorts(rmiObjectId, RemoteObjectStorage.ASYNC_RMI) - // if we are async, then this will immediately return - return if (isAsync) { - null + connection.send(invokeMethod) + return null } else { - responseManager.waitForReply(rmiWaiter, timeoutMillis) + // The response, even if there is NOT one (ie: not void) will always return a thing (so our code execution is in lockstep + val rmiWaiter = responseManager.prep() + invokeMethod.packedId = RmiUtils.packShorts(rmiObjectId, rmiWaiter.id) + + connection.send(invokeMethod) + + return responseManager.waitForReply(rmiWaiter, timeoutMillis) } }