Introduce common SyncingCacheList

This commit is contained in:
Him188 2020-09-16 10:15:00 +08:00
parent 1717501d85
commit 247a37da42
3 changed files with 40 additions and 19 deletions

View File

@ -14,13 +14,13 @@ package net.mamoe.mirai.qqandroid.network
import kotlinx.atomicfu.AtomicBoolean import kotlinx.atomicfu.AtomicBoolean
import kotlinx.atomicfu.AtomicInt import kotlinx.atomicfu.AtomicInt
import kotlinx.atomicfu.atomic import kotlinx.atomicfu.atomic
import kotlinx.coroutines.sync.Mutex
import kotlinx.io.core.* import kotlinx.io.core.*
import net.mamoe.mirai.data.OnlineStatus import net.mamoe.mirai.data.OnlineStatus
import net.mamoe.mirai.network.LoginFailedException import net.mamoe.mirai.network.LoginFailedException
import net.mamoe.mirai.network.NoServerAvailableException import net.mamoe.mirai.network.NoServerAvailableException
import net.mamoe.mirai.qqandroid.BotAccount import net.mamoe.mirai.qqandroid.BotAccount
import net.mamoe.mirai.qqandroid.QQAndroidBot 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.data.jce.FileStoragePushFSSvcListFuckKotlin
import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.qqandroid.network.protocol.packet.PacketLogger import net.mamoe.mirai.qqandroid.network.protocol.packet.PacketLogger
@ -206,7 +206,7 @@ internal open class QQAndroidClient(
*/ */
val protocolVersion: Short = 8001 val protocolVersion: Short = 8001
class C2cMessageSyncData { class MessageSvcSyncData {
val firstNotify: AtomicBoolean = atomic(true) val firstNotify: AtomicBoolean = atomic(true)
@Volatile @Volatile
@ -215,17 +215,17 @@ internal open class QQAndroidClient(
var msgCtrlBuf: ByteArray = EMPTY_BYTE_ARRAY var msgCtrlBuf: ByteArray = EMPTY_BYTE_ARRAY
internal data class SyncPacketIdentifier( internal data class PbGetMessageSyncId(
val uid: Long, val uid: Long,
val sequence: Int, val sequence: Int,
val time: Int val time: Int
) )
val packetIdList = LinkedList<SyncPacketIdentifier>() val pbGetMessageCacheList = SyncingCacheList<PbGetMessageSyncId>()
val packetIdListLock = Mutex() val systemMsgNewGroupCacheList = SyncingCacheList<PbGetMessageSyncId>()
} }
val c2cMessageSync = C2cMessageSyncData() val c2cMessageSync = MessageSvcSyncData()
/* /*
* 以下登录使用 * 以下登录使用

View File

@ -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<E>(private val size: Int = 50) {
private val packetIdList = LinkedList<E>()
@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
}
}

View File

@ -191,20 +191,14 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
MessageSvcPbDeleteMsg.delete(bot, it) // 删除消息 MessageSvcPbDeleteMsg.delete(bot, it) // 删除消息
} }
.mapNotNull { msg -> .mapNotNull { msg ->
if (!bot.client.c2cMessageSync.pbGetMessageCacheList.addCache(
bot.client.c2cMessageSync.run { QQAndroidClient.MessageSvcSyncData.PbGetMessageSyncId(
val identifier = QQAndroidClient.C2cMessageSyncData.SyncPacketIdentifier( uid = msg.msgHead.msgUid,
uid = msg.msgHead.msgUid, sequence = msg.msgHead.msgSeq,
sequence = msg.msgHead.msgSeq, time = msg.msgHead.msgTime
time = msg.msgHead.msgTime )
) )
) return@mapNotNull null
packetIdListLock.withLock {
if (packetIdList.contains(identifier)) return@mapNotNull null // duplicate
packetIdList.addLast(identifier)
if (packetIdList.size >= 50) packetIdList.removeFirst()
}
}
when (msg.msgHead.msgType) { when (msg.msgHead.msgType) {
33 -> bot.groupListModifyLock.withLock { 33 -> bot.groupListModifyLock.withLock {