Adjust function name

This commit is contained in:
Him188 2019-11-12 14:51:01 +08:00
parent 3d8a8b5ab5
commit a42fbe47a4
10 changed files with 50 additions and 23 deletions

View File

@ -213,14 +213,14 @@ Mirai 22:04:48 : Packet received: UnknownEventPacket(id=00 D6, identity=(2092749
* @param lazyMessage 若需要验证请求时的验证消息. * @param lazyMessage 若需要验证请求时的验证消息.
*/ */
suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }, lazyRemark: () -> String = { "" }): AddFriendResult = bot.withSession { suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }, lazyRemark: () -> String = { "" }): AddFriendResult = bot.withSession {
when (CanAddFriendPacket(bot.qqAccount, id, bot.sessionKey).sendAndExpect<CanAddFriendResponse>().await()) { when (CanAddFriendPacket(bot.qqAccount, id, bot.sessionKey).sendAndExpect<CanAddFriendResponse>()) {
is CanAddFriendResponse.AlreadyAdded -> AddFriendResult.ALREADY_ADDED is CanAddFriendResponse.AlreadyAdded -> AddFriendResult.ALREADY_ADDED
is CanAddFriendResponse.Rejected -> AddFriendResult.REJECTED is CanAddFriendResponse.Rejected -> AddFriendResult.REJECTED
is CanAddFriendResponse.ReadyToAdd, is CanAddFriendResponse.ReadyToAdd,
is CanAddFriendResponse.RequireVerification -> { is CanAddFriendResponse.RequireVerification -> {
val key = RequestFriendAdditionKeyPacket(bot.qqAccount, id, sessionKey).sendAndExpect<RequestFriendAdditionKeyPacket.Response>().await().key val key = RequestFriendAdditionKeyPacket(bot.qqAccount, id, sessionKey).sendAndExpect<RequestFriendAdditionKeyPacket.Response>().key
AddFriendPacket(bot.qqAccount, id, sessionKey, lazyMessage(), lazyRemark(), key).sendAndExpect<AddFriendPacket.Response>().await() AddFriendPacket(bot.qqAccount, id, sessionKey, lazyMessage(), lazyRemark(), key).sendAndExpect<AddFriendPacket.Response>()
return AddFriendResult.WAITING_FOR_APPROVE return AddFriendResult.WAITING_FOR_APPROVE
} }
//这个做的是需要验证消息的情况, 不确定 ReadyToAdd 的是啥 //这个做的是需要验证消息的情况, 不确定 ReadyToAdd 的是啥
@ -232,13 +232,12 @@ suspend fun ContactSystem.addFriend(id: UInt, lazyMessage: () -> String = { "" }
/*is CanAddFriendResponse.ReadyToAdd -> { /*is CanAddFriendResponse.ReadyToAdd -> {
// TODO: 2019/11/11 这不需要验证信息的情况 // TODO: 2019/11/11 这不需要验证信息的情况
//AddFriendPacket(bot.qqAccount, id, bot.sessionKey, ).sendAndExpect<AddFriendPacket.Response>().await() //AddFriendPacket(bot.qqAccount, id, bot.sessionKey, ).sendAndExpectAsync<AddFriendPacket.Response>().await()
TODO() TODO()
}*/ }*/
} }
} }
/* /*
1494695429 同意好友请求后收到以下包: 1494695429 同意好友请求后收到以下包:

View File

@ -41,7 +41,7 @@ inline val Bot.qqs: ContactList<QQ>
/** /**
* [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值. * [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, [BotSession.sendAndExpect] * 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, [BotSession.sendAndExpectAsync]
*/ */
inline fun <R> Bot.withSession(block: BotSession.() -> R): R = with(this.network.session) { block() } inline fun <R> Bot.withSession(block: BotSession.() -> R): R = with(this.network.session) { block() }

View File

@ -104,7 +104,7 @@ class Group internal constructor(bot: Bot, val groupId: GroupId) : Contact(bot,
/** /**
* [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值. * [BotSession] 作为接收器 (receiver) 并调用 [block], 返回 [block] 的返回值.
* 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, [BotSession.sendAndExpect] * 这个方法将能帮助使用在 [BotSession] 中定义的一些扩展方法, [BotSession.sendAndExpectAsync]
*/ */
inline fun <R> Contact.withSession(block: BotSession.() -> R): R = bot.withSession(block) inline fun <R> Contact.withSession(block: BotSession.() -> R): R = bot.withSession(block)
@ -138,7 +138,7 @@ open class QQ internal constructor(bot: Bot, id: UInt) : Contact(bot, id) {
*/ */
suspend fun updateProfile(): Profile = bot.withSession { suspend fun updateProfile(): Profile = bot.withSession {
RequestProfileDetailsPacket(bot.qqAccount, id, sessionKey) RequestProfileDetailsPacket(bot.qqAccount, id, sessionKey)
.sendAndExpect<RequestProfileDetailsResponse, Profile> { it.profile } .sendAndExpectAsync<RequestProfileDetailsResponse, Profile> { it.profile }
.await().let { .await().let {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
if ((::profile as SuspendLazy<Profile>).isInitialized()) { if ((::profile as SuspendLazy<Profile>).isInitialized()) {

View File

@ -66,7 +66,7 @@ interface BotNetworkHandler<Socket : DataPacketSocketAdapter> : CoroutineScope {
/** /**
* 添加一个临时包处理器, 并发送相应的包 * 添加一个临时包处理器, 并发送相应的包
* *
* @see [BotSession.sendAndExpect] 发送并期待一个包 * @see [BotSession.sendAndExpectAsync] 发送并期待一个包
* @see [TemporaryPacketHandler] 临时包处理器 * @see [TemporaryPacketHandler] 临时包处理器
*/ */
suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>) suspend fun addHandler(temporaryPacketHandler: TemporaryPacketHandler<*, *>)

View File

@ -4,6 +4,7 @@ package net.mamoe.mirai.network
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Group
@ -75,7 +76,7 @@ class BotSession(
* 实现方法: * 实现方法:
* ```kotlin * ```kotlin
* with(session){ * with(session){
* ClientPacketXXX(...).sendAndExpect<ServerPacketXXX> { * ClientPacketXXX(...).sendAndExpectAsync<ServerPacketXXX> {
* //it: ServerPacketXXX * //it: ServerPacketXXX
* } * }
* } * }
@ -84,14 +85,14 @@ class BotSession(
* *
* @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包. * @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包.
* @param P 期待的包 * @param P 期待的包
* @param handler 处理期待的包. 将会在调用本函数的 [coroutineContext] 下执行. * @param handler 处理期待的包. **将会在调用本函数的 [coroutineContext] 下执行.**
* *
* @see Bot.withSession 转换接收器 (receiver, `this` 的指向) [BotSession] * @see Bot.withSession 转换 receiver, `this` 的指向, [BotSession]
*/ */
suspend inline fun <reified P : Packet, R> OutgoingPacket.sendAndExpect( suspend inline fun <reified P : Packet, R> OutgoingPacket.sendAndExpectAsync(
checkSequence: Boolean = true, checkSequence: Boolean = true,
noinline handler: suspend (P) -> R noinline handler: suspend (P) -> R
): CompletableDeferred<R> { ): Deferred<R> {
val deferred: CompletableDeferred<R> = CompletableDeferred(coroutineContext[Job]) val deferred: CompletableDeferred<R> = CompletableDeferred(coroutineContext[Job])
bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this@BotSession, checkSequence, coroutineContext + deferred).also { bot.network.addHandler(TemporaryPacketHandler(P::class, deferred, this@BotSession, checkSequence, coroutineContext + deferred).also {
it.toSend(this) it.toSend(this)
@ -103,9 +104,36 @@ class BotSession(
/** /**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P]. * 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
* 将能从本函数的返回值 [CompletableDeferred] 接收到所期待的 [P] * 将能从本函数的返回值 [CompletableDeferred] 接收到所期待的 [P]
* 本函数将能帮助实现清晰的包处理逻辑
*
* 实现方法:
* ```kotlin
* with(session){
* val deferred = ClientPacketXXX(...).sendAndExpectAsync<ServerPacketXXX>()
* // do something
*
* deferred.await() // or deferred.join()
* }
* ```
*/ */
suspend inline fun <reified P : Packet> OutgoingPacket.sendAndExpect(checkSequence: Boolean = true): CompletableDeferred<P> = suspend inline fun <reified P : Packet> OutgoingPacket.sendAndExpectAsync(checkSequence: Boolean = true): Deferred<P> =
sendAndExpect<P, P>(checkSequence) { it } sendAndExpectAsync<P, P>(checkSequence) { it }
/**
* 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
* 将调用 [CompletableDeferred.await], 挂起等待接收到指定的包后才返回.
* 本函数将能帮助实现清晰的包处理逻辑
*
* 实现方法:
* ```kotlin
* with(session){
* val packet:AddFriendPacket.Response = AddFriendPacket(...).sendAndExpect<AddFriendPacket.Response>()
* }
* ```
* @sample Bot.addFriend 添加好友
*/
suspend inline fun <reified P : Packet> OutgoingPacket.sendAndExpect(checkSequence: Boolean = true): P =
sendAndExpectAsync<P, P>(checkSequence) { it }.await()
suspend inline fun OutgoingPacket.send() = socket.sendPacket(this) suspend inline fun OutgoingPacket.send() = socket.sendPacket(this)

View File

@ -513,7 +513,7 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
HeartbeatPacket( HeartbeatPacket(
bot.qqAccount, bot.qqAccount,
sessionKey sessionKey
).sendAndExpect<HeartbeatPacketResponse>().join() ).sendAndExpectAsync<HeartbeatPacketResponse>().join()
} == null) { } == null) {
bot.logger.warning("Heartbeat timed out") bot.logger.warning("Heartbeat timed out")
bot.reinitializeNetworkHandler(configuration, HeartbeatTimeoutException()) bot.reinitializeNetworkHandler(configuration, HeartbeatTimeoutException())

View File

@ -41,7 +41,7 @@ interface DataPacketSocketAdapter : Closeable {
* *
* 可通过 hook 事件 [ServerPacketReceivedEvent] 来获取服务器返回. * 可通过 hook 事件 [ServerPacketReceivedEvent] 来获取服务器返回.
* *
* @see [BotSession.sendAndExpect] kotlin DSL * @see [BotSession.sendAndExpectAsync] kotlin DSL
*/ */
suspend fun sendPacket(packet: OutgoingPacket) suspend fun sendPacket(packet: OutgoingPacket)

View File

@ -21,7 +21,7 @@ import kotlin.reflect.KClass
* } * }
* ``` * ```
* *
* @see BotSession.sendAndExpect * @see BotSession.sendAndExpectAsync
*/ */
class TemporaryPacketHandler<P : Packet, R>( class TemporaryPacketHandler<P : Packet, R>(
private val expectationClass: KClass<P>, private val expectationClass: KClass<P>,

View File

@ -37,7 +37,7 @@ class OverFileSizeMaxException : IllegalStateException()
suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession { suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession {
val userContext = coroutineContext val userContext = coroutineContext
GroupImageIdRequestPacket(bot.qqAccount, internalId, image, sessionKey) GroupImageIdRequestPacket(bot.qqAccount, internalId, image, sessionKey)
.sendAndExpect<GroupImageIdRequestPacket.Response, Unit> { .sendAndExpectAsync<GroupImageIdRequestPacket.Response, Unit> {
withContext(userContext) { withContext(userContext) {
when (it) { when (it) {
is GroupImageIdRequestPacket.Response.RequireUpload -> httpClient.postImage( is GroupImageIdRequestPacket.Response.RequireUpload -> httpClient.postImage(
@ -69,8 +69,8 @@ suspend fun Group.uploadImage(image: ExternalImage): ImageId = withSession {
*/ */
suspend fun QQ.uploadImage(image: ExternalImage): ImageId = bot.withSession { suspend fun QQ.uploadImage(image: ExternalImage): ImageId = bot.withSession {
FriendImageIdRequestPacket(qqAccount, sessionKey, id, image) FriendImageIdRequestPacket(qqAccount, sessionKey, id, image)
.sendAndExpect<FriendImageIdRequestPacket.Response, ImageId> { .sendAndExpectAsync<FriendImageIdRequestPacket.Response, ImageId> {
return@sendAndExpect when (it) { return@sendAndExpectAsync when (it) {
is FriendImageIdRequestPacket.Response.RequireUpload -> { is FriendImageIdRequestPacket.Response.RequireUpload -> {
httpClient.postImage( httpClient.postImage(
htcmd = "0x6ff0070", htcmd = "0x6ff0070",

View File

@ -312,7 +312,7 @@ internal object DebugNetworkHandler : BotNetworkHandler<DataPacketSocketAdapter>
get() = bot get() = bot
} }
override val bot: Bot = Bot(0u, "") override val bot: Bot = Bot(qq, "")
val session = BotSession(bot, sessionKey, socket, this) val session = BotSession(bot, sessionKey, socket, this)
val action = ActionPacketHandler(session) val action = ActionPacketHandler(session)