package com.github.kr328.clash.util import android.os.DeadObjectException import com.github.kr328.clash.common.log.Log import com.github.kr328.clash.remote.Remote import com.github.kr328.clash.service.remote.IClashManager import com.github.kr328.clash.service.remote.IProfileManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import kotlin.coroutines.CoroutineContext suspend fun withClash( context: CoroutineContext = Dispatchers.IO, block: suspend IClashManager.() -> T ): T { while (true) { val remote = Remote.service.remote.get() val client = remote.clash() try { return withContext(context) { client.block() } } catch (e: DeadObjectException) { Log.w("Remote services panic") Remote.service.remote.reset(remote) } } } suspend fun withProfile( context: CoroutineContext = Dispatchers.IO, block: suspend IProfileManager.() -> T ): T { while (true) { val remote = Remote.service.remote.get() val client = remote.profile() try { return withContext(context) { client.block() } } catch (e: DeadObjectException) { Log.w("Remote services panic") Remote.service.remote.reset(remote) } } }