This commit is contained in:
anlicheng 2025-08-01 15:49:44 +08:00
parent a280a22d3a
commit 12b1d68635
2 changed files with 6 additions and 23 deletions

View File

@ -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()
}
}

View File

@ -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()
}
}