Internal optimizations: simplify RequestPacket and jce struct sending

This commit is contained in:
Him188 2020-12-24 17:46:41 +08:00
parent 4e778170ae
commit ad8ffa6cd4
9 changed files with 54 additions and 38 deletions

View File

@ -13,18 +13,17 @@ import kotlinx.serialization.Serializable
import net.mamoe.mirai.internal.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.internal.utils.io.JceStruct
import net.mamoe.mirai.internal.utils.io.serialization.tars.TarsId
import kotlin.jvm.JvmField
private val EMPTY_MAP = mapOf<String, String>()
@Serializable
internal class RequestPacket(
@TarsId(1) @JvmField val iVersion: Short? = 3,
@TarsId(1) @JvmField val version: Short? = 3,
@TarsId(2) @JvmField val cPacketType: Byte = 0,
@TarsId(3) @JvmField val iMessageType: Int = 0,
@TarsId(4) @JvmField val iRequestId: Int = 0,
@TarsId(5) @JvmField val sServantName: String = "",
@TarsId(6) @JvmField val sFuncName: String = "",
@TarsId(4) @JvmField val requestId: Int = 0,
@TarsId(5) @JvmField val servantName: String = "",
@TarsId(6) @JvmField val funcName: String = "",
@TarsId(7) @JvmField val sBuffer: ByteArray = EMPTY_BYTE_ARRAY,
@TarsId(8) @JvmField val iTimeout: Int? = 0,
@TarsId(9) @JvmField val context: Map<String, String>? = EMPTY_MAP,

View File

@ -9,7 +9,7 @@
@file:Suppress("SpellCheckingInspection")
package net.mamoe.mirai.internal.network.protocol.data.jce
package net.mamoe.mirai.internal.network.protocol.data.jce
import kotlinx.serialization.Serializable
import net.mamoe.mirai.internal.utils.io.JceStruct

View File

@ -128,6 +128,7 @@ internal object KnownPacketFactories {
WtLogin.Login,
StatSvc.Register,
StatSvc.GetOnlineStatus,
StatSvc.GetDevLoginInfo,
MessageSvcPbGetMsg,
MessageSvcPushForceOffline,
MessageSvcPbSendMsg,

View File

@ -406,11 +406,11 @@ internal class TroopManagement {
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
sFuncName = "ModifyGroupCardReq",
sServantName = "mqq.IMService.FriendListServiceServantObj",
iVersion = 3,
funcName = "ModifyGroupCardReq",
servantName = "mqq.IMService.FriendListServiceServantObj",
version = 3,
cPacketType = 0x00,
iRequestId = client.nextRequestPacketRequestId(),
requestId = client.nextRequestPacketRequestId(),
sBuffer = jceRequestSBuffer(
"MGCREQ",
ModifyGroupCardReq.serializer(),

View File

@ -135,9 +135,9 @@ internal object OnlinePushReqPush : IncomingPacketFactory<OnlinePushReqPush.ReqP
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
sServantName = "OnlinePush",
sFuncName = "SvcRespPushMsg",
iRequestId = sequenceId,
servantName = "OnlinePush",
funcName = "SvcRespPushMsg",
requestId = sequenceId,
sBuffer = jceRequestSBuffer(
"resp",
OnlinePushPack.SvcRespPushMsg.serializer(),

View File

@ -47,10 +47,10 @@ internal class FriendList {
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
sFuncName = "GetTroopMemberListReq",
sServantName = "mqq.IMService.FriendListServiceServantObj",
iVersion = 3,
iRequestId = client.nextRequestPacketRequestId(),
funcName = "GetTroopMemberListReq",
servantName = "mqq.IMService.FriendListServiceServantObj",
version = 3,
requestId = client.nextRequestPacketRequestId(),
sBuffer = jceRequestSBuffer(
"GTML",
GetTroopMemberListReq.serializer(),
@ -97,11 +97,11 @@ internal class FriendList {
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
sFuncName = "GetTroopListReqV2Simplify",
sServantName = "mqq.IMService.FriendListServiceServantObj",
iVersion = 3,
funcName = "GetTroopListReqV2Simplify",
servantName = "mqq.IMService.FriendListServiceServantObj",
version = 3,
cPacketType = 0x00,
iRequestId = client.nextRequestPacketRequestId(),
requestId = client.nextRequestPacketRequestId(),
sBuffer = jceRequestSBuffer(
"GetTroopListReqV2Simplify",
GetTroopListReqV2Simplify.serializer(),
@ -153,11 +153,11 @@ internal class FriendList {
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
sFuncName = "GetFriendListReq",
sServantName = "mqq.IMService.FriendListServiceServantObj",
iVersion = 3,
funcName = "GetFriendListReq",
servantName = "mqq.IMService.FriendListServiceServantObj",
version = 3,
cPacketType = 0x003,
iRequestId = 1921334514,
requestId = 1921334514,
sBuffer = jceRequestSBuffer(
"FL",
GetFriendListReq.serializer(),

View File

@ -40,9 +40,9 @@ internal class ProfileService {
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
sServantName = "KQQ.ProfileService.ProfileServantObj",
sFuncName = "GroupMngReq",
iRequestId = client.nextRequestPacketRequestId(),
servantName = "KQQ.ProfileService.ProfileServantObj",
funcName = "GroupMngReq",
requestId = client.nextRequestPacketRequestId(),
sBuffer = jceRequestSBuffer(
"GroupMngReq",
GroupMngReqJce.serializer(),

View File

@ -245,10 +245,10 @@ msfwifi.3g.qq.com ?
writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
iRequestId = 0,
iVersion = 3,
sServantName = "QQService.ConfigPushSvc.MainServant",
sFuncName = "PushResp",
requestId = 0,
version = 3,
servantName = "QQService.ConfigPushSvc.MainServant",
funcName = "PushResp",
sBuffer = jceRequestSBuffer(
"PushResp",
PushResp.serializer(),

View File

@ -24,8 +24,6 @@ import net.mamoe.mirai.internal.utils.io.ProtoBuf
import net.mamoe.mirai.internal.utils.io.readPacketExact
import net.mamoe.mirai.internal.utils.io.serialization.tars.Tars
import net.mamoe.mirai.internal.utils.read
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
internal fun <T : JceStruct> ByteArray.loadWithUniPacket(
deserializer: DeserializationStrategy<T>,
@ -50,6 +48,24 @@ internal fun <T : JceStruct> ByteReadPacket.readJceStruct(
return Tars.UTF_8.load(serializer, this.readPacketExact(length))
}
internal fun <T : JceStruct> BytePacketBuilder.writeJceRequestPacket(
version: Int = 3,
servantName: String,
funcName: String,
name: String = funcName,
serializer: SerializationStrategy<T>,
body: T
) = writeJceStruct(
RequestPacket.serializer(),
RequestPacket(
requestId = 0,
version = version.toShort(),
servantName = servantName,
funcName = funcName,
sBuffer = jceRequestSBuffer(name, serializer, body)
)
)
/**
* 先解析为 [RequestPacket], `UniRequest`, 再按版本解析 map, 再找出指定数据并反序列化
*/
@ -85,15 +101,15 @@ private fun <K, V> Map<K, V>.firstValue(): V = this.entries.first().value
private fun <R> ByteReadPacket.decodeUniRequestPacketAndDeserialize(name: String? = null, block: (ByteArray) -> R): R {
val request = this.readJceStruct(RequestPacket.serializer())
return block(if (name == null) when (request.iVersion?.toInt() ?: 3) {
return block(if (name == null) when (request.version?.toInt() ?: 3) {
2 -> request.sBuffer.loadAs(RequestDataVersion2.serializer()).map.firstValue().firstValue()
3 -> request.sBuffer.loadAs(RequestDataVersion3.serializer()).map.firstValue()
else -> error("unsupported version ${request.iVersion}")
} else when (request.iVersion?.toInt() ?: 3) {
else -> error("unsupported version ${request.version}")
} else when (request.version?.toInt() ?: 3) {
2 -> request.sBuffer.loadAs(RequestDataVersion2.serializer()).map.getOrElse(name) { error("cannot find $name") }
.firstValue()
3 -> request.sBuffer.loadAs(RequestDataVersion3.serializer()).map.getOrElse(name) { error("cannot find $name") }
else -> error("unsupported version ${request.iVersion}")
else -> error("unsupported version ${request.version}")
})
}