From 0e455b187dfd663016fa232a8b74c93fa8f807b2 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 27 May 2026 21:45:53 +0800 Subject: [PATCH] fix --- Tun/Super/SDLSuperService.swift | 1 + Tun/Super/SDLSuperSession.swift | 8 +-- Tun/UDPHole/SDLUDPHoleService.swift | 1 + Tun/UDPHole/SDLUDPHoleSession.swift | 69 +++++++++---------------- Tun/UDPHoleV6/SDLUDPHoleV6Service.swift | 1 + Tun/UDPHoleV6/SDLUDPHoleV6Session.swift | 69 +++++++++++-------------- 6 files changed, 60 insertions(+), 89 deletions(-) diff --git a/Tun/Super/SDLSuperService.swift b/Tun/Super/SDLSuperService.swift index 98ab823..acf6772 100644 --- a/Tun/Super/SDLSuperService.swift +++ b/Tun/Super/SDLSuperService.swift @@ -34,6 +34,7 @@ actor SDLSuperService { do { try await session.run() self.clearCurrent(session, generation: generation) + await session.stop() } catch is CancellationError { self.clearCurrent(session, generation: generation) await session.stop() diff --git a/Tun/Super/SDLSuperSession.swift b/Tun/Super/SDLSuperSession.swift index 87b4b15..7fb633c 100644 --- a/Tun/Super/SDLSuperSession.swift +++ b/Tun/Super/SDLSuperSession.swift @@ -22,13 +22,7 @@ final class SDLSuperSession: @unchecked Sendable { } func run() async throws { - do { - try await self.runLoops() - await self.stop() - } catch { - await self.stop() - throw error - } + try await self.runLoops() } func stop() async { diff --git a/Tun/UDPHole/SDLUDPHoleService.swift b/Tun/UDPHole/SDLUDPHoleService.swift index 1941124..0285c16 100644 --- a/Tun/UDPHole/SDLUDPHoleService.swift +++ b/Tun/UDPHole/SDLUDPHoleService.swift @@ -43,6 +43,7 @@ actor SDLUDPHoleService { do { try await session.run() self.clearCurrent(session, generation: generation) + await session.stop() } catch is CancellationError { self.clearCurrent(session, generation: generation) await session.stop() diff --git a/Tun/UDPHole/SDLUDPHoleSession.swift b/Tun/UDPHole/SDLUDPHoleSession.swift index 9938f3f..3bc3dce 100644 --- a/Tun/UDPHole/SDLUDPHoleSession.swift +++ b/Tun/UDPHole/SDLUDPHoleSession.swift @@ -26,12 +26,28 @@ actor SDLUDPHoleSession { } func run() async throws { - do { - try await self.runV4() - await self.stop() - } catch { - await self.stop() - throw error + let udpHole = try SDLUDPHole() + let localAddress = try udpHole.start() + self.udpHole = udpHole + self.localAddress = localAddress + + SDLLogger.log("[SDLUDPHoleSession] udpHole started, on address: \(localAddress)") + await self.onEvent(.ready(localAddress)) + + try await withThrowingTaskGroup(of: Void.self) { group in + defer { + group.cancelAll() + } + + group.addTask { + try await self.readLoop(udpHole: udpHole) + } + + group.addTask { + await self.probeNatType(udpHole: udpHole) + } + + try await group.waitForAll() } } @@ -58,45 +74,10 @@ actor SDLUDPHoleSession { udpHole.send(type: type, data: data, remoteAddress: remoteAddress) } - private func runV4() async throws { - let udpHole = try SDLUDPHole() - let localAddress = try udpHole.start() - self.udpHole = udpHole - self.localAddress = localAddress - - SDLLogger.log("[SDLUDPHoleSession] udpHole started, on address: \(localAddress)") - await self.onEvent(.ready(localAddress)) - - do { - try await withThrowingTaskGroup(of: Void.self) { group in - defer { - group.cancelAll() - } - - group.addTask { - try await self.readV4Loop(udpHole: udpHole) - } - - group.addTask { - await self.probeNatType(udpHole: udpHole) - } - - try await group.waitForAll() - } - } catch { - udpHole.stop() - if self.udpHole === udpHole { - self.udpHole = nil - self.localAddress = nil - } - throw error - } - } - - private func readV4Loop(udpHole: SDLUDPHole) async throws { + private func readLoop(udpHole: SDLUDPHole) async throws { for try await datagram in udpHole.messageStream { try Task.checkCancellation() - try await self.handleV4Message(remoteAddress: datagram.remoteAddress, message: datagram.message) + try await self.handleMessage(remoteAddress: datagram.remoteAddress, message: datagram.message) } } @@ -113,7 +94,7 @@ actor SDLUDPHoleSession { await self.onEvent(.natType(natType)) } - private func handleV4Message(remoteAddress: SocketAddress, message: SDLHoleMessage) async throws { + private func handleMessage(remoteAddress: SocketAddress, message: SDLHoleMessage) async throws { switch message { case .control(let control): switch control { diff --git a/Tun/UDPHoleV6/SDLUDPHoleV6Service.swift b/Tun/UDPHoleV6/SDLUDPHoleV6Service.swift index 26187b4..9b490a9 100644 --- a/Tun/UDPHoleV6/SDLUDPHoleV6Service.swift +++ b/Tun/UDPHoleV6/SDLUDPHoleV6Service.swift @@ -29,6 +29,7 @@ actor SDLUDPHoleV6Service { do { try await session.run() self.clearCurrent(session, generation: generation) + await session.stop() } catch is CancellationError { self.clearCurrent(session, generation: generation) await session.stop() diff --git a/Tun/UDPHoleV6/SDLUDPHoleV6Session.swift b/Tun/UDPHoleV6/SDLUDPHoleV6Session.swift index a51949d..5b9987a 100644 --- a/Tun/UDPHoleV6/SDLUDPHoleV6Session.swift +++ b/Tun/UDPHoleV6/SDLUDPHoleV6Session.swift @@ -26,46 +26,39 @@ actor SDLUDPHoleV6Session { SDLLogger.log("[SDLUDPHoleV6Session] udpHoleV6 started, no local address") } - do { - try await withThrowingTaskGroup(of: Void.self) { group in - defer { - group.cancelAll() - } - - let onEvent = self.onEvent - let onData = self.onData - - group.addTask { - for await (remoteAddress, message) in udpHoleV6.messageStream { - try Task.checkCancellation() - switch message { - case .control(let control): - await onEvent(.packet(remoteAddress, control)) - case .data(let data): - await onData(data) - } - } - } - - group.addTask { - for await event in udpHoleV6.eventStream { - try Task.checkCancellation() - switch event { - case .ready: - SDLLogger.log("[SDLUDPHoleV6Session] udpHoleV6 ready") - case .closed, .errorCaught: - throw SDLContextError.udpHoleClosed - } - } - } - - _ = try await group.next() + try await withThrowingTaskGroup(of: Void.self) { group in + defer { + group.cancelAll() } - await self.stop() - } catch { - await self.stop() - throw error + let onEvent = self.onEvent + let onData = self.onData + + group.addTask { + for await (remoteAddress, message) in udpHoleV6.messageStream { + try Task.checkCancellation() + switch message { + case .control(let control): + await onEvent(.packet(remoteAddress, control)) + case .data(let data): + await onData(data) + } + } + } + + group.addTask { + for await event in udpHoleV6.eventStream { + try Task.checkCancellation() + switch event { + case .ready: + SDLLogger.log("[SDLUDPHoleV6Session] udpHoleV6 ready") + case .closed, .errorCaught: + throw SDLContextError.udpHoleClosed + } + } + } + + _ = try await group.next() } }