From c425a809ba8bf59de27b8437a19cf435882006ff Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 21 Apr 2020 14:53:24 +0800 Subject: [PATCH] Fix #245 --- .../mirai/qqandroid/contact/GroupImpl.kt | 2 +- .../qqandroid/message/outgoingSourceImpl.kt | 30 ++++++++----------- .../network/QQAndroidBotNetworkHandler.kt | 5 ++-- .../packet/chat/receive/MessageSvc.kt | 1 + 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt index 923f0da98..8c86774da 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt @@ -328,7 +328,6 @@ internal class GroupImpl( msg ) { source = it - source.startWaitingSequenceId(this) }.sendAndExpect() if (response is MessageSvc.PbSendMsg.Response.Failed) { when (response.resultType) { @@ -352,6 +351,7 @@ internal class GroupImpl( "Timeout awaiting sequenceId for group message(${message.contentToString() .take(10)}). Some features may not work properly" } + bot.network.logger.warning(e) } return MessageReceipt(source, this, botAsMember) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/outgoingSourceImpl.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/outgoingSourceImpl.kt index 56f7da9b5..13b0939e7 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/outgoingSourceImpl.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/outgoingSourceImpl.kt @@ -17,8 +17,8 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.QQ +import net.mamoe.mirai.event.asyncFromEventOrNull import net.mamoe.mirai.event.internal.MiraiAtomicBoolean -import net.mamoe.mirai.event.subscribingGetAsync import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.OnlineMessageSource @@ -27,7 +27,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgComm import net.mamoe.mirai.qqandroid.network.protocol.data.proto.SourceMsg import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.OnlinePush import net.mamoe.mirai.qqandroid.utils.io.serialization.toByteArray -import net.mamoe.mirai.utils.MiraiExperimentalAPI private fun T.toJceDataImpl(): ImMsgBody.SourceMsg @@ -104,6 +103,7 @@ internal class MessageSourceToTempImpl( } internal class MessageSourceToGroupImpl( + coroutineScope: CoroutineScope, override val random: Int, override val time: Int, override val originalMessage: MessageChain, @@ -115,29 +115,25 @@ internal class MessageSourceToGroupImpl( override val bot: Bot get() = sender override var isRecalledOrPlanned: MiraiAtomicBoolean = MiraiAtomicBoolean(false) - private lateinit var sequenceIdDeferred: Deferred + + private val sequenceIdDeferred: Deferred = + coroutineScope.asyncFromEventOrNull( + timeoutMillis = 3000 + ) { + if (it.messageRandom == this@MessageSourceToGroupImpl.random) { + it.sequenceId + } else null + } @OptIn(ExperimentalCoroutinesApi::class) override val sequenceId: Int get() = when { - sequenceIdDeferred.isCompleted -> sequenceIdDeferred.getCompleted() + sequenceIdDeferred.isCompleted -> sequenceIdDeferred.getCompleted() ?: 0 !sequenceIdDeferred.isActive -> 0 else -> error("sequenceId not yet available") } - @OptIn(MiraiExperimentalAPI::class) - internal fun startWaitingSequenceId(coroutineScope: CoroutineScope) { - sequenceIdDeferred = - coroutineScope.subscribingGetAsync( - timeoutMillis = 3000 - ) { - if (it.messageRandom == this@MessageSourceToGroupImpl.id) { - it.sequenceId - } else null - } - } - - suspend fun ensureSequenceIdAvailable() = sequenceIdDeferred.join() + suspend fun ensureSequenceIdAvailable() = kotlin.run { sequenceIdDeferred.await() } private val jceData by lazy { val elements = originalMessage.toRichTextElems(forGroup = false, withGeneralFlags = true) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt index 631b7f626..92af20e90 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt @@ -37,7 +37,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.login.ConfigPushSvc import net.mamoe.mirai.qqandroid.network.protocol.packet.login.Heartbeat import net.mamoe.mirai.qqandroid.network.protocol.packet.login.StatSvc import net.mamoe.mirai.qqandroid.network.protocol.packet.login.WtLogin -import net.mamoe.mirai.qqandroid.utils.NoRouteToHostException import net.mamoe.mirai.qqandroid.utils.PlatformSocket import net.mamoe.mirai.qqandroid.utils.SocketException import net.mamoe.mirai.qqandroid.utils.io.readPacketExact @@ -315,7 +314,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler this@QQAndroidBotNetworkHandler.launch { logger.info { "Awaiting ConfigPushSvc.PushReq" } val resp = - subscribingGetOrNull( + syncFromEventOrNull( 10_000) { it } when (resp) { @@ -337,7 +336,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler } withTimeoutOrNull(30000) { - launch { subscribingGet { Unit } } + launch { syncFromEvent { Unit } } MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect() } ?: error("timeout syncing friend message history") diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt index 05ca2e6dd..95f228532 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt @@ -468,6 +468,7 @@ internal class MessageSvc { ): OutgoingPacket { val source = MessageSourceToGroupImpl( + group, random = Random.nextInt().absoluteValue, sender = client.bot, target = group,