From a4ea0772e6cf7792edeb4a280b2fe7f4a7d1a3f6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 8 Nov 2019 20:10:45 +0800 Subject: [PATCH] Migrate to data class --- .../protocol/tim/TIMBotNetworkHandler.kt | 46 +++++++++--------- .../protocol/tim/packet/login/Touch.kt | 47 +++++++++++++------ 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt index af9842299..505211106 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt @@ -357,31 +357,31 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot suspend fun onPacketReceived(packet: Any) {//complex function, but it doesn't matter when (packet) { - is TouchPacket.TouchResponse -> { - if (packet.serverIP != null) {//redirection - withContext(userContext) { - socket.close() - socket = BotSocketAdapter(packet.serverIP!!, socket.configuration) - bot.logger.info("Redirecting to ${packet.serverIP}") - loginResult.complete(socket.resendTouch()) - } - } else {//password submission - this.loginIP = packet.loginIP - this.loginTime = packet.loginTime - this.token0825 = packet.token0825 + is TouchPacket.TouchResponse.OK -> { + loginIP = packet.loginIP + loginTime = packet.loginTime + token0825 = packet.token0825 - socket.sendPacket( - SubmitPasswordPacket( - bot = bot.qqAccount, - password = bot.account.password, - loginTime = loginTime, - loginIP = loginIP, - privateKey = privateKey, - token0825 = token0825, - token00BA = null, - randomDeviceName = socket.configuration.randomDeviceName - ) + socket.sendPacket( + SubmitPasswordPacket( + bot = bot.qqAccount, + password = bot.account.password, + loginTime = loginTime, + loginIP = loginIP, + privateKey = privateKey, + token0825 = token0825, + token00BA = null, + randomDeviceName = socket.configuration.randomDeviceName ) + ) + } + + is TouchPacket.TouchResponse.Redirection -> { + withContext(userContext) { + socket.close() + socket = BotSocketAdapter(packet.serverIP!!, socket.configuration) + bot.logger.info("Redirecting to ${packet.serverIP}") + loginResult.complete(socket.resendTouch()) } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Touch.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Touch.kt index 592e58607..cbfe59c20 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Touch.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/login/Touch.kt @@ -44,31 +44,50 @@ object TouchPacket : PacketFactory(TouchKey } } - class TouchResponse : Packet { - var serverIP: String? = null - internal set - var loginTime: Int = 0 - internal set + sealed class TouchResponse : Packet { + data class OK( + var loginTime: Int, + val loginIP: String, + val token0825: ByteArray + ) : TouchResponse() { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is OK) return false - lateinit var loginIP: String - internal set - lateinit var token0825: ByteArray - internal set + if (loginTime != other.loginTime) return false + if (loginIP != other.loginIP) return false + if (!token0825.contentEquals(other.token0825)) return false + + return true + } + + override fun hashCode(): Int { + var result = loginTime + result = 31 * result + loginIP.hashCode() + result = 31 * result + token0825.contentHashCode() + return result + } + } + + data class Redirection( + val serverIP: String? = null + ) : TouchResponse() } - override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler<*>): TouchResponse = TouchResponse().apply { + override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler<*>): TouchResponse { when (val flag = readByte().toUByte().toInt()) { 0xFE -> { discardExact(94) - serverIP = readIP() + return TouchResponse.Redirection(readIP()) } 0x00 -> { discardExact(4) - token0825 = readBytes(56) + val token0825 = readBytes(56) discardExact(6) - loginTime = readInt() - loginIP = readIP() + val loginTime = readInt() + val loginIP = readIP() + return TouchResponse.OK(loginTime, loginIP, token0825) } else -> throw IllegalStateException(flag.toByte().toUHexString())