Integrate ServerList into component system

This commit is contained in:
Him188 2021-04-17 18:30:20 +08:00
parent 3ec8a94096
commit 9ac0a8715c
4 changed files with 16 additions and 11 deletions

View File

@ -33,7 +33,6 @@ import net.mamoe.mirai.internal.contact.info.FriendInfoImpl
import net.mamoe.mirai.internal.contact.info.StrangerInfoImpl
import net.mamoe.mirai.internal.contact.uin
import net.mamoe.mirai.internal.network.handler.NetworkHandler
import net.mamoe.mirai.internal.network.handler.components.ServerListImpl
import net.mamoe.mirai.supervisorJob
import net.mamoe.mirai.utils.*
import kotlin.coroutines.CoroutineContext
@ -172,9 +171,6 @@ internal abstract class AbstractBot constructor(
// network
///////////////////////////////////////////////////////////////////////////
internal val serverListNew = ServerListImpl() // TODO: 2021/4/16 load server list from cache (add a provider)
// bot.bdhSyncer.loadServerListFromCache()
val network: NetworkHandler by lazy { createNetworkHandler(coroutineContext) }
final override suspend fun login() {

View File

@ -73,7 +73,6 @@ internal class QQAndroidBot constructor(
) : AbstractBot(configuration, account.id) {
override val bot: QQAndroidBot get() = this
val bdhSyncer: BdhSessionSyncer by lazy { BdhSessionSyncerImpl(configuration, serverListNew, network.logger) }
internal var firstLoginSucceed: Boolean = false
///////////////////////////////////////////////////////////////////////////
@ -92,6 +91,13 @@ internal class QQAndroidBot constructor(
set(StateObserver, debugConfiguration.stateObserver)
set(ContactCacheService, ContactCacheServiceImpl(bot))
set(ContactUpdater, ContactUpdaterImpl(bot, this))
set(BdhSessionSyncer, BdhSessionSyncerImpl(configuration, network.logger, this))
set(ServerList, ServerListImpl())
// TODO: 2021/4/16 load server list from cache (add a provider)
// bot.bdhSyncer.loadServerListFromCache()
}
}
@ -109,7 +115,7 @@ internal class QQAndroidBot constructor(
)
return SelectorNetworkHandler(
context,
FactoryKeepAliveNetworkHandlerSelector(NettyNetworkHandlerFactory, serverListNew, context)
FactoryKeepAliveNetworkHandlerSelector(NettyNetworkHandlerFactory, context)
) // We can move the factory to configuration but this is not necessary for now.
}

View File

@ -16,6 +16,7 @@ import kotlinx.serialization.builtins.SetSerializer
import net.mamoe.mirai.internal.network.JsonForCache
import net.mamoe.mirai.internal.network.ProtoBufForCache
import net.mamoe.mirai.internal.network.handler.component.ComponentKey
import net.mamoe.mirai.internal.network.handler.component.ComponentStorage
import net.mamoe.mirai.internal.network.handler.context.BdhSession
import net.mamoe.mirai.internal.utils.actualCacheDir
import net.mamoe.mirai.utils.BotConfiguration
@ -45,8 +46,8 @@ internal interface BdhSessionSyncer {
@OptIn(ExperimentalCoroutinesApi::class)
internal class BdhSessionSyncerImpl(
private val configuration: BotConfiguration,
private val serverList: ServerList,
private val logger: MiraiLogger,
private val componentStorage: ComponentStorage,
) : BdhSessionSyncer {
override var bdhSession: CompletableDeferred<BdhSession> = CompletableDeferred()
override val hasSession: Boolean
@ -74,7 +75,7 @@ internal class BdhSessionSyncerImpl(
logger.verbose("Loading server list from cache.")
kotlin.runCatching {
val list = JsonForCache.decodeFromString(ServerListSerializer, serverListCacheFile.readText())
serverList.setPreferred(list.map { ServerAddress(it.host, it.port) })
componentStorage[ServerList].setPreferred(list.map { ServerAddress(it.host, it.port) })
}.onFailure {
logger.warning("Error in loading server list from cache", it)
}
@ -110,7 +111,7 @@ internal class BdhSessionSyncerImpl(
serverListCacheFile.writeText(
JsonForCache.encodeToString(
ServerListSerializer,
serverList.getPreferred()
componentStorage[ServerList].getPreferred()
)
)
}.onFailure {

View File

@ -19,9 +19,11 @@ import net.mamoe.mirai.internal.network.handler.context.NetworkHandlerContext
*/
internal class FactoryKeepAliveNetworkHandlerSelector<H : NetworkHandler>(
private val factory: NetworkHandlerFactory<H>,
private val serverList: ServerList,
private val context: NetworkHandlerContext,
) : AbstractKeepAliveNetworkHandlerSelector<H>() {
override fun createInstance(): H =
factory.create(context, serverList.pollCurrent()?.toSocketAddress() ?: throw NoServerAvailableException())
factory.create(
context,
context[ServerList].pollCurrent()?.toSocketAddress() ?: throw NoServerAvailableException()
)
}