Commit e8127920 authored by Mygod's avatar Mygod

Disconnect from main thread properly

parent 7ef7644e
......@@ -31,6 +31,7 @@ import com.github.shadowsocks.plugin.PluginConfiguration
import com.github.shadowsocks.plugin.PluginManager
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.disconnectFromMain
import com.github.shadowsocks.utils.parseNumericAddress
import com.github.shadowsocks.utils.signaturesCompat
import kotlinx.coroutines.Dispatchers
......@@ -70,7 +71,7 @@ class ProxyInstance(val profile: Profile, private val route: String = profile.ro
}
}
} finally {
conn.disconnect()
conn.disconnectFromMain()
}.split('|').toMutableList()
proxies.shuffle()
val proxy = proxies.first().split(':')
......
......@@ -30,6 +30,7 @@ import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.core.R
import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Key
import com.github.shadowsocks.utils.disconnectFromMain
import kotlinx.coroutines.*
import java.io.IOException
import java.net.HttpURLConnection
......@@ -109,7 +110,7 @@ class HttpsTest : ViewModel() {
private fun cancelTest() = running?.let { (conn, job) ->
job.cancel() // ensure job is cancelled before interrupting
conn.disconnect()
conn.disconnectFromMain()
running = null
}
......
......@@ -36,6 +36,10 @@ import android.util.TypedValue
import androidx.annotation.AttrRes
import androidx.preference.Preference
import com.crashlytics.android.Crashlytics
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.net.HttpURLConnection
import java.net.InetAddress
private val parseNumericAddress by lazy {
......@@ -51,6 +55,10 @@ private val parseNumericAddress by lazy {
fun String?.parseNumericAddress(): InetAddress? = Os.inet_pton(OsConstants.AF_INET, this)
?: Os.inet_pton(OsConstants.AF_INET6, this)?.let { parseNumericAddress.invoke(null, this) as InetAddress }
fun HttpURLConnection.disconnectFromMain() {
if (Build.VERSION.SDK_INT >= 26) disconnect() else GlobalScope.launch(Dispatchers.IO) { disconnect() }
}
fun parsePort(str: String?, default: Int, min: Int = 1025): Int {
val value = str?.toIntOrNull() ?: default
return if (value < min || value > 65535) default else value
......
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