Commit b4140b78 authored by Him188's avatar Him188

Extract `reloadFriendList()` outside `init()`

parent 6480e78b
...@@ -185,44 +185,18 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -185,44 +185,18 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
internal var pendingIncomingPackets: LockFreeLinkedList<KnownPacketFactories.IncomingPacket<*>>? = internal var pendingIncomingPackets: LockFreeLinkedList<KnownPacketFactories.IncomingPacket<*>>? =
LockFreeLinkedList() LockFreeLinkedList()
@OptIn(MiraiExperimentalAPI::class, ExperimentalTime::class) /**
override suspend fun init(): Unit = coroutineScope { * Don't use concurrently
check(bot.isActive) { "bot is dead therefore network can't init" } */
check(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't init" } suspend fun reloadFriendList() {
CancellationException("re-init").let { reInitCancellationException ->
bot.friends.delegate.clear { it.cancel(reInitCancellationException) }
bot.groups.delegate.clear { it.cancel(reInitCancellationException) }
}
if (!pendingEnabled) {
pendingIncomingPackets = LockFreeLinkedList()
_pendingEnabled.value = true
}
supervisorScope {
launch {
lateinit var loadFriends: suspend () -> Unit
// 不要用 fun, 不要 join declaration, 不要用 val, 编译失败警告 // 不要用 fun, 不要 join declaration, 不要用 val, 编译失败警告
loadFriends = suspend loadFriends@{
logger.info("开始加载好友信息") logger.info("开始加载好友信息")
var currentFriendCount = 0 var currentFriendCount = 0
var totalFriendCount: Short var totalFriendCount: Short
while (true) { while (true) {
val data = runCatching { val data = FriendList.GetFriendGroupList(
FriendList.GetFriendGroupList( bot.client, currentFriendCount, 150, 0, 0
bot.client,
currentFriendCount,
150,
0,
0
).sendAndExpect<FriendList.GetFriendGroupList.Response>(timeoutMillis = 5000, retry = 2) ).sendAndExpect<FriendList.GetFriendGroupList.Response>(timeoutMillis = 5000, retry = 2)
}.getOrElse {
logger.error("无法加载好友列表", it)
this@QQAndroidBotNetworkHandler.launch { delay(10.secondsToMillis); loadFriends() }
logger.error("稍后重试加载好友列表")
return@loadFriends
}
// self info // self info
data.selfInfo?.run { data.selfInfo?.run {
...@@ -233,7 +207,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -233,7 +207,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
totalFriendCount = data.totalFriendCount totalFriendCount = data.totalFriendCount
data.friendList.forEach { data.friendList.forEach {
// atomic add // atomic
bot.friends.delegate.addLast( bot.friends.delegate.addLast(
QQImpl(bot, bot.coroutineContext, it.friendUin, FriendInfoImpl(it)) QQImpl(bot, bot.coroutineContext, it.friendUin, FriendInfoImpl(it))
).also { currentFriendCount++ } ).also { currentFriendCount++ }
...@@ -247,9 +221,24 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -247,9 +221,24 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
logger.info { "好友列表加载完成, 共 ${currentFriendCount}个" } logger.info { "好友列表加载完成, 共 ${currentFriendCount}个" }
} }
loadFriends() @OptIn(MiraiExperimentalAPI::class, ExperimentalTime::class)
override suspend fun init(): Unit = coroutineScope {
check(bot.isActive) { "bot is dead therefore network can't init" }
check(this@QQAndroidBotNetworkHandler.isActive) { "network is dead therefore can't init" }
CancellationException("re-init").let { reInitCancellationException ->
bot.friends.delegate.clear { it.cancel(reInitCancellationException) }
bot.groups.delegate.clear { it.cancel(reInitCancellationException) }
}
if (!pendingEnabled) {
pendingIncomingPackets = LockFreeLinkedList()
_pendingEnabled.value = true
} }
supervisorScope {
this.launch { reloadFriendList() }
launch { launch {
try { try {
logger.info("开始加载群组列表与群成员列表") logger.info("开始加载群组列表与群成员列表")
...@@ -591,7 +580,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -591,7 +580,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
/** /**
* 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms) * 发送一个包, 并挂起直到接收到指定的返回包或超时(3000ms)
*/ */
suspend fun <E : Packet> OutgoingPacket.sendAndExpect(timeoutMillis: Long = 3000, retry: Int = 1): E { suspend fun <E : Packet> OutgoingPacket.sendAndExpect(timeoutMillis: Long = 3000, retry: Int = 2): E {
require(timeoutMillis > 100) { "timeoutMillis must > 100" } require(timeoutMillis > 100) { "timeoutMillis must > 100" }
require(retry >= 0) { "retry must >= 0" } require(retry >= 0) { "retry must >= 0" }
......
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