解决网络状态变化的问题
This commit is contained in:
parent
971fa64685
commit
9a3fb6a8d6
@ -39,17 +39,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
|||||||
self.contextActor = contextActor
|
self.contextActor = contextActor
|
||||||
await contextActor.start()
|
await contextActor.start()
|
||||||
|
|
||||||
// 这里的事件,只有启动相关的事件
|
|
||||||
for await event in await contextActor.events() {
|
|
||||||
switch event {
|
|
||||||
case .ready:
|
|
||||||
completionHandler(nil)
|
completionHandler(nil)
|
||||||
case .failed(let err):
|
|
||||||
await self.contextActor?.stop()
|
|
||||||
self.contextActor = nil
|
|
||||||
completionHandler(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,11 +9,6 @@ import Foundation
|
|||||||
import NetworkExtension
|
import NetworkExtension
|
||||||
import NIOCore
|
import NIOCore
|
||||||
|
|
||||||
public enum SDLContextEvent: Sendable {
|
|
||||||
case ready
|
|
||||||
case failed(Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 上下文环境变量,全局共享
|
// 上下文环境变量,全局共享
|
||||||
/*
|
/*
|
||||||
1. 处理rsa的加解密逻辑
|
1. 处理rsa的加解密逻辑
|
||||||
@ -41,10 +36,6 @@ actor SDLContextActor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 内部事件
|
|
||||||
private let eventStream: AsyncStream<SDLContextEvent>
|
|
||||||
private let eventContinuation: AsyncStream<SDLContextEvent>.Continuation
|
|
||||||
|
|
||||||
private var readyState: ReadyState = .idle
|
private var readyState: ReadyState = .idle
|
||||||
|
|
||||||
var config: SDLConfiguration
|
var config: SDLConfiguration
|
||||||
@ -129,11 +120,6 @@ actor SDLContextActor {
|
|||||||
self.config = config
|
self.config = config
|
||||||
self.rsaCipher = rsaCipher
|
self.rsaCipher = rsaCipher
|
||||||
|
|
||||||
// 事件通知器
|
|
||||||
let pair = AsyncStream<SDLContextEvent>.makeStream(bufferingPolicy: .bufferingNewest(32))
|
|
||||||
self.eventStream = pair.stream
|
|
||||||
self.eventContinuation = pair.continuation
|
|
||||||
|
|
||||||
self.puncherActor = SDLPuncherActor()
|
self.puncherActor = SDLPuncherActor()
|
||||||
self.proberActor = SDLNATProberActor(addressArray: config.stunProbeSocketAddressArray)
|
self.proberActor = SDLNATProberActor(addressArray: config.stunProbeSocketAddressArray)
|
||||||
|
|
||||||
@ -154,10 +140,6 @@ actor SDLContextActor {
|
|||||||
await self.startRuntime(resetNotifier: true)
|
await self.startRuntime(resetNotifier: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func events() -> AsyncStream<SDLContextEvent> {
|
|
||||||
return eventStream
|
|
||||||
}
|
|
||||||
|
|
||||||
private func startRuntime(resetNotifier: Bool) async {
|
private func startRuntime(resetNotifier: Bool) async {
|
||||||
if resetNotifier {
|
if resetNotifier {
|
||||||
self.prepareTunnelNotifier()
|
self.prepareTunnelNotifier()
|
||||||
@ -791,14 +773,10 @@ extension SDLContextActor {
|
|||||||
SDLLogger.log("[SDLContext] setNetworkSettings successed")
|
SDLLogger.log("[SDLContext] setNetworkSettings successed")
|
||||||
self.superRegistrationStateMachine.handleRegisterSuperAck()
|
self.superRegistrationStateMachine.handleRegisterSuperAck()
|
||||||
self.startReader()
|
self.startReader()
|
||||||
self.eventContinuation.yield(.ready)
|
|
||||||
self.eventContinuation.finish()
|
|
||||||
} catch let err {
|
} catch let err {
|
||||||
self.superRegistrationStateMachine.handleFailure()
|
self.superRegistrationStateMachine.handleFailure()
|
||||||
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)")
|
SDLLogger.log("[SDLContext] setTunnelNetworkSettings get error: \(err)")
|
||||||
self.provider.cancelTunnelWithError(err)
|
self.provider.cancelTunnelWithError(err)
|
||||||
self.eventContinuation.yield(.failed(err))
|
|
||||||
self.eventContinuation.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user