From 2b02121b66b6ac2a1a2c270e2667da7824c654eb Mon Sep 17 00:00:00 2001 From: Him188moe Date: Sun, 22 Sep 2019 20:04:43 +0800 Subject: [PATCH] Kotlin coroutine --- .../java/net/mamoe/mirai/contact/Contact.kt | 2 +- .../net/mamoe/mirai/event/MiraiEventHookKt.kt | 17 +++++++--- .../mamoe/mirai/event/MiraiEventManagerKt.kt | 31 +++++++++++++------ .../mirai/message/defaults/UnsolvedImage.kt | 2 +- .../mirai/network/BotNetworkHandlerImpl.kt | 20 +++++++++--- .../net/mamoe/mirai/network/NetworkScope.kt | 19 ++---------- 6 files changed, 53 insertions(+), 38 deletions(-) diff --git a/mirai-core/src/main/java/net/mamoe/mirai/contact/Contact.kt b/mirai-core/src/main/java/net/mamoe/mirai/contact/Contact.kt index 0299a42d6..dcbb16107 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/contact/Contact.kt +++ b/mirai-core/src/main/java/net/mamoe/mirai/contact/Contact.kt @@ -23,7 +23,7 @@ abstract class Contact internal constructor(val bot: Bot, val number: Long) { /** * 上传图片 */ - suspend fun uploadImage(session: LoginSession, image: UnsolvedImage): CompletableFuture { + fun uploadImage(session: LoginSession, image: UnsolvedImage): CompletableFuture { return image.upload(session, this) } diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHookKt.kt b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHookKt.kt index 4a072a2be..f6b9d45cd 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHookKt.kt +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHookKt.kt @@ -1,5 +1,6 @@ package net.mamoe.mirai.event +import kotlinx.coroutines.runBlocking import java.util.function.Consumer import java.util.function.Predicate @@ -7,12 +8,20 @@ import java.util.function.Predicate * @author Him188moe */ class MiraiEventHookKt(eventClass: Class) : MiraiEventHook(eventClass) { - fun onEvent(handler: (E) -> Unit) { - this@MiraiEventHookKt.handler = Consumer(handler) + fun onEvent(handler: suspend (E) -> Unit) { + this@MiraiEventHookKt.handler = Consumer { + runBlocking { + handler(it) + } + } } - fun validChecker(predicate: (E) -> Boolean) { - this@MiraiEventHookKt.validChecker = Predicate(predicate) + fun validChecker(predicate: suspend (E) -> Boolean) {//todo 把 mirai event 变为 suspend, 而不是在这里 run blocking + this@MiraiEventHookKt.validChecker = Predicate { + runBlocking { + predicate(it) + } + } } } diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManagerKt.kt b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManagerKt.kt index 8b1a364f5..8acbb81c6 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManagerKt.kt +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManagerKt.kt @@ -2,6 +2,7 @@ package net.mamoe.mirai.event +import kotlinx.coroutines.runBlocking import kotlin.reflect.KClass /** @@ -18,21 +19,29 @@ object EventManager : MiraiEventManager() /** * 每次事件触发时都会调用 hook */ -fun , E : MiraiEvent> C.hookAlways(hook: (E) -> Unit) { - MiraiEventManager.getInstance().hookAlways(MiraiEventHook(this, hook)) +fun , E : MiraiEvent> C.hookAlways(hook: suspend (E) -> Unit) { + MiraiEventManager.getInstance().hookAlways(MiraiEventHook(this) { + runBlocking { + hook(it) + } + }) } /** * 当下一次事件触发时调用 hook */ -fun , E : MiraiEvent> C.hookOnce(hook: (E) -> Unit) { - MiraiEventManager.getInstance().hookOnce(MiraiEventHook(this, hook)) +fun , E : MiraiEvent> C.hookOnce(hook: suspend (E) -> Unit) { + MiraiEventManager.getInstance().hookOnce(MiraiEventHook(this) { + runBlocking { + hook(it) + } + }) } /** * 每次事件触发时都会调用 hook, 直到 hook 返回 false 时停止 hook */ -fun , E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) { +fun , E : MiraiEvent> C.hookWhile(hook: suspend (E) -> Boolean) { MiraiEventManager.getInstance().hookAlways(MiraiEventHookSimple(this, hook)) } @@ -40,28 +49,30 @@ fun , E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) { /** * 每次事件触发时都会调用 hook */ -fun , E : MiraiEvent> C.hookAlways(hook: (E) -> Unit) { +fun , E : MiraiEvent> C.hookAlways(hook: suspend (E) -> Unit) { this.java.hookAlways(hook) } /** * 当下一次事件触发时调用 hook */ -fun , E : MiraiEvent> C.hookOnce(hook: (E) -> Unit) { +fun , E : MiraiEvent> C.hookOnce(hook: suspend (E) -> Unit) { this.java.hookOnce(hook) } /** * 每次事件触发时都会调用 hook, 直到 hook 返回 false 时停止 hook */ -fun , E : MiraiEvent> C.hookWhile(hook: (E) -> Boolean) { +fun , E : MiraiEvent> C.hookWhile(hook: suspend (E) -> Boolean) { this.java.hookWhile(hook) } -private class MiraiEventHookSimple(clazz: Class, val hook: (E) -> Boolean) : MiraiEventHook(clazz) { +private class MiraiEventHookSimple(clazz: Class, val hook: suspend (E) -> Boolean) : MiraiEventHook(clazz) { override fun accept(event: MiraiEvent?): Boolean { @Suppress("UNCHECKED_CAST") - return !hook.invoke(event as E) + return runBlocking { + return@runBlocking !hook.invoke(event as E) + } } } \ No newline at end of file diff --git a/mirai-core/src/main/java/net/mamoe/mirai/message/defaults/UnsolvedImage.kt b/mirai-core/src/main/java/net/mamoe/mirai/message/defaults/UnsolvedImage.kt index 16fd75eb5..b3cf226b9 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/message/defaults/UnsolvedImage.kt +++ b/mirai-core/src/main/java/net/mamoe/mirai/message/defaults/UnsolvedImage.kt @@ -26,7 +26,7 @@ class UnsolvedImage(filename: String, val image: BufferedImage) : Image(getImage constructor(imageFile: File) : this(imageFile.name, ImageIO.read(imageFile)) constructor(url: URL) : this(File(url.file)) - suspend fun upload(session: LoginSession, contact: Contact): CompletableFuture {//todo be suspend + fun upload(session: LoginSession, contact: Contact): CompletableFuture { return session.expectPacket { toSend { ClientTryGetImageIDPacket(session.bot.account.qqNumber, session.sessionKey, contact.number, image) } diff --git a/mirai-core/src/main/java/net/mamoe/mirai/network/BotNetworkHandlerImpl.kt b/mirai-core/src/main/java/net/mamoe/mirai/network/BotNetworkHandlerImpl.kt index 59a767623..5778777f1 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/network/BotNetworkHandlerImpl.kt +++ b/mirai-core/src/main/java/net/mamoe/mirai/network/BotNetworkHandlerImpl.kt @@ -117,8 +117,10 @@ internal class BotNetworkHandlerImpl(private val bot: Bot) : BotNetworkHandler { if (!packet.javaClass.name.endsWith("Encrypted") && !packet.javaClass.name.endsWith("Raw")) { bot.notice("Packet received: $packet") } + } - if (packet is ServerEventPacket) { + if (packet is ServerEventPacket) { + NetworkScope.launch { sendPacket(ClientEventResponsePacket(bot.account.qqNumber, packet.packetId, sessionKey, packet.eventIdentity)) } } @@ -127,10 +129,18 @@ internal class BotNetworkHandlerImpl(private val bot: Bot) : BotNetworkHandler { return } - login.onPacketReceived(packet) - packetHandlers.forEach { - it.instance.onPacketReceived(packet) - } + withContext(NetworkScope.coroutineContext) { + launch { + login.onPacketReceived(packet) + } + + + packetHandlers.forEach { + launch { + it.instance.onPacketReceived(packet) + } + } + }//awaits all coroutines launched in this block } private var socket: DatagramSocket? = null diff --git a/mirai-core/src/main/java/net/mamoe/mirai/network/NetworkScope.kt b/mirai-core/src/main/java/net/mamoe/mirai/network/NetworkScope.kt index 3e537ce18..735ab1886 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/network/NetworkScope.kt +++ b/mirai-core/src/main/java/net/mamoe/mirai/network/NetworkScope.kt @@ -1,21 +1,6 @@ package net.mamoe.mirai.network import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.asCoroutineDispatcher -import java.util.concurrent.SynchronousQueue -import java.util.concurrent.ThreadPoolExecutor -import java.util.concurrent.TimeUnit -import kotlin.coroutines.CoroutineContext +import kotlinx.coroutines.Dispatchers -object NetworkScope : CoroutineScope { - override val coroutineContext: CoroutineContext - get() = lazy { - ThreadPoolExecutor( - 1, - 4, - 8000, TimeUnit.MILLISECONDS, - SynchronousQueue() - ).asCoroutineDispatcher() - }.value//todo improve - -} \ No newline at end of file +object NetworkScope : CoroutineScope by CoroutineScope(Dispatchers.Default) \ No newline at end of file