调整生命周期的管理
This commit is contained in:
parent
385edf3c6c
commit
b6a23226bc
@ -161,53 +161,48 @@ actor SDLContextActor {
|
||||
}
|
||||
|
||||
private func startSuperClient() async throws {
|
||||
SDLLogger.log("[SDLContext] try start quicClient", for: .debug)
|
||||
|
||||
// 启动monitor
|
||||
let quicClient = SDLQUICClient(host: self.config.serverHost, port: 1443)
|
||||
self.quicClient = quicClient
|
||||
await quicClient.start()
|
||||
|
||||
defer {
|
||||
Task {
|
||||
await self.quicClient?.stop()
|
||||
self.quicClient = nil
|
||||
SDLLogger.log("[SDLContext] quicClient: stop")
|
||||
}
|
||||
}
|
||||
|
||||
// 这里必须等待quic的协商完成
|
||||
try await Task.sleep(for: .seconds(0.5))
|
||||
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)")
|
||||
|
||||
try await withTaskCancellationHandler {
|
||||
try await withThrowingTaskGroup { group in
|
||||
defer {
|
||||
group.cancelAll()
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
for try await message in await quicClient.messageStream {
|
||||
try Task.checkCancellation()
|
||||
await self.handleQUICMessage(message: message)
|
||||
do {
|
||||
try await withTaskCancellationHandler {
|
||||
try await withThrowingTaskGroup { group in
|
||||
defer {
|
||||
group.cancelAll()
|
||||
}
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
while true {
|
||||
try await Task.sleep(for: .seconds(5))
|
||||
try Task.checkCancellation()
|
||||
await quicClient.send(type: .ping, data: Data())
|
||||
group.addTask {
|
||||
for try await message in await quicClient.messageStream {
|
||||
try Task.checkCancellation()
|
||||
await self.handleQUICMessage(message: message)
|
||||
}
|
||||
}
|
||||
SDLLogger.log("[SDLQUICClient] udp pingTask cancel", for: .debug)
|
||||
}
|
||||
|
||||
try await group.next()
|
||||
}
|
||||
} onCancel: {
|
||||
Task {
|
||||
await quicClient.stop()
|
||||
group.addTask {
|
||||
while true {
|
||||
try await Task.sleep(for: .seconds(5))
|
||||
try Task.checkCancellation()
|
||||
await quicClient.send(type: .ping, data: Data())
|
||||
}
|
||||
}
|
||||
|
||||
try await group.next()
|
||||
}
|
||||
} onCancel: {
|
||||
SDLLogger.log("[SDLQUICClient] quicClient taskGroup cancel", for: .debug)
|
||||
Task {
|
||||
await quicClient.stop()
|
||||
}
|
||||
}
|
||||
} catch let err {
|
||||
await quicClient.stop()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@ -289,21 +284,20 @@ actor SDLContextActor {
|
||||
let dnsClient = DNSCloudClient(host: self.config.serverHost, port: 15353)
|
||||
self.dnsClient = dnsClient
|
||||
dnsClient.start()
|
||||
do {
|
||||
try await withTaskCancellationHandler {
|
||||
for try await packet in dnsClient.packetFlow {
|
||||
try Task.checkCancellation()
|
||||
|
||||
defer {
|
||||
self.dnsClient = nil
|
||||
dnsClient.stop()
|
||||
}
|
||||
|
||||
try await withTaskCancellationHandler {
|
||||
for try await packet in dnsClient.packetFlow {
|
||||
try Task.checkCancellation()
|
||||
|
||||
let nePacket = NEPacket(data: packet, protocolFamily: 2)
|
||||
self.provider.packetFlow.writePacketObjects([nePacket])
|
||||
let nePacket = NEPacket(data: packet, protocolFamily: 2)
|
||||
self.provider.packetFlow.writePacketObjects([nePacket])
|
||||
}
|
||||
} onCancel: {
|
||||
dnsClient.stop()
|
||||
}
|
||||
} onCancel: {
|
||||
} catch let err {
|
||||
dnsClient.stop()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,25 +319,23 @@ actor SDLContextActor {
|
||||
SDLLogger.log("[SDLContext] dnsLocalClient started")
|
||||
self.dnsLocalClient = dnsLocalClient
|
||||
|
||||
defer {
|
||||
Task {
|
||||
self.dnsLocalClient = nil
|
||||
await dnsLocalClient.stop()
|
||||
}
|
||||
}
|
||||
|
||||
try await withTaskCancellationHandler {
|
||||
// 处理事件流
|
||||
for try await packet in dnsLocalClient.packetFlow {
|
||||
try Task.checkCancellation()
|
||||
// 要想办法构造一个完整的Ip包
|
||||
let nePacket = NEPacket(data: packet, protocolFamily: 2)
|
||||
self.provider.packetFlow.writePacketObjects([nePacket])
|
||||
}
|
||||
} onCancel: {
|
||||
Task {
|
||||
await dnsLocalClient.stop()
|
||||
do {
|
||||
try await withTaskCancellationHandler {
|
||||
// 处理事件流
|
||||
for try await packet in dnsLocalClient.packetFlow {
|
||||
try Task.checkCancellation()
|
||||
// 要想办法构造一个完整的Ip包
|
||||
let nePacket = NEPacket(data: packet, protocolFamily: 2)
|
||||
self.provider.packetFlow.writePacketObjects([nePacket])
|
||||
}
|
||||
} onCancel: {
|
||||
Task {
|
||||
await dnsLocalClient.stop()
|
||||
}
|
||||
}
|
||||
} catch let err {
|
||||
await dnsLocalClient.stop()
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user