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 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() {
job?.cancel()
@ -27,7 +28,7 @@ class TrafficLooper
val traffic = mutableMapOf<Long, TrafficData>()
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<TrafficUpdater.TrafficLooperData>()
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
)