From 01c050d0b6fd64dfcad2a8f75d7d77c7f9cc7a51 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sun, 10 Jan 2021 23:20:15 +0800 Subject: [PATCH] FragmentedMsgParsingCache --- ...gCache.kt => FragmentedMsgParsingCache.kt} | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) rename mirai-core/src/commonMain/kotlin/utils/{GroupPkgMsgParsingCache.kt => FragmentedMsgParsingCache.kt} (66%) diff --git a/mirai-core/src/commonMain/kotlin/utils/GroupPkgMsgParsingCache.kt b/mirai-core/src/commonMain/kotlin/utils/FragmentedMsgParsingCache.kt similarity index 66% rename from mirai-core/src/commonMain/kotlin/utils/GroupPkgMsgParsingCache.kt rename to mirai-core/src/commonMain/kotlin/utils/FragmentedMsgParsingCache.kt index 94d0fbd14..dc73027c6 100644 --- a/mirai-core/src/commonMain/kotlin/utils/GroupPkgMsgParsingCache.kt +++ b/mirai-core/src/commonMain/kotlin/utils/FragmentedMsgParsingCache.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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. @@ -10,6 +10,7 @@ package net.mamoe.mirai.internal.utils import kotlinx.atomicfu.locks.withLock +import net.mamoe.mirai.internal.network.protocol.data.proto.MsgComm import net.mamoe.mirai.internal.network.protocol.data.proto.MsgOnlinePush import net.mamoe.mirai.utils.currentTimeMillis import java.util.concurrent.locks.ReentrantLock @@ -17,16 +18,16 @@ import java.util.concurrent.locks.ReentrantLock /** * fragmented message */ -internal class GroupPkgMsgParsingCache { - class PkgMsg( +internal abstract class FragmentedMsgParsingCache { + class PkgMsg( val size: Int, val divSeq: Int, - val data: MutableMap, + val data: MutableMap, ) { val createTime = currentTimeMillis() } - private val deque = ArrayList(16) + private val deque = ArrayList>(16) private val accessLock = ReentrantLock() private fun clearInvalid() { deque.removeIf { @@ -34,8 +35,10 @@ internal class GroupPkgMsgParsingCache { } } - fun tryMerge(msg: MsgOnlinePush.PbPushMsg): List { - val head = msg.msg.contentHead ?: return listOf(msg) + internal abstract val T.contentHead: MsgComm.ContentHead? + + fun tryMerge(msg: T): List { + val head = msg.contentHead ?: return listOf(msg) val size = head.pkgNum if (size < 2) return listOf(msg) accessLock.withLock { @@ -44,7 +47,7 @@ internal class GroupPkgMsgParsingCache { val index = head.pkgIndex val pkgMsg = deque.find { it.divSeq == seq - } ?: PkgMsg(size, seq, mutableMapOf()).also { deque.add(it) } + } ?: PkgMsg(size, seq, mutableMapOf()).also { deque.add(it) } pkgMsg.data[index] = msg if (pkgMsg.data.size == pkgMsg.size) { deque.removeIf { it.divSeq == seq } @@ -56,4 +59,12 @@ internal class GroupPkgMsgParsingCache { return emptyList() } } -} \ No newline at end of file +} + +/** + * fragmented message + */ +internal class GroupPkgMsgParsingCache : FragmentedMsgParsingCache() { + override val MsgOnlinePush.PbPushMsg.contentHead: MsgComm.ContentHead? + get() = this.msg.contentHead +}