From 2f65e0f5ed0f5041a2d8c49ca6d7e02bcd8af4db Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 15 Oct 2019 19:15:54 +0800 Subject: [PATCH] Use CompletableJob instead of CompletableDeffered --- .../network/HandlerClosedException.kt | 1 - .../net.mamoe.mirai/network/LoginSession.kt | 20 ++++++++++--------- .../protocol/tim/TIMBotNetworkHandler.kt | 2 +- .../tim/handler/TemporaryPacketHandler.kt | 13 ++++++------ .../net/mamoe/mirai/message/UnsolvedImage.kt | 4 ++-- 5 files changed, 21 insertions(+), 19 deletions(-) delete mode 100644 mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/HandlerClosedException.kt diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/HandlerClosedException.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/HandlerClosedException.kt deleted file mode 100644 index 66c2f82bf..000000000 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/HandlerClosedException.kt +++ /dev/null @@ -1 +0,0 @@ -package net.mamoe.mirai.network diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/LoginSession.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/LoginSession.kt index 8916e070c..78f39e9df 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/LoginSession.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/LoginSession.kt @@ -1,13 +1,15 @@ package net.mamoe.mirai.network -import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CompletableJob import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job import net.mamoe.mirai.Bot import net.mamoe.mirai.network.protocol.tim.handler.DataPacketSocket import net.mamoe.mirai.network.protocol.tim.handler.TemporaryPacketHandler import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket import net.mamoe.mirai.utils.getGTK +import kotlin.coroutines.coroutineContext import kotlin.jvm.JvmSynthetic /** @@ -62,10 +64,10 @@ class LoginSession( * @param handlerTemporary 处理器. */ //@JvmSynthetic - suspend inline fun expectPacket(handlerTemporary: TemporaryPacketHandler

.() -> Unit): CompletableDeferred { - val deferred = CompletableDeferred() - this.bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this).also(handlerTemporary)) - return deferred + suspend inline fun expectPacket(handlerTemporary: TemporaryPacketHandler

.() -> Unit): CompletableJob { + val job = coroutineContext[Job].takeIf { it != null }?.let { Job(it) } ?: Job() + this.bot.network.addHandler(TemporaryPacketHandler(P::class, job, this).also(handlerTemporary)) + return job } /** @@ -85,13 +87,13 @@ class LoginSession( * @param handler 处理期待的包 */ @JvmSynthetic - suspend inline fun expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableDeferred { - val deferred = CompletableDeferred() - this.bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this).also { + suspend inline fun expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableJob { + val job = coroutineContext[Job].takeIf { it != null }?.let { Job(it) } ?: Job() + this.bot.network.addHandler(TemporaryPacketHandler(P::class, job, this).also { it.toSend(toSend) it.onExpect(handler) }) - return deferred + return job } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt index 72fdf1088..0c3e39871 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt @@ -177,7 +177,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) : //coz removeIf is not inline handlersLock.withLock { temporaryPacketHandlers.removeIfInlined { - it.onPacketReceived(this@TIMBotNetworkHandler[ActionPacketHandler].session, packet) + it.shouldRemove(this@TIMBotNetworkHandler[ActionPacketHandler].session, packet) } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt index 6c7624717..2a5fee698 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/TemporaryPacketHandler.kt @@ -1,6 +1,6 @@ package net.mamoe.mirai.network.protocol.tim.handler -import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CompletableJob import net.mamoe.mirai.network.LoginSession import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket @@ -21,7 +21,7 @@ import kotlin.reflect.KClass */ class TemporaryPacketHandler

( private val expectationClass: KClass

, - private val deferred: CompletableDeferred, + private val deferred: CompletableJob, private val fromSession: LoginSession ) { private lateinit var toSend: ClientPacket @@ -49,11 +49,12 @@ class TemporaryPacketHandler

( session.socket.sendPacket(toSend) } - suspend fun onPacketReceived(session: LoginSession, packet: ServerPacket): Boolean { + suspend fun shouldRemove(session: LoginSession, packet: ServerPacket): Boolean { if (expectationClass.isInstance(packet) && session === this.fromSession) { - @Suppress("UNCHECKED_CAST") - expect(packet as P) - deferred.complete(Unit) + kotlin.runCatching { + @Suppress("UNCHECKED_CAST") + expect(packet as P) + }.onFailure { deferred.completeExceptionally(it) }.onSuccess { deferred.complete() } return true } return false diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt index c3d99c6d7..846506fc0 100644 --- a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/message/UnsolvedImage.kt @@ -1,6 +1,6 @@ package net.mamoe.mirai.message -import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CompletableJob import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import net.mamoe.mirai.contact.Contact @@ -30,7 +30,7 @@ class UnsolvedImage(private val filename: String, val image: BufferedImage) { constructor(imageFile: File) : this(imageFile.name, ImageIO.read(imageFile)) constructor(url: URL) : this(File(url.file)) - suspend fun upload(session: LoginSession, contact: Contact): CompletableDeferred { + suspend fun upload(session: LoginSession, contact: Contact): CompletableJob { return session.expectPacket { toSend { ClientTryGetImageIDPacketJvm(session.bot.qqNumber, session.sessionKey, contact.number, image) }