mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-07 16:40:43 +08:00
Reinitialize network before connect server
This commit is contained in:
parent
038ea831ac
commit
716b8c6129
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user