changed the coroutinescope's cancel and relaunch

This commit is contained in:
asxalex 2025-07-14 16:05:30 +08:00
parent 678e9de066
commit c7aed59f25
2 changed files with 12 additions and 5 deletions

View File

@ -40,7 +40,7 @@ import java.io.FileOutputStream
class PunchnetService : VpnService() , IfaceTun { class PunchnetService : VpnService() , IfaceTun {
private val TAG = "PunchnetService" private val TAG = "PunchnetService"
private val scope = CoroutineScope(Dispatchers.IO) private var scope = CoroutineScope(Dispatchers.IO)
private val routes: MutableList<RouteItem> = mutableListOf() private val routes: MutableList<RouteItem> = mutableListOf()
@ -50,7 +50,7 @@ class PunchnetService : VpnService() , IfaceTun {
var input: FileInputStream? = null var input: FileInputStream? = null
var output: FileOutputStream? = null var output: FileOutputStream? = null
override val arpTable = ARPTable(scope) override var arpTable = ARPTable(scope)
override val arpWaitList = ARPWaitList() override val arpWaitList = ARPWaitList()
var config: DeviceConfig = DeviceConfig(1400) var config: DeviceConfig = DeviceConfig(1400)
@ -126,11 +126,13 @@ class PunchnetService : VpnService() , IfaceTun {
private fun disconnect() { private fun disconnect() {
Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show() Toast.makeText(this, "stop vpn called", Toast.LENGTH_LONG).show()
stopForeground(STOP_FOREGROUND_REMOVE)
input?.close() input?.close()
output?.close() output?.close()
vpnDescriptor?.close() vpnDescriptor?.close()
stopForeground(true) vpnDescriptor = null
stopSelf() stopSelf()
} }
@ -180,8 +182,11 @@ class PunchnetService : VpnService() , IfaceTun {
return if (intent?.action == ACTION_DISCONNECT) { return if (intent?.action == ACTION_DISCONNECT) {
scope.cancel() scope.cancel()
scope = CoroutineScope(Dispatchers.IO)
arpTable = ARPTable(scope)
disconnect() disconnect()
START_STICKY START_NOT_STICKY
} else { } else {
val argument = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { val argument = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent?.getParcelableExtra("argument", PunchnetServiceArgument::class.java) intent?.getParcelableExtra("argument", PunchnetServiceArgument::class.java)
@ -215,6 +220,8 @@ class PunchnetService : VpnService() , IfaceTun {
override fun onDestroy() { override fun onDestroy() {
// scope.cancel() // scope.cancel()
// disconnect() // disconnect()
scope.cancel()
stopForeground(STOP_FOREGROUND_REMOVE)
super.onDestroy() super.onDestroy()
} }
} }

View File

@ -440,7 +440,7 @@ suspend fun initTCPConn(
} }
connectingChan?.send(ConnectingState.Connecting) connectingChan?.send(ConnectingState.Connecting)
TerminalLogger.debugf {"try connecting..."} TerminalLogger.debugf {"try connecting tcp..."}
val socket: Socket val socket: Socket
try { try {