fix: trafficlooper

This commit is contained in:
arm64v8a 2023-06-04 10:54:10 +09:00
parent 478f826c6f
commit f8b111a683

View File

@ -18,7 +18,8 @@ class TrafficLooper
) { ) {
private var job: Job? = null private var job: Job? = null
private val items = mutableMapOf<Long, TrafficUpdater.TrafficLooperData>() // associate ent id private val idMap = mutableMapOf<Long, TrafficUpdater.TrafficLooperData>() // id to 1 data
private val tagMap = mutableMapOf<String, TrafficUpdater.TrafficLooperData>() // tag to 1 data
suspend fun stop() { suspend fun stop() {
job?.cancel() job?.cancel()
@ -27,7 +28,7 @@ class TrafficLooper
val traffic = mutableMapOf<Long, TrafficData>() val traffic = mutableMapOf<Long, TrafficData>()
data.proxy?.config?.trafficMap?.forEach { (_, ents) -> data.proxy?.config?.trafficMap?.forEach { (_, ents) ->
for (ent in ents) { for (ent in ents) {
val item = items[ent.id] ?: return@forEach val item = idMap[ent.id] ?: return@forEach
ent.rx = item.rx ent.rx = item.rx
ent.tx = item.tx ent.tx = item.tx
ProfileManager.updateProfile(ent) // update DB ProfileManager.updateProfile(ent) // update DB
@ -55,8 +56,8 @@ class TrafficLooper
fun selectMain(id: Long) { fun selectMain(id: Long) {
Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId") Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId")
val oldData = items[selectorNowId] val oldData = idMap[selectorNowId]
val newData = items[id] ?: return val newData = idMap[id] ?: return
oldData?.apply { oldData?.apply {
tag = selectorNowFakeTag tag = selectorNowFakeTag
ignore = true ignore = true
@ -90,7 +91,6 @@ class TrafficLooper
// for display // for display
val itemBypass = TrafficUpdater.TrafficLooperData(tag = TAG_BYPASS) val itemBypass = TrafficUpdater.TrafficLooperData(tag = TAG_BYPASS)
val itemMainList = mutableListOf<TrafficUpdater.TrafficLooperData>()
while (sc.isActive) { while (sc.isActive) {
delay(delayMs) delay(delayMs)
@ -98,8 +98,8 @@ class TrafficLooper
if (trafficUpdater == null) { if (trafficUpdater == null) {
if (!proxy.isInitialized()) continue if (!proxy.isInitialized()) continue
items.clear() idMap.clear()
items[-1] = itemBypass idMap[-1] = itemBypass
// //
val tags = hashSetOf(TAG_PROXY, TAG_BYPASS) val tags = hashSetOf(TAG_PROXY, TAG_BYPASS)
proxy.config.trafficMap.forEach { (tag, ents) -> proxy.config.trafficMap.forEach { (tag, ents) ->
@ -113,8 +113,8 @@ class TrafficLooper
txBase = ent.tx, txBase = ent.tx,
ignore = proxy.config.selectorGroupId >= 0L, ignore = proxy.config.selectorGroupId >= 0L,
) )
items[ent.id] = item idMap[ent.id] = item
itemMainList += item tagMap[tag] = item
Logs.d("traffic count $tag to ${ent.id}") Logs.d("traffic count $tag to ${ent.id}")
} }
} }
@ -123,7 +123,7 @@ class TrafficLooper
} }
// //
trafficUpdater = TrafficUpdater( trafficUpdater = TrafficUpdater(
box = proxy.box, items = items.values.toList() box = proxy.box, items = idMap.values.toList()
) )
proxy.box.setV2rayStats(tags.joinToString("\n")) proxy.box.setV2rayStats(tags.joinToString("\n"))
} }
@ -136,7 +136,7 @@ class TrafficLooper
var mainRxRate = 0L var mainRxRate = 0L
var mainTx = 0L var mainTx = 0L
var mainRx = 0L var mainRx = 0L
itemMainList.forEach { tagMap.forEach { (_, it) ->
mainTxRate += it.txRate mainTxRate += it.txRate
mainRxRate += it.rxRate mainRxRate += it.rxRate
mainTx += it.tx - it.txBase mainTx += it.tx - it.txBase
@ -161,7 +161,7 @@ class TrafficLooper
if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) { if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) {
b.cbSpeedUpdate(speed) b.cbSpeedUpdate(speed)
if (profileTrafficStatistics) { if (profileTrafficStatistics) {
items.forEach { (id, item) -> idMap.forEach { (id, item) ->
b.cbTrafficUpdate( b.cbTrafficUpdate(
TrafficData(id = id, rx = item.rx, tx = item.tx) // display TrafficData(id = id, rx = item.rx, tx = item.tx) // display
) )