Commit d240f2bd authored by Mygod's avatar Mygod

Ensure broadcast is finished

parent b4f97b88
...@@ -94,7 +94,7 @@ object BaseService { ...@@ -94,7 +94,7 @@ object BaseService {
} }
class Binder(private var data: Data? = null) : IShadowsocksService.Stub(), CoroutineScope, AutoCloseable { class Binder(private var data: Data? = null) : IShadowsocksService.Stub(), CoroutineScope, AutoCloseable {
val callbacks = object : RemoteCallbackList<IShadowsocksServiceCallback>() { private val callbacks = object : RemoteCallbackList<IShadowsocksServiceCallback>() {
override fun onCallbackDied(callback: IShadowsocksServiceCallback?, cookie: Any?) { override fun onCallbackDied(callback: IShadowsocksServiceCallback?, cookie: Any?) {
super.onCallbackDied(callback, cookie) super.onCallbackDied(callback, cookie)
stopListeningForBandwidth(callback ?: return) stopListeningForBandwidth(callback ?: return)
...@@ -112,7 +112,9 @@ object BaseService { ...@@ -112,7 +112,9 @@ object BaseService {
} }
private fun broadcast(work: (IShadowsocksServiceCallback) -> Unit) { private fun broadcast(work: (IShadowsocksServiceCallback) -> Unit) {
repeat(callbacks.beginBroadcast()) { val count = callbacks.beginBroadcast()
try {
repeat(count) {
try { try {
work(callbacks.getBroadcastItem(it)) work(callbacks.getBroadcastItem(it))
} catch (_: RemoteException) { } catch (_: RemoteException) {
...@@ -120,8 +122,10 @@ object BaseService { ...@@ -120,8 +122,10 @@ object BaseService {
printLog(e) printLog(e)
} }
} }
} finally {
callbacks.finishBroadcast() callbacks.finishBroadcast()
} }
}
private suspend fun loop() { private suspend fun loop() {
while (true) { while (true) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment