Commit a63c925c authored by Him188's avatar Him188

Avoid using UShort for sequenceId

parent c3ccb0b4
...@@ -22,7 +22,7 @@ import net.mamoe.mirai.utils.io.* ...@@ -22,7 +22,7 @@ import net.mamoe.mirai.utils.io.*
internal class OutgoingPacket constructor( internal class OutgoingPacket constructor(
name: String?, name: String?,
val packetId: PacketId, val packetId: PacketId,
val sequenceId: UShort, val sequenceId: Short,
val delegate: ByteReadPacket val delegate: ByteReadPacket
) : Packet { ) : Packet {
val name: String by lazy { val name: String by lazy {
...@@ -48,13 +48,12 @@ private val EMPTY_BYTE_ARRAY = ByteArray(0) ...@@ -48,13 +48,12 @@ private val EMPTY_BYTE_ARRAY = ByteArray(0)
* *
* byte[] body encrypted by 16 zero * byte[] body encrypted by 16 zero
*/ */
@UseExperimental(ExperimentalUnsignedTypes::class)
internal inline fun PacketFactory<*, *>.buildLoginOutgoingPacket( internal inline fun PacketFactory<*, *>.buildLoginOutgoingPacket(
uinAccount: String, uinAccount: String,
extraData: ByteArray = EMPTY_BYTE_ARRAY, extraData: ByteArray = EMPTY_BYTE_ARRAY,
name: String? = null, name: String? = null,
id: PacketId = this.id, id: PacketId = this.id,
sequenceId: UShort = PacketFactory.atomicNextSequenceId(), sequenceId: Short = PacketFactory.atomicNextSequenceId(),
body: BytePacketBuilder.() -> Unit body: BytePacketBuilder.() -> Unit
): OutgoingPacket = OutgoingPacket(name, id, sequenceId, buildPacket { ): OutgoingPacket = OutgoingPacket(name, id, sequenceId, buildPacket {
writeIntLVPacket(lengthOffset = { it + 4 }) { writeIntLVPacket(lengthOffset = { it + 4 }) {
...@@ -259,7 +258,7 @@ internal inline fun BytePacketBuilder.writeRequestPacket( ...@@ -259,7 +258,7 @@ internal inline fun BytePacketBuilder.writeRequestPacket(
client: QQAndroidClient, client: QQAndroidClient,
encryptMethod: EncryptMethod, encryptMethod: EncryptMethod,
commandId: CommandId, commandId: CommandId,
sequenceId: UShort = PacketFactory.atomicNextSequenceId(), sequenceId: Short = PacketFactory.atomicNextSequenceId(),
bodyBlock: BytePacketBuilder.() -> Unit bodyBlock: BytePacketBuilder.() -> Unit
) { ) {
val body = encryptMethod.run { val body = encryptMethod.run {
...@@ -270,7 +269,7 @@ internal inline fun BytePacketBuilder.writeRequestPacket( ...@@ -270,7 +269,7 @@ internal inline fun BytePacketBuilder.writeRequestPacket(
writeByte(0x02) // head writeByte(0x02) // head
writeShort((27 + 2 + body.remaining).toShort()) // orthodox algorithm writeShort((27 + 2 + body.remaining).toShort()) // orthodox algorithm
writeShort(client.protocolVersion) writeShort(client.protocolVersion)
writeShort(sequenceId.toShort()) writeShort(sequenceId)
writeShort(commandId.id.toShort()) writeShort(commandId.id.toShort())
writeQQ(client.account.id) writeQQ(client.account.id)
writeByte(3) // originally const writeByte(3) // originally const
......
...@@ -7,6 +7,7 @@ import net.mamoe.mirai.data.Packet ...@@ -7,6 +7,7 @@ import net.mamoe.mirai.data.Packet
import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.BotNetworkHandler
import net.mamoe.mirai.qqandroid.network.protocol.packet.login.NullPacketId import net.mamoe.mirai.qqandroid.network.protocol.packet.login.NullPacketId
import net.mamoe.mirai.qqandroid.network.protocol.packet.login.PacketId import net.mamoe.mirai.qqandroid.network.protocol.packet.login.PacketId
import net.mamoe.mirai.utils.LockFreeLinkedList
import net.mamoe.mirai.utils.cryptor.Decrypter import net.mamoe.mirai.utils.cryptor.Decrypter
import net.mamoe.mirai.utils.cryptor.DecrypterType import net.mamoe.mirai.utils.cryptor.DecrypterType
...@@ -31,18 +32,21 @@ internal abstract class PacketFactory<out TPacket : Packet, TDecrypter : Decrypt ...@@ -31,18 +32,21 @@ internal abstract class PacketFactory<out TPacket : Packet, TDecrypter : Decrypt
/** /**
* **解码**服务器的回复数据包 * **解码**服务器的回复数据包
*/ */
abstract suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler): TPacket abstract suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: Short, handler: BotNetworkHandler): TPacket
companion object { companion object {
private val sequenceId: AtomicInt = atomic(1) private val sequenceId: AtomicInt = atomic(1)
fun atomicNextSequenceId(): UShort { fun atomicNextSequenceId(): Short {
val id = sequenceId.getAndAdd(1) val id = sequenceId.getAndAdd(1)
if (id > Short.MAX_VALUE.toInt() * 2) { if (id > Short.MAX_VALUE.toInt() * 2) {
sequenceId.value = 0 sequenceId.value = 0
return atomicNextSequenceId() return atomicNextSequenceId()
} }
return id.toUShort() return id.toShort()
} }
} }
}
internal class KnownPacketFactories : LockFreeLinkedList<PacketFactory<*, *>>() {
} }
\ No newline at end of file
...@@ -19,7 +19,6 @@ class LoginPacketDecrypter(override val value: ByteArray) : DecrypterByteArray { ...@@ -19,7 +19,6 @@ class LoginPacketDecrypter(override val value: ByteArray) : DecrypterByteArray {
companion object : DecrypterType<LoginPacketDecrypter> companion object : DecrypterType<LoginPacketDecrypter>
} }
@UseExperimental(ExperimentalUnsignedTypes::class)
internal object LoginPacket : PacketFactory<LoginPacket.LoginPacketResponse, LoginPacketDecrypter>(LoginPacketDecrypter) { internal object LoginPacket : PacketFactory<LoginPacket.LoginPacketResponse, LoginPacketDecrypter>(LoginPacketDecrypter) {
init { init {
this._id = PacketId(CommandId("wtlogin.login", 0x0810), 9) this._id = PacketId(CommandId("wtlogin.login", 0x0810), 9)
...@@ -164,7 +163,8 @@ internal object LoginPacket : PacketFactory<LoginPacket.LoginPacketResponse, Log ...@@ -164,7 +163,8 @@ internal object LoginPacket : PacketFactory<LoginPacket.LoginPacketResponse, Log
class LoginPacketResponse : Packet class LoginPacketResponse : Packet
override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler): LoginPacketResponse { @ExperimentalUnsignedTypes
override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: Short, handler: BotNetworkHandler): LoginPacketResponse {
TODO() TODO()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment