From 247a37da421e73570bcd7d840f64925c2fac9d33 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 16 Sep 2020 10:15:00 +0800 Subject: [PATCH] Introduce common SyncingCacheList --- .../qqandroid/network/QQAndroidClient.kt | 12 ++++----- .../network/protocol/SyncingCacheList.kt | 27 +++++++++++++++++++ .../chat/receive/MessageSvc.PbGetMsg.kt | 20 +++++--------- 3 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/SyncingCacheList.kt diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt index 113b601c5..7721433a1 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidClient.kt @@ -14,13 +14,13 @@ package net.mamoe.mirai.qqandroid.network import kotlinx.atomicfu.AtomicBoolean import kotlinx.atomicfu.AtomicInt import kotlinx.atomicfu.atomic -import kotlinx.coroutines.sync.Mutex import kotlinx.io.core.* import net.mamoe.mirai.data.OnlineStatus import net.mamoe.mirai.network.LoginFailedException import net.mamoe.mirai.network.NoServerAvailableException import net.mamoe.mirai.qqandroid.BotAccount import net.mamoe.mirai.qqandroid.QQAndroidBot +import net.mamoe.mirai.qqandroid.network.protocol.SyncingCacheList import net.mamoe.mirai.qqandroid.network.protocol.data.jce.FileStoragePushFSSvcListFuckKotlin import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY import net.mamoe.mirai.qqandroid.network.protocol.packet.PacketLogger @@ -206,7 +206,7 @@ internal open class QQAndroidClient( */ val protocolVersion: Short = 8001 - class C2cMessageSyncData { + class MessageSvcSyncData { val firstNotify: AtomicBoolean = atomic(true) @Volatile @@ -215,17 +215,17 @@ internal open class QQAndroidClient( var msgCtrlBuf: ByteArray = EMPTY_BYTE_ARRAY - internal data class SyncPacketIdentifier( + internal data class PbGetMessageSyncId( val uid: Long, val sequence: Int, val time: Int ) - val packetIdList = LinkedList() - val packetIdListLock = Mutex() + val pbGetMessageCacheList = SyncingCacheList() + val systemMsgNewGroupCacheList = SyncingCacheList() } - val c2cMessageSync = C2cMessageSyncData() + val c2cMessageSync = MessageSvcSyncData() /* * 以下登录使用 diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/SyncingCacheList.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/SyncingCacheList.kt new file mode 100644 index 000000000..70da8a59f --- /dev/null +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/SyncingCacheList.kt @@ -0,0 +1,27 @@ +/* + * + * * Copyright 2020 Mamoe Technologies and contributors. + * * + * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * * + * * https://github.com/mamoe/mirai/blob/master/LICENSE + * + */ + +package net.mamoe.mirai.qqandroid.network.protocol + +import net.mamoe.mirai.qqandroid.utils.LinkedList +import kotlin.jvm.Synchronized + +internal class SyncingCacheList(private val size: Int = 50) { + private val packetIdList = LinkedList() + + @Synchronized // faster than suspending Mutex + fun addCache(element: E): Boolean { + if (packetIdList.contains(element)) return false // duplicate + packetIdList.addLast(element) + if (packetIdList.size >= size) packetIdList.removeFirst() + return true + } +} \ No newline at end of file diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt index 987b36ed4..9e37e2cb5 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt @@ -191,20 +191,14 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory - - bot.client.c2cMessageSync.run { - val identifier = QQAndroidClient.C2cMessageSyncData.SyncPacketIdentifier( - uid = msg.msgHead.msgUid, - sequence = msg.msgHead.msgSeq, - time = msg.msgHead.msgTime + if (!bot.client.c2cMessageSync.pbGetMessageCacheList.addCache( + QQAndroidClient.MessageSvcSyncData.PbGetMessageSyncId( + uid = msg.msgHead.msgUid, + sequence = msg.msgHead.msgSeq, + time = msg.msgHead.msgTime + ) ) - - packetIdListLock.withLock { - if (packetIdList.contains(identifier)) return@mapNotNull null // duplicate - packetIdList.addLast(identifier) - if (packetIdList.size >= 50) packetIdList.removeFirst() - } - } + ) return@mapNotNull null when (msg.msgHead.msgType) { 33 -> bot.groupListModifyLock.withLock {