修复休眠恢复的问题

This commit is contained in:
anlicheng 2026-05-04 20:45:12 +08:00
parent dcbaffe70e
commit ad6f7a8c42
3 changed files with 24 additions and 39 deletions

View File

@ -28,22 +28,22 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
return return
} }
let rsaCipher = try! CCRSACipher(keySize: 1024) if self.runtimeEnv == nil {
self.runtimeEnv = SDLRuntimeEnvironment(config: config, rsaCipher: rsaCipher, provider: self) let rsaCipher = try! CCRSACipher(keySize: 1024)
Task { self.runtimeEnv = SDLRuntimeEnvironment(config: config, rsaCipher: rsaCipher, provider: self)
await self.runtimeEnv?.submitCommand(command: .start(completion: { err in self.runtimeEnv?.run()
completionHandler(err)
}))
} }
self.runtimeEnv?.submitCommand(command: .start(completion: { err in
completionHandler(err)
}))
} }
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) { override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
// Add code here to start the process of stopping the tunnel. // Add code here to start the process of stopping the tunnel.
Task { self.runtimeEnv?.submitCommand(command: .stop(completion: {
await self.runtimeEnv?.submitCommand(command: .stop(completion: { completionHandler()
completionHandler() }))
}))
}
} }
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) { override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
@ -66,27 +66,23 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
override func sleep(completionHandler: @escaping () -> Void) { override func sleep(completionHandler: @escaping () -> Void) {
// Add code here to get ready to sleep. // Add code here to get ready to sleep.
Task { self.runtimeEnv?.submitCommand(command: .stop(completion: {
await self.runtimeEnv?.submitCommand(command: .stop(completion: { SDLLogger.log("[PacketTunnelProvider] sleep")
SDLLogger.log("[PacketTunnelProvider] sleep") }))
}))
}
completionHandler() completionHandler()
} }
override func wake() { override func wake() {
SDLLogger.log("[PacketTunnelProvider] wake up!!!!!!!") SDLLogger.log("[PacketTunnelProvider] wake up!!!!!!!")
// Add code here to wake up. // Add code here to wake up.
Task { //
// self.runtimeEnv?.submitCommand(command: .start(completion: { err in
await self.runtimeEnv?.submitCommand(command: .start(completion: { err in SDLLogger.log("[PacketTunnelProvider] wakeup and try start")
SDLLogger.log("[PacketTunnelProvider] wakeup and try start") }))
}))
}
} }
private func handleAppRequest(message: AppRequest) async throws -> Data? { private func handleAppRequest(message: AppRequest) async throws -> Data? {
guard let contextActor = await self.runtimeEnv?.getContextActor() else { guard let contextActor = self.runtimeEnv?.getContextActor() else {
throw TunnelError.invalidContext throw TunnelError.invalidContext
} }

View File

@ -34,7 +34,7 @@ actor SDLContextActor {
} }
} }
var config: SDLConfiguration private var config: SDLConfiguration
// nat // nat
var natType: SDLNATProberActor.NatType = .blocked var natType: SDLNATProberActor.NatType = .blocked
@ -119,13 +119,7 @@ actor SDLContextActor {
} }
public func start() async { public func start() async {
await self.startRuntime(resetNotifier: true) self.prepareTunnelNotifier()
}
private func startRuntime(resetNotifier: Bool) async {
if resetNotifier {
self.prepareTunnelNotifier()
}
// arp // arp
await self.puncherActor.start() await self.puncherActor.start()

View File

@ -5,12 +5,12 @@ enum SDLRuntimeEnvironmentCommand {
case stop(completion: @Sendable () -> Void) case stop(completion: @Sendable () -> Void)
} }
actor SDLRuntimeEnvironment { final class SDLRuntimeEnvironment {
private enum State { private enum State {
case idle case idle
case running case running
} }
private var state: State = .idle private var state: State = .idle
private var contextActor: SDLContextActor? private var contextActor: SDLContextActor?
@ -104,14 +104,9 @@ actor SDLRuntimeEnvironment {
} }
} }
func shutdown() { deinit {
self.commandCont.finish() self.commandCont.finish()
self.commandTask?.cancel() self.commandTask?.cancel()
self.commandTask = nil self.commandTask = nil
} }
deinit {
self.commandCont.finish()
self.commandTask?.cancel()
}
} }