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) { } catch (e: SecurityException) {
// SecurityException would be thrown in requestNetwork on Android 6.0 thanks to Google's stupid bug // SecurityException would be thrown in requestNetwork on Android 6.0 thanks to Google's stupid bug
if (Build.VERSION.SDK_INT != 23) throw e if (Build.VERSION.SDK_INT != 23) throw e
callback.onFallback() GlobalScope.launch { callback.onFallback() }
return return
} }
registered = true registered = true

View File

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

View File

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