Make close not suspend

This commit is contained in:
Him188 2019-12-06 12:45:34 +08:00
parent 785ddd735d
commit 770865caae
2 changed files with 4 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import net.mamoe.mirai.network.protocol.tim.packet.Packet
import net.mamoe.mirai.network.protocol.tim.packet.login.HeartbeatPacket import net.mamoe.mirai.network.protocol.tim.packet.login.HeartbeatPacket
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult
import net.mamoe.mirai.network.protocol.tim.packet.login.RequestSKeyPacket import net.mamoe.mirai.network.protocol.tim.packet.login.RequestSKeyPacket
import net.mamoe.mirai.utils.MiraiInternalAPI
import net.mamoe.mirai.utils.io.PlatformDatagramChannel import net.mamoe.mirai.utils.io.PlatformDatagramChannel
/** /**
@ -52,6 +53,7 @@ interface BotNetworkHandler<Socket : DataPacketSocketAdapter> : CoroutineScope {
* @see [BotSession.sendAndExpectAsync] 发送并期待一个包 * @see [BotSession.sendAndExpectAsync] 发送并期待一个包
* @see [TemporaryPacketHandler] 临时包处理器 * @see [TemporaryPacketHandler] 临时包处理器
*/ */
@MiraiInternalAPI
suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>) suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>)
/** /**
@ -67,7 +69,7 @@ interface BotNetworkHandler<Socket : DataPacketSocketAdapter> : CoroutineScope {
/** /**
* 关闭网络接口, 停止所有有关协程和任务 * 关闭网络接口, 停止所有有关协程和任务
*/ */
suspend fun close(cause: Throwable? = null) { fun close(cause: Throwable? = null) {
supervisor.cancel(CancellationException("handler closed", cause)) supervisor.cancel(CancellationException("handler closed", cause))
} }
} }

View File

@ -101,16 +101,14 @@ internal class TIMBotNetworkHandler internal constructor(coroutineContext: Corou
heartbeatJob?.join() heartbeatJob?.join()
} }
override suspend fun close(cause: Throwable?) { override fun close(cause: Throwable?) {
super.close(cause) super.close(cause)
this.heartbeatJob?.cancel(CancellationException("handler closed")) this.heartbeatJob?.cancel(CancellationException("handler closed"))
this.heartbeatJob?.join()//等待 cancel 完成
this.heartbeatJob = null this.heartbeatJob = null
if (!this.loginResult.isCompleted && !this.loginResult.isCancelled) { if (!this.loginResult.isCompleted && !this.loginResult.isCancelled) {
this.loginResult.cancel(CancellationException("socket closed")) this.loginResult.cancel(CancellationException("socket closed"))
this.loginResult.join()
} }
this.socket.close() this.socket.close()