Prevent calling callback in synchronized

This commit is contained in:
Mygod
2020-04-23 03:54:49 +08:00
parent 099c6b2a73
commit 48ca2c1623
3 changed files with 10 additions and 6 deletions

View File

@@ -85,7 +85,7 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
} catch (e: SecurityException) {
// SecurityException would be thrown in requestNetwork on Android 6.0 thanks to Google's stupid bug
if (Build.VERSION.SDK_INT != 23) throw e
callback.onFallback()
GlobalScope.launch { callback.onFallback() }
return
}
registered = true

View File

@@ -9,8 +9,11 @@ class InterfaceMonitor(val iface: String) : UpstreamMonitor() {
val old = currentIface != null
if (present == old) return
currentIface = if (present) iface else null
if (present) {
val lp = currentLinkProperties ?: return@synchronized
if (present) Pair(iface, currentLinkProperties) else null
}.let { pair ->
if (pair != null) {
val (iface, lp) = pair
lp ?: return
callbacks.forEach { it.onAvailable(iface, lp) }
} else callbacks.forEach { it.onLost() }
}

View File

@@ -15,14 +15,15 @@ class IpNeighbourMonitor private constructor() : IpMonitor() {
var instance: IpNeighbourMonitor? = null
fun registerCallback(callback: Callback) = synchronized(callbacks) {
if (!callbacks.add(callback)) return@synchronized
if (!callbacks.add(callback)) return@synchronized null
var monitor = instance
if (monitor == null) {
monitor = IpNeighbourMonitor()
instance = monitor
monitor.flush()
} else callback.onIpNeighbourAvailable(monitor.neighbours.values)
}
null
} else monitor.neighbours.values
}?.let { callback.onIpNeighbourAvailable(it) }
fun unregisterCallback(callback: Callback) = synchronized(callbacks) {
if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return@synchronized
instance?.destroy()