From f8b111a68362c4d5dc63158a19d06ce7fa1126c7 Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Sun, 4 Jun 2023 10:54:10 +0900 Subject: [PATCH] fix: trafficlooper --- .../sagernet/bg/proto/TrafficLooper.kt | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt index d84c62d..362e805 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt @@ -18,7 +18,8 @@ class TrafficLooper ) { private var job: Job? = null - private val items = mutableMapOf() // associate ent id + private val idMap = mutableMapOf() // id to 1 data + private val tagMap = mutableMapOf() // tag to 1 data suspend fun stop() { job?.cancel() @@ -27,7 +28,7 @@ class TrafficLooper val traffic = mutableMapOf() data.proxy?.config?.trafficMap?.forEach { (_, ents) -> for (ent in ents) { - val item = items[ent.id] ?: return@forEach + val item = idMap[ent.id] ?: return@forEach ent.rx = item.rx ent.tx = item.tx ProfileManager.updateProfile(ent) // update DB @@ -55,8 +56,8 @@ class TrafficLooper fun selectMain(id: Long) { Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId") - val oldData = items[selectorNowId] - val newData = items[id] ?: return + val oldData = idMap[selectorNowId] + val newData = idMap[id] ?: return oldData?.apply { tag = selectorNowFakeTag ignore = true @@ -90,7 +91,6 @@ class TrafficLooper // for display val itemBypass = TrafficUpdater.TrafficLooperData(tag = TAG_BYPASS) - val itemMainList = mutableListOf() while (sc.isActive) { delay(delayMs) @@ -98,8 +98,8 @@ class TrafficLooper if (trafficUpdater == null) { if (!proxy.isInitialized()) continue - items.clear() - items[-1] = itemBypass + idMap.clear() + idMap[-1] = itemBypass // val tags = hashSetOf(TAG_PROXY, TAG_BYPASS) proxy.config.trafficMap.forEach { (tag, ents) -> @@ -113,8 +113,8 @@ class TrafficLooper txBase = ent.tx, ignore = proxy.config.selectorGroupId >= 0L, ) - items[ent.id] = item - itemMainList += item + idMap[ent.id] = item + tagMap[tag] = item Logs.d("traffic count $tag to ${ent.id}") } } @@ -123,7 +123,7 @@ class TrafficLooper } // trafficUpdater = TrafficUpdater( - box = proxy.box, items = items.values.toList() + box = proxy.box, items = idMap.values.toList() ) proxy.box.setV2rayStats(tags.joinToString("\n")) } @@ -136,7 +136,7 @@ class TrafficLooper var mainRxRate = 0L var mainTx = 0L var mainRx = 0L - itemMainList.forEach { + tagMap.forEach { (_, it) -> mainTxRate += it.txRate mainRxRate += it.rxRate mainTx += it.tx - it.txBase @@ -161,7 +161,7 @@ class TrafficLooper if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) { b.cbSpeedUpdate(speed) if (profileTrafficStatistics) { - items.forEach { (id, item) -> + idMap.forEach { (id, item) -> b.cbTrafficUpdate( TrafficData(id = id, rx = item.rx, tx = item.tx) // display )