optimize traffic looper

This commit is contained in:
arm64v8a 2023-05-26 15:34:01 +09:00
parent ccbc71c8a9
commit 9d78e4f287
3 changed files with 31 additions and 31 deletions

View File

@ -48,6 +48,8 @@ class ServiceNotification(
} }
} }
var listenPostSpeed = true
fun postNotificationSpeedUpdate(stats: SpeedDisplayData) { fun postNotificationSpeedUpdate(stats: SpeedDisplayData) {
builder.apply { builder.apply {
if (showDirectSpeed) { if (showDirectSpeed) {
@ -119,7 +121,6 @@ class ServiceNotification(
Theme.apply(service) Theme.apply(service)
builder.color = service.getColorAttr(R.attr.colorPrimary) builder.color = service.getColorAttr(R.attr.colorPrimary)
updateCallback(SagerNet.power.isInteractive)
service.registerReceiver(this, IntentFilter().apply { service.registerReceiver(this, IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_ON) addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_SCREEN_OFF) addAction(Intent.ACTION_SCREEN_OFF)
@ -167,23 +168,19 @@ class ServiceNotification(
} }
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
if (service.data.state == BaseService.State.Connected) updateCallback(intent.action == Intent.ACTION_SCREEN_ON) if (service.data.state == BaseService.State.Connected) {
listenPostSpeed = intent.action == Intent.ACTION_SCREEN_ON
}
} }
var listenPostSpeed = false
private fun updateCallback(screenOn: Boolean) {
if (DataStore.speedInterval == 0) return
listenPostSpeed = screenOn
}
private fun show() = (service as Service).startForeground(notificationId, builder.build()) private fun show() = (service as Service).startForeground(notificationId, builder.build())
private fun update() = private fun update() =
NotificationManagerCompat.from(service as Service).notify(notificationId, builder.build()) NotificationManagerCompat.from(service as Service).notify(notificationId, builder.build())
fun destroy() { fun destroy() {
listenPostSpeed = false
(service as Service).stopForeground(true) (service as Service).stopForeground(true)
service.unregisterReceiver(this) service.unregisterReceiver(this)
updateCallback(false)
} }
} }

View File

@ -89,9 +89,8 @@ class TrafficLooper
var proxy: ProxyInstance? var proxy: ProxyInstance?
// for display // for display
var itemMain: TrafficUpdater.TrafficLooperData? = null val itemBypass = TrafficUpdater.TrafficLooperData(tag = TAG_BYPASS)
var itemMainBase: TrafficUpdater.TrafficLooperData? = null val itemMainList = mutableListOf<TrafficUpdater.TrafficLooperData>()
var itemBypass: TrafficUpdater.TrafficLooperData? = null
while (sc.isActive) { while (sc.isActive) {
delay(delayMs) delay(delayMs)
@ -100,7 +99,6 @@ class TrafficLooper
if (trafficUpdater == null) { if (trafficUpdater == null) {
if (!proxy.isInitialized()) continue if (!proxy.isInitialized()) continue
items.clear() items.clear()
itemBypass = TrafficUpdater.TrafficLooperData(tag = "bypass")
items[-1] = itemBypass items[-1] = itemBypass
// //
val tags = hashSetOf(TAG_PROXY, TAG_BYPASS) val tags = hashSetOf(TAG_PROXY, TAG_BYPASS)
@ -111,25 +109,16 @@ class TrafficLooper
tag = tag, tag = tag,
rx = ent.rx, rx = ent.rx,
tx = ent.tx, tx = ent.tx,
rxBase = ent.rx,
txBase = ent.tx,
ignore = proxy.config.selectorGroupId >= 0L, ignore = proxy.config.selectorGroupId >= 0L,
) )
if (tag == TAG_PROXY && itemMain == null) {
itemMain = item
itemMainBase = TrafficUpdater.TrafficLooperData(
tag = tag,
rx = ent.rx,
tx = ent.tx,
)
Logs.d("traffic count $tag to main to ${ent.id}")
}
items[ent.id] = item items[ent.id] = item
itemMainList += item
Logs.d("traffic count $tag to ${ent.id}") Logs.d("traffic count $tag to ${ent.id}")
} }
} }
if (proxy.config.selectorGroupId >= 0L) { if (proxy.config.selectorGroupId >= 0L) {
itemMain = TrafficUpdater.TrafficLooperData(tag = TAG_PROXY)
itemMainBase = TrafficUpdater.TrafficLooperData(tag = TAG_PROXY)
items[-2] = itemMain!!
selectMain(proxy.config.mainEntId) selectMain(proxy.config.mainEntId)
} }
// //
@ -142,14 +131,26 @@ class TrafficLooper
trafficUpdater.updateAll() trafficUpdater.updateAll()
if (!sc.isActive) return if (!sc.isActive) return
// add all non-bypass to "main"
var mainTxRate = 0L
var mainRxRate = 0L
var mainTx = 0L
var mainRx = 0L
itemMainList.forEach {
mainTxRate += it.txRate
mainRxRate += it.rxRate
mainTx += it.tx - it.txBase
mainRx += it.rx - it.rxBase
}
// speed // speed
val speed = SpeedDisplayData( val speed = SpeedDisplayData(
itemMain!!.txRate, mainTxRate,
itemMain!!.rxRate, mainRxRate,
if (showDirectSpeed) itemBypass!!.txRate else 0L, if (showDirectSpeed) itemBypass.txRate else 0L,
if (showDirectSpeed) itemBypass!!.rxRate else 0L, if (showDirectSpeed) itemBypass.rxRate else 0L,
itemMain!!.tx - itemMainBase!!.tx, mainTx,
itemMain!!.rx - itemMainBase!!.rx mainRx
) )
// broadcast (MainActivity) // broadcast (MainActivity)

View File

@ -10,6 +10,8 @@ class TrafficUpdater(
var tag: String, var tag: String,
var tx: Long = 0, var tx: Long = 0,
var rx: Long = 0, var rx: Long = 0,
var txBase: Long = 0,
var rxBase: Long = 0,
var txRate: Long = 0, var txRate: Long = 0,
var rxRate: Long = 0, var rxRate: Long = 0,
var lastUpdate: Long = 0, var lastUpdate: Long = 0,