Commit f49d3cb4 authored by Mygod's avatar Mygod

Save LocalSocket instance from getting GC

parent baa2fdeb
......@@ -27,7 +27,6 @@ import android.util.Log
import com.github.shadowsocks.App.Companion.app
import java.io.File
import java.io.IOException
import java.util.concurrent.atomic.AtomicReference
abstract class LocalSocketListener(protected val tag: String) : Thread() {
init {
......@@ -35,24 +34,24 @@ abstract class LocalSocketListener(protected val tag: String) : Thread() {
}
protected abstract val socketFile: File
private val serverSocket = AtomicReference<LocalServerSocket?>()
@Volatile
private var running = true
/**
* Inherited class do not need to close input/output streams as they will be closed automatically.
*/
protected abstract fun accept(socket: LocalSocket)
override fun run() {
override final fun run() {
Thread.sleep(500)
try {
val localSocket = LocalSocket()
LocalSocket().use { localSocket ->
val serverSocket = try {
localSocket.bind(LocalSocketAddress(socketFile.absolutePath, LocalSocketAddress.Namespace.FILESYSTEM))
serverSocket.set(LocalServerSocket(localSocket.fileDescriptor))
LocalServerSocket(localSocket.fileDescriptor)
} catch (e: IOException) {
Log.e(tag, "unable to bind", e)
return
}
while (true) {
val serverSocket = serverSocket.get() ?: return
while (running) {
try {
serverSocket.accept()
} catch (e: IOException) {
......@@ -62,11 +61,10 @@ abstract class LocalSocketListener(protected val tag: String) : Thread() {
}?.use(this::accept)
}
}
}
fun stopThread() {
val old = serverSocket.getAndSet(null) ?: return
try {
old.close()
} catch (_: Exception) { } // ignore
running = false
interrupt()
}
}
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