Improve performance

This commit is contained in:
Him188 2021-06-27 18:39:11 +08:00
parent 09265190e9
commit 089d0eed8c
2 changed files with 27 additions and 29 deletions

View File

@ -9,9 +9,6 @@
package net.mamoe.mirai.internal.network.protocol.packet.chat.receive
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import kotlinx.io.core.ByteReadPacket
import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.network.QQAndroidClient
@ -34,9 +31,8 @@ internal object MessageSvcPbDeleteMsg : OutgoingPacketFactory<Nothing?>("Message
)
}
internal suspend fun delete(bot: QQAndroidBot, messages: Flow<MsgComm.Msg>) =
internal suspend fun delete(bot: QQAndroidBot, messages: List<MsgComm.Msg>) =
bot.network.run {
val map = messages.map {
MsgSvc.PbDeleteMsgReq.MsgItem(
fromUin = it.msgHead.fromUin,
@ -44,9 +40,9 @@ internal object MessageSvcPbDeleteMsg : OutgoingPacketFactory<Nothing?>("Message
// 群为84、好友为187。群通过其他方法删除但测试结果显示通过187也能删除群消息。
msgType = 187,
msgSeq = it.msgHead.msgSeq,
msgUid = it.msgHead.msgUid
msgUid = it.msgHead.msgUid,
)
}.toList()
}
MessageSvcPbDeleteMsg(bot.client, map).sendWithoutExpect()
}

View File

@ -12,9 +12,7 @@
package net.mamoe.mirai.internal.network.protocol.packet.chat.receive
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.io.core.ByteReadPacket
import net.mamoe.mirai.Bot
@ -60,11 +58,11 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
syncFlag = syncFlag,
// serverBuf = from.serverBuf ?: EMPTY_BYTE_ARRAY,
syncCookie = syncCookie ?: client.syncingController.syncCookie
?: byteArrayOf()//.also { client.c2cMessageSync.syncCookie = it },
?: byteArrayOf(), //.also { client.c2cMessageSync.syncCookie = it },
// syncFlag = client.c2cMessageSync.syncFlag,
//msgCtrlBuf = client.c2cMessageSync.msgCtrlBuf,
//pubaccountCookie = client.c2cMessageSync.pubAccountCookie
)
),
)
}
@ -80,7 +78,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
open class Response(
internal val syncFlagFromServer: MsgSvc.SyncFlag,
delegate: List<Packet>,
val syncCookie: ByteArray?, override val bot: Bot
val syncCookie: ByteArray?, override val bot: Bot,
) : AbstractEvent(),
MultiPacket,
Collection<Packet> by delegate,
@ -92,10 +90,9 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
}
class EmptyResponse(
bot: QQAndroidBot
bot: QQAndroidBot,
) : GetMsgSuccess(emptyList(), null, bot)
@OptIn(FlowPreview::class)
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
// 00 00 01 0F 08 00 12 00 1A 34 08 FF C1 C4 F1 05 10 FF C1 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 8A CA 91 D1 0C 48 9B A5 BD 9B 0A 58 DE 9D 99 F8 08 60 1D 68 FF C1 C4 F1 05 70 00 20 02 2A 9D 01 08 F3 C1 C4 F1 05 10 A2 FF 8C F0 03 18 01 22 8A 01 0A 2A 08 A2 FF 8C F0 03 10 DD F1 92 B7 07 18 A6 01 20 0B 28 AE F9 01 30 F4 C1 C4 F1 05 38 A7 E3 D8 D4 84 80 80 80 01 B8 01 CD B5 01 12 08 08 01 10 00 18 00 20 00 1A 52 0A 50 0A 27 08 00 10 F4 C1 C4 F1 05 18 A7 E3 D8 D4 04 20 00 28 0C 30 00 38 86 01 40 22 4A 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 12 08 0A 06 0A 04 4E 4D 53 4C 12 15 AA 02 12 9A 01 0F 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00 12 04 4A 02 08 00 30 01 2A 15 08 97 A2 C1 F1 05 10 95 A6 F5 E5 0C 18 01 30 01 40 01 48 81 01 2A 10 08 D3 F7 B5 F1 05 10 DD F1 92 B7 07 18 01 30 01 38 00 42 00 48 00
val resp = readProtoBuf(MsgSvc.PbGetMsgResp.serializer())
@ -104,8 +101,8 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
// this is normally recoverable, no need to log
// bot.network.logger
// .warning { "MessageSvcPushNotify: result != 0, result = ${resp.result}, errorMsg=${resp.errmsg}" }
// bot.network.logger
// .warning { "MessageSvcPushNotify: result != 0, result = ${resp.result}, errorMsg=${resp.errmsg}" }
bot.network.launch(CoroutineName("MessageSvcPushNotify.retry")) {
delay(500 + Random.nextLong(0, 1000))
bot.network.run {
@ -129,23 +126,28 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
}
// bot.logger.debug(resp.msgRspType._miraiContentToString())
// bot.logger.debug(resp.syncCookie._miraiContentToString())
// bot.logger.debug(resp.msgRspType._miraiContentToString())
// bot.logger.debug(resp.syncCookie._miraiContentToString())
bot.client.syncingController.msgCtrlBuf = resp.msgCtrlBuf
if (resp.uinPairMsgs.isEmpty()) {
return EmptyResponse(bot)
if (resp.uinPairMsgs.isEmpty()) return EmptyResponse(bot)
for (uinPairMsg in resp.uinPairMsgs) {
if (uinPairMsg.msg.isEmpty()) continue
for (msg in uinPairMsg.msg) {
if (msg.msgHead.msgTime <= uinPairMsg.lastReadTime.toLong() and 4294967295L) continue
}
}
val messages = resp.uinPairMsgs.asFlow()
val messages = resp.uinPairMsgs.asSequence()
.filterNot { it.msg.isEmpty() }
.flatMapConcat {
it.msg.asFlow()
.filter { msg: MsgComm.Msg -> msg.msgHead.msgTime > it.lastReadTime.toLong() and 4294967295L }
}.also {
MessageSvcPbDeleteMsg.delete(bot, it) // 删除消息
.flatMap { pair ->
pair.msg.asSequence()
.filter { msg: MsgComm.Msg -> msg.msgHead.msgTime > pair.lastReadTime.toLong() and 4294967295L }
}
.toList()
.also { MessageSvcPbDeleteMsg.delete(bot, it) } // 删除消息
.filter { msg ->
bot.client.syncingController.pbGetMessageCacheList.addCache(
QQAndroidClient.MessageSvcSyncData.PbGetMessageSyncId(
@ -159,7 +161,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
bot.processPacketThroughPipeline(msg, KEY_FROM_SYNC to false)
}
val list: List<Packet> = messages.toList()
val list: List<Packet> = messages
if (resp.syncFlag == MsgSvc.SyncFlag.STOP) {
return GetMsgSuccess(list, resp.syncCookie, bot)
}
@ -177,7 +179,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
MessageSvcPbGetMsg(
client,
MsgSvc.SyncFlag.CONTINUE,
bot.client.syncingController.syncCookie
bot.client.syncingController.syncCookie,
).sendAndExpect()
}
return
@ -188,7 +190,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
MessageSvcPbGetMsg(
client,
MsgSvc.SyncFlag.CONTINUE,
bot.client.syncingController.syncCookie
bot.client.syncingController.syncCookie,
).sendAndExpect()
}
return