mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-08 09:10:11 +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
|
||||
|
||||
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 <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableDeferred<Unit> {
|
||||
val deferred = CompletableDeferred<Unit>()
|
||||
this.bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this).also(handlerTemporary))
|
||||
return deferred
|
||||
suspend inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> 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 <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableDeferred<Unit> {
|
||||
val deferred = CompletableDeferred<Unit>()
|
||||
this.bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this).also {
|
||||
suspend inline fun <reified P : ServerPacket> 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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<P : ServerPacket>(
|
||||
private val expectationClass: KClass<P>,
|
||||
private val deferred: CompletableDeferred<Unit>,
|
||||
private val deferred: CompletableJob,
|
||||
private val fromSession: LoginSession
|
||||
) {
|
||||
private lateinit var toSend: ClientPacket
|
||||
@ -49,11 +49,12 @@ class TemporaryPacketHandler<P : ServerPacket>(
|
||||
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
|
||||
|
@ -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<Unit> {
|
||||
suspend fun upload(session: LoginSession, contact: Contact): CompletableJob {
|
||||
return session.expectPacket<ServerTryGetImageIDResponsePacket> {
|
||||
toSend { ClientTryGetImageIDPacketJvm(session.bot.qqNumber, session.sessionKey, contact.number, image) }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user