Commit 61973170 authored by Him188's avatar Him188

Cancel heartbeat job if network is dead

parent 843643a7
...@@ -63,6 +63,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -63,6 +63,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
private lateinit var channel: PlatformSocket private lateinit var channel: PlatformSocket
private var _packetReceiverJob: Job? = null private var _packetReceiverJob: Job? = null
private var heartbeatJob: Job? = null
private val packetReceiveLock: Mutex = Mutex() private val packetReceiveLock: Mutex = Mutex()
...@@ -90,11 +91,13 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -90,11 +91,13 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} }
override suspend fun relogin() { override suspend fun relogin() {
heartbeatJob?.cancel()
if (::channel.isInitialized) { if (::channel.isInitialized) {
if (channel.isOpen) { if (channel.isOpen) {
kotlin.runCatching { kotlin.runCatching {
registerClientOnline() registerClientOnline()
}.exceptionOrNull() ?: return }.exceptionOrNull() ?: return
logger.info("Cannot do fast relogin. Trying slow relogin")
} }
channel.close() channel.close()
} }
...@@ -157,7 +160,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -157,7 +160,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} }
private suspend fun registerClientOnline() { private suspend fun registerClientOnline() {
StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>(6000) // it's slow StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>()
} }
@UseExperimental(MiraiExperimentalAPI::class, ExperimentalTime::class) @UseExperimental(MiraiExperimentalAPI::class, ExperimentalTime::class)
...@@ -250,7 +253,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -250,7 +253,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
joinAll(friendListJob, groupJob) joinAll(friendListJob, groupJob)
this@QQAndroidBotNetworkHandler.launch(CoroutineName("Heartbeat")) { heartbeatJob = this@QQAndroidBotNetworkHandler.launch(CoroutineName("Heartbeat")) {
while (this.isActive) { while (this.isActive) {
delay(bot.configuration.heartbeatPeriodMillis) delay(bot.configuration.heartbeatPeriodMillis)
val failException = doHeartBeat() val failException = doHeartBeat()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment