From 17c2069c82992cf0ea61f712a211653b4ed35ca9 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Tue, 9 Feb 2021 15:08:50 +0800 Subject: [PATCH] Handle ConfigPushSvc.PushReq.PushReqResponse.ChangeServer in ConfigPushSvc --- .../handler/QQAndroidBotNetworkHandler.kt | 15 ++-------- .../protocol/packet/login/ConfigPushSvc.kt | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/network/handler/QQAndroidBotNetworkHandler.kt b/mirai-core/src/commonMain/kotlin/network/handler/QQAndroidBotNetworkHandler.kt index aaee046d1..8d40ffd75 100644 --- a/mirai-core/src/commonMain/kotlin/network/handler/QQAndroidBotNetworkHandler.kt +++ b/mirai-core/src/commonMain/kotlin/network/handler/QQAndroidBotNetworkHandler.kt @@ -371,19 +371,8 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo logger.info { "ConfigPushSvc.PushReq: Success." } } is ConfigPushSvc.PushReq.PushReqResponse.ChangeServer -> { - bot.logger.info { "Server requires reconnect." } - bot.logger.info { "Server list: ${resp.serverList.joinToString()}." } - - if (resp.serverList.isNotEmpty()) { - bot.serverList.clear() - resp.serverList.shuffled().forEach { - bot.serverList.add(it.host to it.port) - } - } - bot.bdhSyncer.saveToCache() - bot.bdhSyncer.saveServerListToCache() - - bot.launch { BotOfflineEvent.RequireReconnect(bot).broadcast() } + logger.info { "ConfigPushSvc.PushReq: Require reconnect" } + // handled in ConfigPushSvc return@launch } } diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/ConfigPushSvc.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/ConfigPushSvc.kt index 5724a33f6..fe9aaeb90 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/ConfigPushSvc.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/ConfigPushSvc.kt @@ -9,10 +9,14 @@ package net.mamoe.mirai.internal.network.protocol.packet.login +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import kotlinx.io.core.ByteReadPacket import kotlinx.serialization.Serializable import net.mamoe.mirai.event.AbstractEvent import net.mamoe.mirai.event.Event +import net.mamoe.mirai.event.broadcast +import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.message.contextualBugReportException import net.mamoe.mirai.internal.network.BdhSession @@ -30,6 +34,7 @@ import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.readUniPacket import net.mamoe.mirai.internal.utils.io.serialization.tars.TarsId import net.mamoe.mirai.internal.utils.io.serialization.writeJceStruct +import net.mamoe.mirai.utils.info import net.mamoe.mirai.utils.toUHexString import java.lang.IllegalStateException import net.mamoe.mirai.internal.network.protocol.data.jce.PushReq as PushReqJceStruct @@ -144,6 +149,25 @@ internal class ConfigPushSvc { ) } + fun handleRequireReconnect(resp: PushReqResponse.ChangeServer) { + bot.logger.info { "Server requires reconnect." } + bot.logger.info { "Server list: ${resp.serverList.joinToString()}." } + + if (resp.serverList.isNotEmpty()) { + bot.serverList.clear() + resp.serverList.shuffled().forEach { + bot.serverList.add(it.host to it.port) + } + } + bot.bdhSyncer.saveToCache() + bot.bdhSyncer.saveServerListToCache() + + bot.launch { + delay(1000) + BotOfflineEvent.RequireReconnect(bot).broadcast() + } + } + when (packet) { is PushReqResponse.Success -> { handleSuccess(packet) @@ -172,6 +196,10 @@ internal class ConfigPushSvc { // writePacket(this.build().debugPrintThis()) } } + is PushReqResponse.ChangeServer -> { + handleRequireReconnect(packet) + return null + } else -> { // handled in QQABot return null