Fix observatory status update

This commit is contained in:
世界
2022-01-22 13:12:42 +08:00
parent 2c619167e7
commit feada7fd9a
3 changed files with 9 additions and 6 deletions

View File

@@ -36,6 +36,7 @@ import kotlinx.coroutines.runBlocking
import libcore.Libcore
import java.io.IOException
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.concurrent.timerTask
class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) : V2RayInstance(
@@ -113,7 +114,7 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) :
}
val updateTimer by lazy { Timer("Observatory Timer") }
val updateTasks by lazy { hashMapOf<Long, TimerTask>() }
val updateTasks by lazy { ConcurrentHashMap<Long, TimerTask>() }
fun sendObservatoryResult(statusPb: ByteArray?) {
if (statusPb == null || statusPb.isEmpty()) {
@@ -146,7 +147,7 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) :
Logs.d("Send result for #$profileId ${profile.displayName()}")
val groupId = profile.groupId
updateTasks.put(groupId, timerTask {
val task = timerTask {
synchronized(this@ProxyInstance) {
if (updateTasks[groupId] == this) {
service.data.binder.broadcast {
@@ -155,9 +156,9 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) :
updateTasks.remove(profile.groupId)
}
}
}.also {
updateTimer.schedule(it, 2333L)
})?.cancel()
}
updateTimer.schedule(task, 2000L)
updateTasks.put(groupId, task)?.cancel()
}
} else {
Logs.d("Profile with id #$profileId not found")
@@ -174,6 +175,7 @@ class ProxyInstance(profile: ProxyEntity, val service: BaseService.Interface) :
persistStats()
super.close()
if ((::updateTimer.getDelegate() as Lazy<*>).isInitialized()) updateTimer.cancel()
if (::observatoryJob.isInitialized) observatoryJob.cancel()
}

View File

@@ -496,6 +496,7 @@ fun Project.setupApp() {
dependencies {
add("implementation", kotlin("stdlib", "${rootProject.extra["kotlinVersion"]}"))
add("implementation", kotlin("reflect", "${rootProject.extra["kotlinVersion"]}"))
add("implementation", project(":plugin:api"))
add("testImplementation", "junit:junit:4.13.2")
add("androidTestImplementation", "androidx.test.ext:junit:1.1.3")