mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 18:00:33 +08:00
Use CompletableJob instead of CompletableDeffered<Unit>
This commit is contained in:
parent
fb8829a035
commit
2f65e0f5ed
@ -1 +0,0 @@
|
|||||||
package net.mamoe.mirai.network
|
|
@ -1,13 +1,15 @@
|
|||||||
package net.mamoe.mirai.network
|
package net.mamoe.mirai.network
|
||||||
|
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableJob
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import net.mamoe.mirai.Bot
|
import net.mamoe.mirai.Bot
|
||||||
import net.mamoe.mirai.network.protocol.tim.handler.DataPacketSocket
|
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.handler.TemporaryPacketHandler
|
||||||
import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
|
import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
|
||||||
import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
|
import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
|
||||||
import net.mamoe.mirai.utils.getGTK
|
import net.mamoe.mirai.utils.getGTK
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
import kotlin.jvm.JvmSynthetic
|
import kotlin.jvm.JvmSynthetic
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,10 +64,10 @@ class LoginSession(
|
|||||||
* @param handlerTemporary 处理器.
|
* @param handlerTemporary 处理器.
|
||||||
*/
|
*/
|
||||||
//@JvmSynthetic
|
//@JvmSynthetic
|
||||||
suspend inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableDeferred<Unit> {
|
suspend inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableJob {
|
||||||
val deferred = CompletableDeferred<Unit>()
|
val job = coroutineContext[Job].takeIf { it != null }?.let { Job(it) } ?: Job()
|
||||||
this.bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this).also(handlerTemporary))
|
this.bot.network.addHandler(TemporaryPacketHandler(P::class, job, this).also(handlerTemporary))
|
||||||
return deferred
|
return job
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,13 +87,13 @@ class LoginSession(
|
|||||||
* @param handler 处理期待的包
|
* @param handler 处理期待的包
|
||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend inline fun <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableDeferred<Unit> {
|
suspend inline fun <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableJob {
|
||||||
val deferred = CompletableDeferred<Unit>()
|
val job = coroutineContext[Job].takeIf { it != null }?.let { Job(it) } ?: Job()
|
||||||
this.bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this).also {
|
this.bot.network.addHandler(TemporaryPacketHandler(P::class, job, this).also {
|
||||||
it.toSend(toSend)
|
it.toSend(toSend)
|
||||||
it.onExpect(handler)
|
it.onExpect(handler)
|
||||||
})
|
})
|
||||||
return deferred
|
return job
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +177,7 @@ internal class TIMBotNetworkHandler internal constructor(private val bot: Bot) :
|
|||||||
//coz removeIf is not inline
|
//coz removeIf is not inline
|
||||||
handlersLock.withLock {
|
handlersLock.withLock {
|
||||||
temporaryPacketHandlers.removeIfInlined {
|
temporaryPacketHandlers.removeIfInlined {
|
||||||
it.onPacketReceived(this@TIMBotNetworkHandler[ActionPacketHandler].session, packet)
|
it.shouldRemove(this@TIMBotNetworkHandler[ActionPacketHandler].session, packet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package net.mamoe.mirai.network.protocol.tim.handler
|
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.LoginSession
|
||||||
import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
|
import net.mamoe.mirai.network.protocol.tim.packet.ClientPacket
|
||||||
import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
|
import net.mamoe.mirai.network.protocol.tim.packet.ServerPacket
|
||||||
@ -21,7 +21,7 @@ import kotlin.reflect.KClass
|
|||||||
*/
|
*/
|
||||||
class TemporaryPacketHandler<P : ServerPacket>(
|
class TemporaryPacketHandler<P : ServerPacket>(
|
||||||
private val expectationClass: KClass<P>,
|
private val expectationClass: KClass<P>,
|
||||||
private val deferred: CompletableDeferred<Unit>,
|
private val deferred: CompletableJob,
|
||||||
private val fromSession: LoginSession
|
private val fromSession: LoginSession
|
||||||
) {
|
) {
|
||||||
private lateinit var toSend: ClientPacket
|
private lateinit var toSend: ClientPacket
|
||||||
@ -49,11 +49,12 @@ class TemporaryPacketHandler<P : ServerPacket>(
|
|||||||
session.socket.sendPacket(toSend)
|
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) {
|
if (expectationClass.isInstance(packet) && session === this.fromSession) {
|
||||||
@Suppress("UNCHECKED_CAST")
|
kotlin.runCatching {
|
||||||
expect(packet as P)
|
@Suppress("UNCHECKED_CAST")
|
||||||
deferred.complete(Unit)
|
expect(packet as P)
|
||||||
|
}.onFailure { deferred.completeExceptionally(it) }.onSuccess { deferred.complete() }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package net.mamoe.mirai.message
|
package net.mamoe.mirai.message
|
||||||
|
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableJob
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import net.mamoe.mirai.contact.Contact
|
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(imageFile: File) : this(imageFile.name, ImageIO.read(imageFile))
|
||||||
constructor(url: URL) : this(File(url.file))
|
constructor(url: URL) : this(File(url.file))
|
||||||
|
|
||||||
suspend fun upload(session: LoginSession, contact: Contact): CompletableDeferred<Unit> {
|
suspend fun upload(session: LoginSession, contact: Contact): CompletableJob {
|
||||||
return session.expectPacket<ServerTryGetImageIDResponsePacket> {
|
return session.expectPacket<ServerTryGetImageIDResponsePacket> {
|
||||||
toSend { ClientTryGetImageIDPacketJvm(session.bot.qqNumber, session.sessionKey, contact.number, image) }
|
toSend { ClientTryGetImageIDPacketJvm(session.bot.qqNumber, session.sessionKey, contact.number, image) }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user