Commit aa0bf81a authored by Him188's avatar Him188

Add init retry

parent 4c1b25d4
...@@ -312,19 +312,16 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -312,19 +312,16 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
val rawInput = try { val rawInput = try {
channel.read() channel.read()
} catch (e: ClosedChannelException) { } catch (e: ClosedChannelException) {
close()
bot.tryReinitializeNetworkHandler(e) bot.tryReinitializeNetworkHandler(e)
return return
} catch (e: ReadPacketInternalException) { } catch (e: ReadPacketInternalException) {
bot.logger.error("Socket channel read failed: ${e.message}") bot.logger.error("Socket channel read failed: ${e.message}")
close()
bot.tryReinitializeNetworkHandler(e) bot.tryReinitializeNetworkHandler(e)
return return
} catch (e: CancellationException) { } catch (e: CancellationException) {
return return
} catch (e: Throwable) { } catch (e: Throwable) {
bot.logger.error("Caught unexpected exceptions", e) bot.logger.error("Caught unexpected exceptions", e)
close()
bot.tryReinitializeNetworkHandler(e) bot.tryReinitializeNetworkHandler(e)
return return
} }
......
...@@ -25,6 +25,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.buildOutgoingUniPacket ...@@ -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.toMessageChain
import net.mamoe.mirai.qqandroid.utils.toRichTextElems import net.mamoe.mirai.qqandroid.utils.toRichTextElems
import net.mamoe.mirai.utils.cryptor.contentToString 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.hexToBytes
import net.mamoe.mirai.utils.io.toReadPacket import net.mamoe.mirai.utils.io.toReadPacket
import kotlin.math.absoluteValue import kotlin.math.absoluteValue
...@@ -162,8 +163,9 @@ internal class MessageSvc { ...@@ -162,8 +163,9 @@ internal class MessageSvc {
) )
), ),
msgSeq = client.atomicNextMessageSequenceId(), msgSeq = client.atomicNextMessageSequenceId(),
msgRand = Random.nextInt().absoluteValue 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(), syncCookie = client.c2cMessageSync.syncCookie?.takeIf { it.isNotEmpty() }
?: SyncCookie(currentTimeSeconds).toByteArray(SyncCookie.serializer())
// msgVia = 1 // msgVia = 1
) )
) )
......
...@@ -6,6 +6,7 @@ import kotlinx.coroutines.* ...@@ -6,6 +6,7 @@ import kotlinx.coroutines.*
import net.mamoe.mirai.event.broadcast import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.BotOfflineEvent import net.mamoe.mirai.event.events.BotOfflineEvent
import net.mamoe.mirai.network.BotNetworkHandler import net.mamoe.mirai.network.BotNetworkHandler
import net.mamoe.mirai.network.closeAndJoin
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.io.logStacktrace import net.mamoe.mirai.utils.io.logStacktrace
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
...@@ -92,7 +93,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -92,7 +93,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
try { try {
if (::_network.isInitialized) { if (::_network.isInitialized) {
BotOfflineEvent(this).broadcast() BotOfflineEvent(this).broadcast()
_network.close(cause) _network.closeAndJoin(cause)
} }
} catch (e: Exception) { } catch (e: Exception) {
logger.error("Cannot close network handler", e) logger.error("Cannot close network handler", e)
...@@ -105,22 +106,26 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -105,22 +106,26 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
break@loginLoop break@loginLoop
} catch (e: Exception) { } catch (e: Exception) {
e.logStacktrace() e.logStacktrace()
_network.close(e) _network.closeAndJoin(e)
} }
logger.warning("Login failed. Retrying in 3s...") logger.warning("Login failed. Retrying in 3s...")
delay(3000) delay(3000)
} }
while (true) { repeat(1) block@{
try { repeat(2) {
return _network.init() try {
} catch (e: Exception) { _network.init()
e.logStacktrace() return@block
_network.close(e) } catch (e: Exception) {
e.logStacktrace()
}
logger.warning("Init failed. Retrying in 3s...")
delay(3000)
} }
logger.warning("Init failed. Retrying in 3s...") logger.error("cannot init. some features may be affected")
delay(3000)
} }
} }
protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N
......
...@@ -73,4 +73,9 @@ abstract class BotNetworkHandler : CoroutineScope { ...@@ -73,4 +73,9 @@ abstract class BotNetworkHandler : CoroutineScope {
} }
} }
} }
}
suspend fun BotNetworkHandler.closeAndJoin(cause: Throwable? = null){
this.close(cause)
this.supervisor.join()
} }
\ No newline at end of file
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