Improve login and init

This commit is contained in:
Him188 2020-01-30 23:53:28 +08:00
parent 288d0df738
commit fb3fe2bcca
3 changed files with 85 additions and 70 deletions

View File

@ -37,7 +37,6 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
override suspend fun login() { override suspend fun login() {
suspend fun doLogin() {
channel = PlatformSocket() channel = PlatformSocket()
channel.connect("113.96.13.208", 8080) channel.connect("113.96.13.208", 8080)
launch(CoroutineName("Incoming Packet Receiver")) { processReceive() } launch(CoroutineName("Incoming Packet Receiver")) { processReceive() }
@ -95,7 +94,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>() StatSvc.Register(bot.client).sendAndExpect<StatSvc.Register.Response>()
} }
suspend fun doInit() { suspend fun init() {
//start updating friend/group list //start updating friend/group list
bot.logger.info("Start updating friend/group list") bot.logger.info("Start updating friend/group list")
/* /*
@ -113,10 +112,6 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
println(data.contentToString()) println(data.contentToString())
} }
doLogin()
doInit()
}
/** /**
* 单线程处理包的接收, 分割和连接. * 单线程处理包的接收, 分割和连接.

View File

@ -27,7 +27,8 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
@Suppress("CanBePrimaryConstructorProperty") // for logger @Suppress("CanBePrimaryConstructorProperty") // for logger
final override val account: BotAccount = account final override val account: BotAccount = account
@UseExperimental(RawAccountIdUse::class) @UseExperimental(RawAccountIdUse::class)
override val uin: Long get() = account.id override val uin: Long
get() = account.id
final override val logger: MiraiLogger by lazy { configuration.logger ?: DefaultLogger("Bot($uin)").also { configuration.logger = it } } final override val logger: MiraiLogger by lazy { configuration.logger ?: DefaultLogger("Bot($uin)").also { configuration.logger = it } }
init { init {
@ -98,9 +99,10 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
} }
_network = createNetworkHandler(this.coroutineContext) _network = createNetworkHandler(this.coroutineContext)
while (true){ loginLoop@ while (true) {
try { try {
return _network.login() _network.login()
break@loginLoop
} catch (e: Exception) { } catch (e: Exception) {
e.logStacktrace() e.logStacktrace()
_network.dispose(e) _network.dispose(e)
@ -108,6 +110,17 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
logger.warning("Login failed. Retrying in 3s...") logger.warning("Login failed. Retrying in 3s...")
delay(3000) delay(3000)
} }
while (true) {
try {
return _network.init()
} catch (e: Exception) {
e.logStacktrace()
_network.dispose(e)
}
logger.warning("Init failed. Retrying in 3s...")
delay(3000)
}
} }
protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N protected abstract fun createNetworkHandler(coroutineContext: CoroutineContext): N

View File

@ -47,6 +47,13 @@ abstract class BotNetworkHandler : CoroutineScope {
@MiraiInternalAPI @MiraiInternalAPI
abstract suspend fun login() abstract suspend fun login()
/**
* 初始化获取好友列表等值.
*/
@MiraiInternalAPI
open suspend fun init() {
}
/** /**
* 等待直到与服务器断开连接. 若未连接则立即返回 * 等待直到与服务器断开连接. 若未连接则立即返回
*/ */