From 716b8c6129d74dcdcd293ddfc98bb8f2acbe1178 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sat, 6 Mar 2021 21:58:57 +0800 Subject: [PATCH] Reinitialize network before connect server --- mirai-core/src/commonMain/kotlin/AbstractBot.kt | 10 ++++++++-- mirai-core/src/commonMain/kotlin/QQAndroidBot.kt | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/AbstractBot.kt b/mirai-core/src/commonMain/kotlin/AbstractBot.kt index 1ec3b7475..182358909 100644 --- a/mirai-core/src/commonMain/kotlin/AbstractBot.kt +++ b/mirai-core/src/commonMain/kotlin/AbstractBot.kt @@ -28,7 +28,6 @@ import net.mamoe.mirai.event.EventPriority.MONITOR import net.mamoe.mirai.event.events.BotEvent import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.event.events.BotReloginEvent -import net.mamoe.mirai.internal.message.contextualBugReportException import net.mamoe.mirai.internal.network.BotNetworkHandler import net.mamoe.mirai.internal.network.DefaultServerList import net.mamoe.mirai.internal.network.closeAndJoin @@ -76,6 +75,13 @@ internal abstract class AbstractBot constructor( @Suppress("PropertyName") internal lateinit var _network: N + suspend fun reinitializeNetwork() { + if (::_network.isInitialized) { + _network.closeAndJoin(null) + } + _network = createNetworkHandler(coroutineContext) + } + internal var _isConnecting: Boolean = false override val isOnline: Boolean get() = _network.areYouOk() @@ -220,7 +226,7 @@ internal abstract class AbstractBot constructor( private suspend fun doRelogin() { while (true) { - _network = createNetworkHandler(coroutineContext) + reinitializeNetwork() try { _isConnecting = true @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) diff --git a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt index 5df690877..7c701ac06 100644 --- a/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt +++ b/mirai-core/src/commonMain/kotlin/QQAndroidBot.kt @@ -135,6 +135,9 @@ internal class QQAndroidBot constructor( override suspend fun relogin(cause: Throwable?) { bdhSyncer.loadFromCache() client.useNextServers { host, port -> + // net error in login + // network is dead therefore can't send any packet + reinitializeNetwork() network.closeEverythingAndRelogin(host, port, cause, 0) } }