Improved temporary packet handlers

This commit is contained in:
Him188 2019-09-30 03:24:53 +08:00
parent 738c76752f
commit d1cc2710ec
5 changed files with 28 additions and 19 deletions

View File

@ -1,5 +1,7 @@
package net.mamoe.mirai.message.defaults package net.mamoe.mirai.message.defaults
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.network.LoginSession import net.mamoe.mirai.network.LoginSession
import net.mamoe.mirai.network.protocol.tim.packet.image.ClientTryGetImageIDPacket import net.mamoe.mirai.network.protocol.tim.packet.image.ClientTryGetImageIDPacket
@ -30,7 +32,7 @@ class UnsolvedImage(filename: String, val image: BufferedImage) : Image(getImage
return session.expectPacket<ServerTryGetImageIDResponsePacket> { return session.expectPacket<ServerTryGetImageIDResponsePacket> {
toSend { ClientTryGetImageIDPacket(session.bot.account.qqNumber, session.sessionKey, contact.number, image) } toSend { ClientTryGetImageIDPacket(session.bot.account.qqNumber, session.sessionKey, contact.number, image) }
expect { onExpect {
when (it) { when (it) {
is ServerTryGetImageIDFailedPacket -> { is ServerTryGetImageIDFailedPacket -> {
//已经存在于服务器了 //已经存在于服务器了
@ -38,8 +40,10 @@ class UnsolvedImage(filename: String, val image: BufferedImage) : Image(getImage
is ServerTryGetImageIDSuccessPacket -> { is ServerTryGetImageIDSuccessPacket -> {
val data = image.toByteArray() val data = image.toByteArray()
if (!ImageNetworkUtils.postImage(it.uKey.toUHexString(), data.size, session.bot.account.qqNumber, contact.number, data)) { withContext(Dispatchers.IO) {
throw RuntimeException("cannot upload image") if (!ImageNetworkUtils.postImage(it.uKey.toUHexString(), data.size, session.bot.account.qqNumber, contact.number, data)) {
throw RuntimeException("cannot upload image")
}
} }
} }
} }

View File

@ -6,7 +6,6 @@ import net.mamoe.mirai.network.protocol.tim.handler.ActionPacketHandler
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.MessagePacketHandler import net.mamoe.mirai.network.protocol.tim.handler.MessagePacketHandler
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.internal.handler.*
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.Packet import net.mamoe.mirai.network.protocol.tim.packet.Packet
import net.mamoe.mirai.network.protocol.tim.packet.ServerEventPacket import net.mamoe.mirai.network.protocol.tim.packet.ServerEventPacket
@ -46,14 +45,14 @@ interface BotNetworkHandler : Closeable {
* *
* java 调用方式: `botNetWorkHandler.getMessage()` * java 调用方式: `botNetWorkHandler.getMessage()`
*/ */
var message: MessagePacketHandler val message: MessagePacketHandler
/** /**
* 动作处理. 如发送好友请求, 处理别人发来的好友请求等 * 动作处理. 如发送好友请求, 处理别人发来的好友请求等
* *
* java 调用方式: `botNetWorkHandler.getAction()` * java 调用方式: `botNetWorkHandler.getAction()`
*/ */
var action: ActionPacketHandler val action: ActionPacketHandler
fun tryLogin(touchingTimeoutMillis: Long = 200): CompletableFuture<LoginState> fun tryLogin(touchingTimeoutMillis: Long = 200): CompletableFuture<LoginState>

View File

@ -42,13 +42,12 @@ class LoginSession(
/** /**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket]. * 发送一个数据包, 并期待接受一个特定的 [ServerPacket].
* 发送成功后, 该方法会等待收到 [ServerPacket] 直到超时.
* *
* 实现方法: * 实现方法:
* ```kotlin * ```kotlin
* session.expectPacket<ServerPacketXXX> { * session.expectPacket<ServerPacketXXX> {
* toSend { ClientPacketXXX(...) } * toSend { ClientPacketXXX(...) }
* expect {//it: ServerPacketXXX * onExpect {//it: ServerPacketXXX
* *
* } * }
* } * }
@ -57,8 +56,6 @@ class LoginSession(
* @param P 期待的包 * @param P 期待的包
* @param handlerTemporary 处理器. * @param handlerTemporary 处理器.
* @return future. 可进行超时处理 * @return future. 可进行超时处理
*
* Kotlin DSL: Kotlin 使用.
*/ */
@JvmSynthetic @JvmSynthetic
inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableFuture<Unit> { inline fun <reified P : ServerPacket> expectPacket(handlerTemporary: TemporaryPacketHandler<P>.() -> Unit): CompletableFuture<Unit> {
@ -83,15 +80,13 @@ class LoginSession(
* @param toSend 将要发送的包 * @param toSend 将要发送的包
* @param handler 处理期待的包 * @param handler 处理期待的包
* @return future. 可进行超时处理 * @return future. 可进行超时处理
*
* Kotlin DSL: Kotlin 使用.
*/ */
@JvmSynthetic @JvmSynthetic
inline fun <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: (P) -> Unit): CompletableFuture<Unit> { inline fun <reified P : ServerPacket> expectPacket(toSend: ClientPacket, noinline handler: suspend (P) -> Unit): CompletableFuture<Unit> {
val future = CompletableFuture<Unit>() val future = CompletableFuture<Unit>()
this.bot.network.addHandler(TemporaryPacketHandler(P::class, future, this).also { this.bot.network.addHandler(TemporaryPacketHandler(P::class, future, this).also {
it.toSend { toSend } it.toSend(toSend)
it.expect(handler) it.onExpect(handler)
}) })
return future return future
} }

View File

@ -10,7 +10,6 @@ import net.mamoe.mirai.network.BotNetworkHandler
import net.mamoe.mirai.network.LoginSession import net.mamoe.mirai.network.LoginSession
import net.mamoe.mirai.network.NetworkScope import net.mamoe.mirai.network.NetworkScope
import net.mamoe.mirai.network.protocol.tim.handler.* import net.mamoe.mirai.network.protocol.tim.handler.*
import net.mamoe.mirai.network.protocol.tim.internal.handler.*
import net.mamoe.mirai.network.protocol.tim.packet.* import net.mamoe.mirai.network.protocol.tim.packet.*
import net.mamoe.mirai.network.protocol.tim.packet.login.* import net.mamoe.mirai.network.protocol.tim.packet.login.*
import net.mamoe.mirai.task.MiraiThreadPool import net.mamoe.mirai.task.MiraiThreadPool

View File

@ -8,6 +8,14 @@ import kotlin.reflect.KClass
/** /**
* 临时数据包处理器 * 临时数据包处理器
* ```kotlin
* session.addHandler<ClientTouchResponsePacket>{
* toSend { ClientTouchPacket() }
* onExpect {//it: ClientTouchResponsePacket
* //do sth.
* }
* }
* ```
* *
* @see LoginSession.expectPacket * @see LoginSession.expectPacket
*/ */
@ -18,7 +26,7 @@ open class TemporaryPacketHandler<P : ServerPacket>(
) { ) {
private lateinit var toSend: ClientPacket private lateinit var toSend: ClientPacket
private lateinit var expect: (P) -> Unit private lateinit var expect: suspend (P) -> Unit
lateinit var session: LoginSession//无需覆盖 lateinit var session: LoginSession//无需覆盖
@ -27,8 +35,12 @@ open class TemporaryPacketHandler<P : ServerPacket>(
this.toSend = packet() this.toSend = packet()
} }
fun toSend(packet: ClientPacket) {
this.toSend = packet
}
fun expect(handler: (P) -> Unit) {
fun onExpect(handler: suspend (P) -> Unit) {
this.expect = handler this.expect = handler
} }
@ -37,7 +49,7 @@ open class TemporaryPacketHandler<P : ServerPacket>(
session.socket.sendPacket(toSend) session.socket.sendPacket(toSend)
} }
fun onPacketReceived(session: LoginSession, packet: ServerPacket): Boolean { suspend fun onPacketReceived(session: LoginSession, packet: ServerPacket): Boolean {
if (expectationClass.isInstance(packet) && session === this.fromSession) { if (expectationClass.isInstance(packet) && session === this.fromSession) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
expect(packet as P) expect(packet as P)