Commit 23057b11 authored by Him188's avatar Him188

Add timeout

parent 9bb11dba
...@@ -25,6 +25,7 @@ import net.mamoe.mirai.utils.OnlineStatus ...@@ -25,6 +25,7 @@ import net.mamoe.mirai.utils.OnlineStatus
import net.mamoe.mirai.utils.io.* import net.mamoe.mirai.utils.io.*
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.properties.Delegates import kotlin.properties.Delegates
import kotlin.random.Random
/** /**
* 包处理协程调度器. * 包处理协程调度器.
...@@ -69,10 +70,12 @@ internal class TIMBotNetworkHandler internal constructor(coroutineContext: Corou ...@@ -69,10 +70,12 @@ internal class TIMBotNetworkHandler internal constructor(coroutineContext: Corou
override suspend fun login(configuration: BotConfiguration): LoginResult { override suspend fun login(configuration: BotConfiguration): LoginResult {
userContext = coroutineContext userContext = coroutineContext
return withContext(this.coroutineContext) { return withContext(this.coroutineContext) {
TIMProtocol.SERVER_IP.forEach { ip -> TIMProtocol.SERVER_IP.sortedBy { Random.nextInt() }.forEach { ip ->
bot.logger.info("Connecting server $ip") bot.logger.info("Connecting server $ip")
try { try {
socket = BotSocketAdapter(ip, configuration) withTimeout(3000) {
socket = BotSocketAdapter(ip, configuration)
}
} catch (e: Exception) { } catch (e: Exception) {
return@withContext LoginResult.NETWORK_UNAVAILABLE return@withContext LoginResult.NETWORK_UNAVAILABLE
} }
...@@ -447,6 +450,11 @@ internal class TIMBotNetworkHandler internal constructor(coroutineContext: Corou ...@@ -447,6 +450,11 @@ internal class TIMBotNetworkHandler internal constructor(coroutineContext: Corou
this.token00BA = packet.token00BA this.token00BA = packet.token00BA
if (packet.transmissionCompleted) { if (packet.transmissionCompleted) {
if (configuration.failOnCaptcha) {
loginResult.complete(LoginResult.CAPTCHA)
close()
return
}
val code = configuration.captchaSolver(bot, captchaCache!!) val code = configuration.captchaSolver(bot, captchaCache!!)
this.captchaCache = null this.captchaCache = null
......
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