diff --git a/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt b/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt index de92dc153..16832ae0a 100644 --- a/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt +++ b/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt @@ -46,6 +46,12 @@ internal abstract class AbstractKeepAliveNetworkHandlerSelector { */ fun getResumedInstance(): H? + /** + * Returns the currently alive [NetworkHandler] or creates a new one. + */ + fun tryResumeInstanceOrCreate(): H + /** * Returns an alive [NetworkHandler], or suspends the coroutine until the connection has been made again. * diff --git a/mirai-core/src/commonMain/kotlin/network/handler/selector/SelectorNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/handler/selector/SelectorNetworkHandler.kt index c88aacdf1..d660eaab8 100644 --- a/mirai-core/src/commonMain/kotlin/network/handler/selector/SelectorNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/handler/selector/SelectorNetworkHandler.kt @@ -10,7 +10,7 @@ package net.mamoe.mirai.internal.network.handler.selector import kotlinx.coroutines.* -import kotlinx.coroutines.selects.SelectClause1 +import kotlinx.coroutines.channels.ReceiveChannel import net.mamoe.mirai.internal.network.handler.NetworkHandler import net.mamoe.mirai.internal.network.handler.NetworkHandler.State import net.mamoe.mirai.internal.network.handler.NetworkHandlerContext @@ -44,10 +44,9 @@ internal class SelectorNetworkHandler( } override val state: State - get() = selector.getResumedInstance()?.state ?: State.INITIALIZED - override val onStateChanged: SelectClause1 - get() = selector.getResumedInstance()?.onStateChanged - ?: scope.async { instance().state }.onAwait + get() = selector.tryResumeInstanceOrCreate().state + override val stateChannel: ReceiveChannel + get() = selector.tryResumeInstanceOrCreate().stateChannel override suspend fun resumeConnection() { instance() // the selector will resume connection for us. diff --git a/mirai-core/src/commonTest/kotlin/network/framework/TestNetworkHandler.kt b/mirai-core/src/commonTest/kotlin/network/framework/TestNetworkHandler.kt index ddef09d81..fdaf91889 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/TestNetworkHandler.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/TestNetworkHandler.kt @@ -47,12 +47,16 @@ internal open class TestNetworkHandler( } } } + + override fun toString(): String { + return "TestState($correspondingState)" + } } @OptIn(TestOnly::class) - fun setState(correspondingState: NetworkHandler.State) { + fun setState(correspondingState: NetworkHandler.State): TestState? { // `null` means ignore checks. All test states have same type TestState. - setStateImpl(null) { TestState(correspondingState) } + return setStateImpl(null) { TestState(correspondingState) } } private val initialState = TestState(NetworkHandler.State.INITIALIZED)