From 26ee512a9aa6bb620e9567dd6436f398a9fd64a2 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 1 Aug 2025 15:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4task?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Punchnet/SDLContext.swift | 132 +++++++++++++++----------- Sources/Punchnet/SDLSuperClient.swift | 2 - Sources/Punchnet/SDLUDPHole.swift | 2 - 3 files changed, 79 insertions(+), 57 deletions(-) diff --git a/Sources/Punchnet/SDLContext.swift b/Sources/Punchnet/SDLContext.swift index 9ee5d23..72d0c83 100644 --- a/Sources/Punchnet/SDLContext.swift +++ b/Sources/Punchnet/SDLContext.swift @@ -93,28 +93,68 @@ public class SDLContext: @unchecked Sendable { } public func start() async throws { - self.udpHole = try await SDLUDPHole() - self.superClient = try await SDLSuperClient(host: self.config.superHost, port: self.config.superPort) self.noticeClient = try await SDLNoticeClient() - + + try await withThrowingTaskGroup(of: Void.self) { group in + group.addTask { + while !Task.isCancelled { + do { + try await self.startUDPHole() + } catch let err { + NSLog("udp Hole get err: \(err)") + } + } + } + + group.addTask { + while !Task.isCancelled { + do { + try await self.startSuperClient() + } catch let err { + NSLog("SuperClient get error: \(err)") + await self.arpServer.clear() + try? await Task.sleep(for: .seconds(2)) + } + } + } + + group.addTask { + try await self.startMonitor() + } + + group.addTask { + while !Task.isCancelled { + do { + try await self.noticeClient?.start() + } catch let err { + NSLog("noticeClient get err: \(err)") + } + } + } + + try await group.waitForAll() + } + } + + public func stop() async { + self.superClient = nil + self.udpHole = nil + + self.readTask?.cancel() + } + + private func startUDPHole() async throws { + self.udpHole = try await SDLUDPHole() + try await withThrowingTaskGroup(of: Void.self) { group in group.addTask { try await self.udpHole?.start() } group.addTask { - try await self.superClient?.start() - } - - group.addTask { - try await self.noticeClient?.start() - } - - group.addTask { - if let eventFlow = self.superClient?.eventFlow { - for try await event in eventFlow { - try await self.handleSuperEvent(event: event) - } + while !Task.isCancelled { + try await Task.sleep(nanoseconds: 5 * 1_000_000_000) + self.lastCookie = await self.udpHole?.stunRequest(context: self) } } @@ -125,13 +165,34 @@ public class SDLContext: @unchecked Sendable { } } } + try await group.waitForAll() + } + } + + private func startSuperClient() async throws { + self.superClient = try await SDLSuperClient(host: self.config.superHost, port: self.config.superPort) + + try await withThrowingTaskGroup(of: Void.self) { group in + group.addTask { + try await self.superClient?.start() + } group.addTask { - while !Task.isCancelled { - try await Task.sleep(nanoseconds: 5 * 1_000_000_000) - self.lastCookie = await self.udpHole?.stunRequest(context: self) + if let eventFlow = self.superClient?.eventFlow { + for try await event in eventFlow { + try await self.handleSuperEvent(event: event) + } } } + try await group.waitForAll() + } + } + + private func startMonitor() async throws { + try await withThrowingTaskGroup(of: Void.self) { group in + group.addTask { + try await self.noticeClient?.start() + } group.addTask { // 启动网络监控 @@ -154,13 +215,6 @@ public class SDLContext: @unchecked Sendable { } } - public func stop() async { - self.superClient = nil - self.udpHole = nil - - self.readTask?.cancel() - } - private func handleSuperEvent(event: SDLSuperClient.SuperEvent) async throws { switch event { case .ready: @@ -213,14 +267,6 @@ public class SDLContext: @unchecked Sendable { () } - case .closed: - NSLog("[SDLContext] super client closed") - await self.arpServer.clear() - DispatchQueue.main.asyncAfter(deadline: .now() + 5) { -// Task {@MainActor in -// try await self.startSuperClient() -// } - } case .event(let evt): switch evt { case .natChanged(let natChangedEvent): @@ -261,19 +307,6 @@ public class SDLContext: @unchecked Sendable { } - private func startUDPHole() async throws { -// self.udpHole = SDLUDPHole() -// -// self.udpCancel?.cancel() -// self.udpCancel = self.udpHole?.eventFlow.sink { event in -// Task.detached { -// await self.handleUDPEvent(event: event) -// } -// } -// -// try await self.udpHole?.start() - } - private func handleUDPEvent(event: SDLUDPHole.UDPEvent) async throws { switch event { case .ready: @@ -281,13 +314,6 @@ public class SDLContext: @unchecked Sendable { //self.natType = await SDLNatProber.getNatType(udpHole: self.udpHole, config: self.config) SDLLogger.log("[SDLContext] nat type is: \(self.natType)", level: .debug) - case .closed: - DispatchQueue.main.asyncAfter(deadline: .now() + 5) { - Task { - try await self.startUDPHole() - } - } - case .message(let remoteAddress, let message): switch message { case .register(let register): diff --git a/Sources/Punchnet/SDLSuperClient.swift b/Sources/Punchnet/SDLSuperClient.swift index 7493e74..d779fc8 100644 --- a/Sources/Punchnet/SDLSuperClient.swift +++ b/Sources/Punchnet/SDLSuperClient.swift @@ -31,7 +31,6 @@ actor SDLSuperClient { // 定义事件类型 enum SuperEvent { case ready - case closed case event(SDLEvent) case command(UInt32, SDLCommand) } @@ -62,7 +61,6 @@ actor SDLSuperClient { try await withThrowingTaskGroup(of: Void.self) { group in group.addTask { try await self.asyncChannel.channel.closeFuture.get() - self.inboundContinuation.yield(.closed) self.inboundContinuation.finish() } diff --git a/Sources/Punchnet/SDLUDPHole.swift b/Sources/Punchnet/SDLUDPHole.swift index b591eef..37e5fa0 100644 --- a/Sources/Punchnet/SDLUDPHole.swift +++ b/Sources/Punchnet/SDLUDPHole.swift @@ -30,7 +30,6 @@ actor SDLUDPHole { // 定义事件类型 enum UDPEvent { case ready - case closed case message(SocketAddress, SDLHoleInboundMessage) case data(SDLData) } @@ -60,7 +59,6 @@ actor SDLUDPHole { try await withThrowingTaskGroup(of: Void.self) { group in group.addTask { try await self.asyncChannel.channel.closeFuture.get() - self.eventContinuation.yield(.closed) self.eventContinuation.finish() }