Add init retry

This commit is contained in:
Him188 2020-01-31 23:33:03 +08:00
parent 4c1b25d4bb
commit aa0bf81af1
4 changed files with 24 additions and 15 deletions

View File

@ -312,19 +312,16 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
val rawInput = try {
channel.read()
} catch (e: ClosedChannelException) {
close()
bot.tryReinitializeNetworkHandler(e)
return
} catch (e: ReadPacketInternalException) {
bot.logger.error("Socket channel read failed: ${e.message}")
close()
bot.tryReinitializeNetworkHandler(e)
return
} catch (e: CancellationException) {
return
} catch (e: Throwable) {
bot.logger.error("Caught unexpected exceptions", e)
close()
bot.tryReinitializeNetworkHandler(e)
return
}

View File

@ -25,6 +25,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.buildOutgoingUniPacket
import net.mamoe.mirai.qqandroid.utils.toMessageChain
import net.mamoe.mirai.qqandroid.utils.toRichTextElems
import net.mamoe.mirai.utils.cryptor.contentToString
import net.mamoe.mirai.utils.currentTimeSeconds
import net.mamoe.mirai.utils.io.hexToBytes
import net.mamoe.mirai.utils.io.toReadPacket
import kotlin.math.absoluteValue
@ -162,8 +163,9 @@ internal class MessageSvc {
)
),
msgSeq = client.atomicNextMessageSequenceId(),
msgRand = Random.nextInt().absoluteValue
// syncCookie = client.c2cMessageSync.syncCookie.takeIf { it.isNotEmpty() } ?: "08 92 C2 C4 F1 05 10 92 C2 C4 F1 05 18 E6 ED B9 C3 02 20 89 FE BE A4 06 28 89 84 F9 A2 06 48 DE 8C EA E5 0E 58 D9 BD BB A0 09 60 1D 68 92 C2 C4 F1 05 70 00".hexToBytes(),
msgRand = Random.nextInt().absoluteValue,
syncCookie = client.c2cMessageSync.syncCookie?.takeIf { it.isNotEmpty() }
?: SyncCookie(currentTimeSeconds).toByteArray(SyncCookie.serializer())
// msgVia = 1
)
)

View File

@ -6,6 +6,7 @@ import kotlinx.coroutines.*
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.BotOfflineEvent
import net.mamoe.mirai.network.BotNetworkHandler
import net.mamoe.mirai.network.closeAndJoin
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.io.logStacktrace
import kotlin.coroutines.CoroutineContext
@ -92,7 +93,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
try {
if (::_network.isInitialized) {
BotOfflineEvent(this).broadcast()
_network.close(cause)
_network.closeAndJoin(cause)
}
} catch (e: Exception) {
logger.error("Cannot close network handler", e)
@ -105,22 +106,26 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
break@loginLoop
} catch (e: Exception) {
e.logStacktrace()
_network.close(e)
_network.closeAndJoin(e)
}
logger.warning("Login failed. Retrying in 3s...")
delay(3000)
}
while (true) {
try {
return _network.init()
} catch (e: Exception) {
e.logStacktrace()
_network.close(e)
repeat(1) block@{
repeat(2) {
try {
_network.init()
return@block
} catch (e: Exception) {
e.logStacktrace()
}
logger.warning("Init failed. Retrying in 3s...")
delay(3000)
}
logger.warning("Init failed. Retrying in 3s...")
delay(3000)
logger.error("cannot init. some features may be affected")
}
}
protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N

View File

@ -73,4 +73,9 @@ abstract class BotNetworkHandler : CoroutineScope {
}
}
}
}
suspend fun BotNetworkHandler.closeAndJoin(cause: Throwable? = null){
this.close(cause)
this.supervisor.join()
}