Reinitialize network before connect server

This commit is contained in:
Karlatemp 2021-03-06 21:58:57 +08:00
parent 038ea831ac
commit 716b8c6129
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 11 additions and 2 deletions

View File

@ -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<N : BotNetworkHandler> 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<N : BotNetworkHandler> constructor(
private suspend fun doRelogin() {
while (true) {
_network = createNetworkHandler(coroutineContext)
reinitializeNetwork()
try {
_isConnecting = true
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)

View File

@ -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)
}
}