调整生命周期的管理

This commit is contained in:
anlicheng 2026-05-05 16:50:41 +08:00
parent 385edf3c6c
commit b6a23226bc

View File

@ -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
}
}