Rename to handlePacket

This commit is contained in:
Him188 2019-11-08 22:03:01 +08:00
parent 34627b67e6
commit 1e4893c70f
9 changed files with 22 additions and 28 deletions

View File

@ -8,9 +8,9 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.BotSocketAdapter
import net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler.LoginHandler
import net.mamoe.mirai.network.protocol.tim.handler.*
import net.mamoe.mirai.network.protocol.tim.packet.HeartbeatPacket
import net.mamoe.mirai.network.protocol.tim.packet.OutgoingPacket
import net.mamoe.mirai.network.protocol.tim.packet.Packet
import net.mamoe.mirai.network.protocol.tim.packet.login.HeartbeatPacket
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult
import net.mamoe.mirai.network.protocol.tim.packet.login.RequestSKeyPacket
import net.mamoe.mirai.utils.BotConfiguration

View File

@ -255,7 +255,7 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
if (factory is SessionPacketFactory<*>) {
with(factory as SessionPacketFactory<TPacket>) {
processPacket(packet)
handlePacket(packet)
}
}

View File

@ -37,7 +37,7 @@ abstract class SessionPacketFactory<TPacket : Packet> : PacketFactory<TPacket, S
/**
* [BotNetworkHandler] 下处理这个包. 广播事件等.
*/
open suspend fun BotNetworkHandler<*>.processPacket(packet: TPacket) {}
open suspend fun BotNetworkHandler<*>.handlePacket(packet: TPacket) {}
}
/**

View File

@ -52,10 +52,10 @@ abstract class PacketFactory<out TPacket : Packet, TDecrypter : Decrypter>(val d
}
object UnknownPacketFactory : SessionPacketFactory<UnknownPacket>() {
override suspend fun BotNetworkHandler<*>.processPacket(packet: UnknownPacket) {
override suspend fun BotNetworkHandler<*>.handlePacket(packet: UnknownPacket) {
ByteArrayPool.useInstance {
packet.body.readAvailable(it)
bot.logger.debug("Unknown packet(${packet.id.value}) body = " + it.toUHexString())
bot.logger.debug("UnknownPacket(${packet.id.value.toUHexString()}) = " + it.toUHexString())
}
packet.body.close()
}
@ -66,7 +66,7 @@ object UnknownPacketFactory : SessionPacketFactory<UnknownPacket>() {
}
object IgnoredPacketFactory : SessionPacketFactory<IgnoredPacket>() {
override suspend fun BotNetworkHandler<*>.processPacket(packet: IgnoredPacket) {
override suspend fun BotNetworkHandler<*>.handlePacket(packet: IgnoredPacket) {
}
override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler<*>): IgnoredPacket = IgnoredPacket

View File

@ -82,5 +82,5 @@ enum class KnownPacketId(override inline val value: UShort, override inline val
;
override fun toString(): String = factory::class.simpleName ?: this.name
override fun toString(): String = (factory::class.simpleName ?: this.name) + "(${value.toUHexString()})"
}

View File

@ -162,7 +162,7 @@ object AddFriendPacket : SessionPacketFactory<AddFriendPacket.AddFriendResponse>
}
}
override suspend fun BotNetworkHandler<*>.processPacket(packet: AddFriendResponse) {
override suspend fun BotNetworkHandler<*>.handlePacket(packet: AddFriendResponse) {
}

View File

@ -22,8 +22,7 @@ data class UnknownEventPacket(
class UnknownEventParserAndHandler(override val id: UShort) : EventParserAndHandler<UnknownEventPacket> {
override suspend fun ByteReadPacket.parse(bot: Bot, identity: EventPacketIdentity): UnknownEventPacket {
MiraiLogger.debug("UnknownEventPacket type = ${id.toUHexString()}")
MiraiLogger.debug("UnknownEventPacket data = ${readBytes().toUHexString()}")
MiraiLogger.debug("UnknownEventPacket(${id.toUHexString()}) = ${readBytes().toUHexString()}")
return UnknownEventPacket(id, this) //TODO the cause is that `this` reference.
}

View File

@ -41,9 +41,12 @@ fun UShort.toByteArray(): ByteArray = with(toUInt()) {
}
/**
* [ByteArray] 后再转 hex
* [ByteArray] 后再转 hex.
*
* @return 2 Unsigned Hex
*/
fun UShort.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator)
fun UShort.toUHexString(separator: String = " "): String =
(this.toInt().shr(8).toUShort() and 255u).toByte().toUHexString() + separator + (this and 255u).toByte().toUHexString()
/**
* 255u -> 00 00 00 FF
@ -108,7 +111,8 @@ fun getRandomString(length: Int, charRange: CharRange): String = String(CharArra
/**
* 根据所给 [charRanges] 随机生成长度为 [length] [String].
*/
fun getRandomString(length: Int, vararg charRanges: CharRange): String = String(CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() })
fun getRandomString(length: Int, vararg charRanges: CharRange): String =
String(CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() })
/**
* [this] 4 [Byte] bits 合并为一个 [Int]
@ -118,7 +122,8 @@ fun getRandomString(length: Int, vararg charRanges: CharRange): String = String(
* 一个 [Int] 32 bits
* 本函数将 4 [Byte] bits 连接得到 [Int]
*/
fun ByteArray.toUInt(): UInt = this[0].toUInt().and(255u).shl(24) + this[1].toUInt().and(255u).shl(16) + this[2].toUInt().and(255u).shl(8) + this[3].toUInt().and(255u).shl(0)
fun ByteArray.toUInt(): UInt =
this[0].toUInt().and(255u).shl(24) + this[1].toUInt().and(255u).shl(16) + this[2].toUInt().and(255u).shl(8) + this[3].toUInt().and(255u).shl(0)
/**
* [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入.

View File

@ -93,30 +93,20 @@ object Main {
//println("raw = " + data.toUHexString())
data.read {
discardExact(3)
val idHex = readInt().toUHexString(" ")
if (idHex.startsWith("00 81")) {
val id = matchPacketId(readUShort())
val sequenceId = readUShort()
if (id == KnownPacketId.HEARTBEAT || readUInt() != qq)
return@read
}
if (readUInt() != qq) {
return@read
}
println("--------------")
println("接收数据包")
discardExact(3)//0x00 0x00 0x00. 但更可能是应该 discard 8
println("id=$idHex")
println("id=$id, sequence=${sequenceId.toUHexString()}")
val remaining = this.readRemainingBytes().cutTail(1)
try {
val decrypted = remaining.decryptBy(sessionKey)
println("解密body=${decrypted.toUHexString()}")
discardExact(3)
val id = matchPacketId(readUShort())
val sequenceId = readUShort()
discardExact(7)//4 for qq number, 3 for 0x00 0x00 0x00
val packet = use {
with(id.factory) {
provideDecrypter(id.factory)