Commit 79d2290d authored by Mygod's avatar Mygod

Use thread with exception handling in GuardedProcess

parent 39242b69
...@@ -27,13 +27,14 @@ import com.github.shadowsocks.App.Companion.app ...@@ -27,13 +27,14 @@ import com.github.shadowsocks.App.Companion.app
import com.github.shadowsocks.BuildConfig import com.github.shadowsocks.BuildConfig
import com.github.shadowsocks.JniHelper import com.github.shadowsocks.JniHelper
import com.github.shadowsocks.utils.Commandline import com.github.shadowsocks.utils.Commandline
import com.github.shadowsocks.utils.thread
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.util.concurrent.Semaphore import java.util.concurrent.Semaphore
class GuardedProcess(private val cmd: List<String>) { class GuardedProcess(private val cmd: List<String>) {
companion object { companion object {
const val TAG = "GuardedProcess" private const val TAG = "GuardedProcess"
} }
private lateinit var guardThread: Thread private lateinit var guardThread: Thread
...@@ -42,7 +43,7 @@ class GuardedProcess(private val cmd: List<String>) { ...@@ -42,7 +43,7 @@ class GuardedProcess(private val cmd: List<String>) {
@Volatile @Volatile
private lateinit var process: Process private lateinit var process: Process
private fun streamLogger(input: InputStream, logger: (String, String) -> Int) = Thread { private fun streamLogger(input: InputStream, logger: (String, String) -> Int) = thread {
try { try {
input.bufferedReader().useLines { it.forEach { logger(TAG, it) } } input.bufferedReader().useLines { it.forEach { logger(TAG, it) } }
} catch (_: IOException) { } // ignore } catch (_: IOException) { } // ignore
...@@ -52,7 +53,7 @@ class GuardedProcess(private val cmd: List<String>) { ...@@ -52,7 +53,7 @@ class GuardedProcess(private val cmd: List<String>) {
val semaphore = Semaphore(1) val semaphore = Semaphore(1)
semaphore.acquire() semaphore.acquire()
var ioException: IOException? = null var ioException: IOException? = null
guardThread = Thread({ guardThread = thread(name = "GuardThread-" + cmd.first()) {
try { try {
var callback: (() -> Unit)? = null var callback: (() -> Unit)? = null
while (!isDestroyed) { while (!isDestroyed) {
...@@ -64,8 +65,8 @@ class GuardedProcess(private val cmd: List<String>) { ...@@ -64,8 +65,8 @@ class GuardedProcess(private val cmd: List<String>) {
.directory(app.filesDir) .directory(app.filesDir)
.start() .start()
streamLogger(process.inputStream, Log::i).start() streamLogger(process.inputStream, Log::i)
streamLogger(process.errorStream, Log::e).start() streamLogger(process.errorStream, Log::e)
if (callback == null) callback = onRestartCallback else callback() if (callback == null) callback = onRestartCallback else callback()
...@@ -87,8 +88,7 @@ class GuardedProcess(private val cmd: List<String>) { ...@@ -87,8 +88,7 @@ class GuardedProcess(private val cmd: List<String>) {
} finally { } finally {
semaphore.release() semaphore.release()
} }
}, "GuardThread-" + cmd.first()) }
guardThread.start()
semaphore.acquire() semaphore.acquire()
if (ioException != null) throw ioException!! if (ioException != null) throw ioException!!
return this return this
......
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