From b6a23226bc34e4dc72ae764ed67e28f700d3762f Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Tue, 5 May 2026 16:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=94=9F=E5=91=BD=E5=91=A8?= =?UTF-8?q?=E6=9C=9F=E7=9A=84=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tun/Punchnet/Actors/SDLContextActor.swift | 122 ++++++++++------------ 1 file changed, 57 insertions(+), 65 deletions(-) diff --git a/Tun/Punchnet/Actors/SDLContextActor.swift b/Tun/Punchnet/Actors/SDLContextActor.swift index 76b4dac..4708525 100644 --- a/Tun/Punchnet/Actors/SDLContextActor.swift +++ b/Tun/Punchnet/Actors/SDLContextActor.swift @@ -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) + + 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() } - - try await group.next() - } - } onCancel: { - 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() - - 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]) + do { + 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]) + } + } 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 } }