Skip to content

Commit 2259fd3

Browse files
committed
optimize tcping
1 parent 353c54e commit 2259fd3

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.activity.result.contract.ActivityResultContracts
2222
import androidx.appcompat.widget.PopupMenu
2323
import androidx.appcompat.widget.SearchView
2424
import androidx.appcompat.widget.Toolbar
25+
import androidx.core.net.toUri
2526
import androidx.core.view.isGone
2627
import androidx.core.view.isVisible
2728
import androidx.core.view.size
@@ -94,7 +95,6 @@ import io.nekohasekai.sagernet.widget.UndoSnackbarManager
9495
import kotlinx.coroutines.DelicateCoroutinesApi
9596
import kotlinx.coroutines.Dispatchers
9697
import kotlinx.coroutines.Job
97-
import kotlinx.coroutines.delay
9898
import kotlinx.coroutines.isActive
9999
import kotlinx.coroutines.joinAll
100100
import kotlinx.coroutines.launch
@@ -105,6 +105,7 @@ import moe.matsuri.nb4a.Protocols.getProtocolColor
105105
import moe.matsuri.nb4a.proxy.anytls.AnyTLSSettingsActivity
106106
import moe.matsuri.nb4a.proxy.config.ConfigSettingActivity
107107
import moe.matsuri.nb4a.proxy.shadowtls.ShadowTLSSettingsActivity
108+
import moe.matsuri.nb4a.ui.ConnectionTestNotification
108109
import okhttp3.internal.closeQuietly
109110
import java.net.InetSocketAddress
110111
import java.net.Socket
@@ -113,9 +114,6 @@ import java.util.Collections
113114
import java.util.concurrent.ConcurrentLinkedQueue
114115
import java.util.concurrent.atomic.AtomicInteger
115116
import java.util.zip.ZipInputStream
116-
import kotlin.collections.set
117-
import androidx.core.net.toUri
118-
import moe.matsuri.nb4a.ui.ConnectionTestNotification
119117

120118
class ConfigurationFragment @JvmOverloads constructor(
121119
val select: Boolean = false, val selectedItem: ProxyEntity? = null, val titleRes: Int = 0
@@ -610,22 +608,23 @@ class ConfigurationFragment @JvmOverloads constructor(
610608
lateinit var cancel: () -> Unit
611609
lateinit var minimize: () -> Unit
612610

611+
var dialogHidden = false
613612
var notification: ConnectionTestNotification? = null
614613

615-
val fragment by lazy { getCurrentGroupFragment() }
616-
val results = Collections.synchronizedList(mutableListOf<ProxyEntity?>())
614+
val results = Collections.synchronizedSet(mutableSetOf<ProxyEntity>())
617615
var proxyN = 0
618616
val finishedN = AtomicInteger(0)
619617

620-
suspend fun insert(profile: ProxyEntity?) {
621-
results.add(profile)
622-
}
623-
624618
fun update(profile: ProxyEntity) {
619+
results.add(profile)
625620
runOnMainDispatcher {
626621
val context = context ?: return@runOnMainDispatcher
627622
if (!isAdded) return@runOnMainDispatcher
628623

624+
val progress = finishedN.addAndGet(1)
625+
notification?.updateNotification(progress, proxyN, progress >= proxyN)
626+
if (dialogHidden) return@runOnMainDispatcher
627+
629628
var profileStatusText: String? = null
630629
var profileStatusColor = 0
631630

@@ -675,11 +674,8 @@ class ConfigurationFragment @JvmOverloads constructor(
675674
append("\n")
676675
}
677676

678-
val progress = finishedN.addAndGet(1)
679677
binding.nowTesting.text = text
680678
binding.progress.text = "$progress / $proxyN"
681-
682-
notification?.updateNotification(progress, proxyN, progress >= proxyN)
683679
}
684680
}
685681

@@ -695,34 +691,26 @@ class ConfigurationFragment @JvmOverloads constructor(
695691
val group = DataStore.currentGroup()
696692

697693
val mainJob = runOnDefaultDispatcher {
698-
val profilesUnfiltered = SagerDatabase.proxyDao.getByGroup(group.id)
699-
test.proxyN = profilesUnfiltered.size
700-
val profiles = ConcurrentLinkedQueue(profilesUnfiltered)
694+
val profilesList = SagerDatabase.proxyDao.getByGroup(group.id).filter {
695+
if (icmpPing) {
696+
if (it.requireBean().canICMPing()) {
697+
return@filter true
698+
}
699+
} else {
700+
if (it.requireBean().canTCPing()) {
701+
return@filter true
702+
}
703+
}
704+
return@filter false
705+
}
706+
test.proxyN = profilesList.size
707+
val profiles = ConcurrentLinkedQueue(profilesList)
701708
repeat(DataStore.connectionTestConcurrent) {
702709
testJobs.add(launch(Dispatchers.IO) {
703710
while (isActive) {
704711
val profile = profiles.poll() ?: break
705712

706-
if (icmpPing) {
707-
if (!profile.requireBean().canICMPing()) {
708-
profile.status = -1
709-
profile.error =
710-
app.getString(R.string.connection_test_icmp_ping_unavailable)
711-
test.insert(profile)
712-
continue
713-
}
714-
} else {
715-
if (!profile.requireBean().canTCPing()) {
716-
profile.status = -1
717-
profile.error =
718-
app.getString(R.string.connection_test_tcp_ping_unavailable)
719-
test.insert(profile)
720-
continue
721-
}
722-
}
723-
724713
profile.status = 0
725-
test.insert(profile)
726714
var address = profile.requireBean().serverAddress
727715
if (!address.isIpAddress()) {
728716
try {
@@ -811,7 +799,7 @@ class ConfigurationFragment @JvmOverloads constructor(
811799
test.cancel = {
812800
dialog.dismiss()
813801
runOnDefaultDispatcher {
814-
test.results.filterNotNull().forEach {
802+
test.results.forEach {
815803
try {
816804
ProfileManager.updateProfile(it)
817805
} catch (e: Exception) {
@@ -825,6 +813,7 @@ class ConfigurationFragment @JvmOverloads constructor(
825813
}
826814
}
827815
test.minimize = {
816+
test.dialogHidden = true
828817
test.notification = ConnectionTestNotification(
829818
dialog.context,
830819
"[${group.displayName()}] ${getString(R.string.connection_test)}"
@@ -842,16 +831,15 @@ class ConfigurationFragment @JvmOverloads constructor(
842831
val group = DataStore.currentGroup()
843832

844833
val mainJob = runOnDefaultDispatcher {
845-
val profilesUnfiltered = SagerDatabase.proxyDao.getByGroup(group.id)
846-
test.proxyN = profilesUnfiltered.size
847-
val profiles = ConcurrentLinkedQueue(profilesUnfiltered)
834+
val profilesList = SagerDatabase.proxyDao.getByGroup(group.id)
835+
test.proxyN = profilesList.size
836+
val profiles = ConcurrentLinkedQueue(profilesList)
848837
repeat(DataStore.connectionTestConcurrent) {
849838
testJobs.add(launch(Dispatchers.IO) {
850839
val urlTest = UrlTest() // note: this is NOT in bg process
851840
while (isActive) {
852841
val profile = profiles.poll() ?: break
853842
profile.status = 0
854-
test.insert(profile)
855843

856844
try {
857845
val result = urlTest.doTest(profile)
@@ -879,7 +867,7 @@ class ConfigurationFragment @JvmOverloads constructor(
879867
test.cancel = {
880868
dialog.dismiss()
881869
runOnDefaultDispatcher {
882-
test.results.filterNotNull().forEach {
870+
test.results.forEach {
883871
try {
884872
ProfileManager.updateProfile(it)
885873
} catch (e: Exception) {
@@ -893,6 +881,7 @@ class ConfigurationFragment @JvmOverloads constructor(
893881
}
894882
}
895883
test.minimize = {
884+
test.dialogHidden = true
896885
test.notification = ConnectionTestNotification(
897886
dialog.context,
898887
"[${group.displayName()}] ${getString(R.string.connection_test)}"

0 commit comments

Comments
 (0)