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

View File

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