From 12b1d68635bfaa72ae40d8d52d7282fb0f627e6d Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 1 Aug 2025 15:49:44 +0800 Subject: [PATCH] fix --- Sources/Punchnet/SDLSuperClient.swift | 8 +++----- Sources/Punchnet/SDLUDPHole.swift | 21 +++------------------ 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/Sources/Punchnet/SDLSuperClient.swift b/Sources/Punchnet/SDLSuperClient.swift index b06709e..c0d5f40 100644 --- a/Sources/Punchnet/SDLSuperClient.swift +++ b/Sources/Punchnet/SDLSuperClient.swift @@ -58,10 +58,11 @@ actor SDLSuperClient { func start() async throws { try await self.asyncChannel.executeThenClose { inbound, outbound in self.inboundContinuation.yield(.ready) + try await withThrowingTaskGroup(of: Void.self) { group in group.addTask { try await self.asyncChannel.channel.closeFuture.get() - self.inboundContinuation.finish() + NSLog("[SDLSuperClient] socket closed") } group.addTask { @@ -81,10 +82,6 @@ actor SDLSuperClient { } group.addTask { - defer { - self.writeContinuation.finish() - } - for try await message in self.writeStream { var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 5) buffer.writeInteger(message.packetId, as: UInt32.self) @@ -182,6 +179,7 @@ actor SDLSuperClient { deinit { try! group.syncShutdownGracefully() + self.inboundContinuation.finish() } } diff --git a/Sources/Punchnet/SDLUDPHole.swift b/Sources/Punchnet/SDLUDPHole.swift index 4c8c143..72eaa5a 100644 --- a/Sources/Punchnet/SDLUDPHole.swift +++ b/Sources/Punchnet/SDLUDPHole.swift @@ -21,9 +21,6 @@ actor SDLUDPHole { public let (eventFlow, eventContinuation) = AsyncStream.makeStream(of: UDPEvent.self, bufferingPolicy: .unbounded) - // 标记当前通道是否关闭 - private var isClosed: Bool = true - struct UDPMessage { let remoteAddress: SocketAddress let type: SDLPacketType @@ -58,13 +55,11 @@ actor SDLUDPHole { func start() async throws { try await self.asyncChannel.executeThenClose { inbound, outbound in self.eventContinuation.yield(.ready) - self.closeChannel(closed: false) try await withThrowingTaskGroup(of: Void.self) { group in group.addTask { try await self.asyncChannel.channel.closeFuture.get() - await self.closeChannel(closed: true) - self.eventContinuation.finish() + NSLog("[UDPHole] channel closed") } group.addTask { @@ -93,10 +88,6 @@ actor SDLUDPHole { } group.addTask { - defer { - self.writeContinuation.finish() - } - for try await message in self.writeStream { var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 1) buffer.writeBytes([message.type.rawValue]) @@ -163,10 +154,6 @@ actor SDLUDPHole { } } - private func closeChannel(closed: Bool) { - self.isClosed = closed - } - // MARK: client-client apis // 发送数据包到其他session @@ -230,10 +217,6 @@ actor SDLUDPHole { // 处理写入逻辑 private func send(remoteAddress: SocketAddress, type: SDLPacketType, data: Data) { - guard !self.isClosed else { - return - } - let message = UDPMessage(remoteAddress: remoteAddress, type: type, data: data) self.writeContinuation.yield(message) } @@ -270,5 +253,7 @@ actor SDLUDPHole { deinit { try? self.group.syncShutdownGracefully() + self.writeContinuation.finish() + self.eventContinuation.finish() } }