@@ -17,8 +17,10 @@ import kotlinx.coroutines.CoroutineScope
1717import kotlinx.coroutines.Dispatchers
1818import kotlinx.coroutines.SupervisorJob
1919import kotlinx.coroutines.launch
20+ import kotlinx.coroutines.suspendCancellableCoroutine
2021import kotlinx.coroutines.sync.withLock
2122import java.util.UUID
23+ import kotlin.coroutines.resume
2224
2325class RemoteService : Service (),
2426 CoroutineScope by CoroutineScope (SupervisorJob () + Dispatchers .Default ) {
@@ -75,11 +77,22 @@ class RemoteService : Service(),
7577 private val binder = object : IRemoteInterface .Stub () {
7678 override fun invokeAction (data : String , callback : ICallbackInterface ) {
7779 Core .invokeAction(data) {
78- runCatching {
79- val chunks = it?.chunkedForAidl() ? : listOf ()
80- val totalSize = chunks.size
81- chunks.forEachIndexed { index, chunk ->
82- callback.onResult(chunk, totalSize - 1 == index)
80+ launch {
81+ runCatching {
82+ val chunks = it?.chunkedForAidl() ? : listOf ()
83+ for ((index, chunk) in chunks.withIndex()) {
84+ suspendCancellableCoroutine { cont ->
85+ callback.onResult(
86+ chunk,
87+ index == chunks.lastIndex,
88+ object : IAckInterface .Stub () {
89+ override fun onAck () {
90+ cont.resume(Unit )
91+ }
92+ },
93+ )
94+ }
95+ }
8396 }
8497 }
8598 }
@@ -89,6 +102,7 @@ class RemoteService : Service(),
89102 State .notificationParamsFlow.tryEmit(params)
90103 }
91104
105+
92106 override fun startService (
93107 options : VpnOptions ,
94108 runtime : Long ,
@@ -106,12 +120,24 @@ class RemoteService : Service(),
106120 GlobalState .log(" RemoveEventListener ${eventListener == null } " )
107121 when (eventListener != null ) {
108122 true -> Core .callSetEventListener {
109- runCatching {
110- val id = UUID .randomUUID().toString()
111- val chunks = it?.chunkedForAidl() ? : listOf ()
112- val totalSize = chunks.size
113- chunks.forEachIndexed { index, chunk ->
114- eventListener.onEvent(id, chunk, totalSize - 1 == index)
123+ launch {
124+ runCatching {
125+ val id = UUID .randomUUID().toString()
126+ val chunks = it?.chunkedForAidl() ? : listOf ()
127+ for ((index, chunk) in chunks.withIndex()) {
128+ suspendCancellableCoroutine { cont ->
129+ eventListener.onEvent(
130+ id,
131+ chunk,
132+ index == chunks.lastIndex,
133+ object : IAckInterface .Stub () {
134+ override fun onAck () {
135+ cont.resume(Unit )
136+ }
137+ },
138+ )
139+ }
140+ }
115141 }
116142 }
117143 }
0 commit comments