From 4d9f6b9a429acac3d2d3302d5464bff495412642 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 22 May 2026 12:27:08 +0800 Subject: [PATCH] fix superClient --- Tun/Outbound/PacketOutboundActor.swift | 14 +++++------- Tun/Super/SDLSuperClient.swift | 30 ++++++++++++++++---------- Tun/Super/SDLSuperService.swift | 1 + 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Tun/Outbound/PacketOutboundActor.swift b/Tun/Outbound/PacketOutboundActor.swift index c50ecff..3958fdb 100644 --- a/Tun/Outbound/PacketOutboundActor.swift +++ b/Tun/Outbound/PacketOutboundActor.swift @@ -109,7 +109,7 @@ actor PacketOutboundActor { } } - func stop() async { + func stop() { self.packetReaderGeneration &+= 1 let packetReaderTask = self.packetReaderTask @@ -120,15 +120,11 @@ actor PacketOutboundActor { func handleTunPacket(_ packet: IPPacketView) async { let router = PacketOutboundRouter(networkAddress: self.networkAddress, exitNode: self.exitNode) let decision = router.route(packet: packet) - + if decision.shouldTrackFlow { self.policyService.recordOutboundFlow(ipPacket: packet) } - - await self.handleTunRouteDecision(decision) - } - - private func handleTunRouteDecision(_ decision: PacketOutboundRouter.RouteDecision) async { + switch decision { case .loopback(let ipPacketData): let nePacket = NEPacket(data: ipPacketData, protocolFamily: 2) @@ -174,12 +170,12 @@ actor PacketOutboundActor { case .superNode(let payload): await self.sendSuperPacket(type: .data, data: payload) case .peer(let payload, let session): - SDLLogger.log("[PacketOutboundActor] step 5 send packet by session: \(session)", for: .trace) + SDLLogger.log("[PacketOutboundActor] send packet by session: \(session)", for: .trace) await self.sendPeerPacket(type: .data, data: payload, remoteAddress: session.natAddress) self.flowTracer.inc(num: payload.count, type: .p2p) case .superNodeAndPunch(let payload, let request): await self.sendSuperPacket(type: .data, data: payload) - SDLLogger.log("[PacketOutboundActor] step 5 send packet by super: \(self.stunSocketAddress)", for: .trace) + SDLLogger.log("[PacketOutboundActor] send packet by super: \(self.stunSocketAddress)", for: .trace) self.flowTracer.inc(num: payload.count, type: .forward) if let queryData = await self.puncherActor.makeQueryInfoRequest(request: request) { diff --git a/Tun/Super/SDLSuperClient.swift b/Tun/Super/SDLSuperClient.swift index aab0530..a768047 100644 --- a/Tun/Super/SDLSuperClient.swift +++ b/Tun/Super/SDLSuperClient.swift @@ -145,6 +145,7 @@ actor SDLSuperClient { let data = try await self.readOnce() let frames = try self.frameParser.parseFrames(data: data) for frame in frames { + try Task.checkCancellation() if let message = SDLQUICCodec.decode(frame: frame) { self.messageCont.yield(message) } else { @@ -183,19 +184,26 @@ actor SDLSuperClient { throw SDLQUICError.connectionCancelled } - return try await withCheckedThrowingContinuation { cont in - connection.receive(minimumIncompleteLength: 1, maximumLength: 64 * 1024) { data, _, isComplete, error in - if let error { - cont.resume(throwing: error) - return - } - - if isComplete { - cont.resume(throwing: SDLQUICError.dataStreamClosed) - } else { - cont.resume(returning: data ?? Data()) + let readContinuation = OnceContinuation() + + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { cont in + readContinuation.set(cont) + connection.receive(minimumIncompleteLength: 1, maximumLength: 64 * 1024) { data, _, isComplete, error in + if let error { + readContinuation.resume(throwing: error) + return + } + + if isComplete { + readContinuation.resume(throwing: SDLQUICError.dataStreamClosed) + } else { + readContinuation.resume(returning: data ?? Data()) + } } } + } onCancel: { + readContinuation.resume(throwing: CancellationError()) } } diff --git a/Tun/Super/SDLSuperService.swift b/Tun/Super/SDLSuperService.swift index b255757..531d64c 100644 --- a/Tun/Super/SDLSuperService.swift +++ b/Tun/Super/SDLSuperService.swift @@ -61,6 +61,7 @@ actor SDLSuperService { await superClient.stop() } } + await self.cleanup(superClient) } catch { await self.cleanup(superClient)