fix
This commit is contained in:
parent
a280a22d3a
commit
12b1d68635
@ -58,10 +58,11 @@ actor SDLSuperClient {
|
|||||||
func start() async throws {
|
func start() async throws {
|
||||||
try await self.asyncChannel.executeThenClose { inbound, outbound in
|
try await self.asyncChannel.executeThenClose { inbound, outbound in
|
||||||
self.inboundContinuation.yield(.ready)
|
self.inboundContinuation.yield(.ready)
|
||||||
|
|
||||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
group.addTask {
|
group.addTask {
|
||||||
try await self.asyncChannel.channel.closeFuture.get()
|
try await self.asyncChannel.channel.closeFuture.get()
|
||||||
self.inboundContinuation.finish()
|
NSLog("[SDLSuperClient] socket closed")
|
||||||
}
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
@ -81,10 +82,6 @@ actor SDLSuperClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
defer {
|
|
||||||
self.writeContinuation.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
for try await message in self.writeStream {
|
for try await message in self.writeStream {
|
||||||
var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 5)
|
var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 5)
|
||||||
buffer.writeInteger(message.packetId, as: UInt32.self)
|
buffer.writeInteger(message.packetId, as: UInt32.self)
|
||||||
@ -182,6 +179,7 @@ actor SDLSuperClient {
|
|||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
try! group.syncShutdownGracefully()
|
try! group.syncShutdownGracefully()
|
||||||
|
self.inboundContinuation.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,9 +21,6 @@ actor SDLUDPHole {
|
|||||||
|
|
||||||
public let (eventFlow, eventContinuation) = AsyncStream.makeStream(of: UDPEvent.self, bufferingPolicy: .unbounded)
|
public let (eventFlow, eventContinuation) = AsyncStream.makeStream(of: UDPEvent.self, bufferingPolicy: .unbounded)
|
||||||
|
|
||||||
// 标记当前通道是否关闭
|
|
||||||
private var isClosed: Bool = true
|
|
||||||
|
|
||||||
struct UDPMessage {
|
struct UDPMessage {
|
||||||
let remoteAddress: SocketAddress
|
let remoteAddress: SocketAddress
|
||||||
let type: SDLPacketType
|
let type: SDLPacketType
|
||||||
@ -58,13 +55,11 @@ actor SDLUDPHole {
|
|||||||
func start() async throws {
|
func start() async throws {
|
||||||
try await self.asyncChannel.executeThenClose { inbound, outbound in
|
try await self.asyncChannel.executeThenClose { inbound, outbound in
|
||||||
self.eventContinuation.yield(.ready)
|
self.eventContinuation.yield(.ready)
|
||||||
self.closeChannel(closed: false)
|
|
||||||
|
|
||||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
group.addTask {
|
group.addTask {
|
||||||
try await self.asyncChannel.channel.closeFuture.get()
|
try await self.asyncChannel.channel.closeFuture.get()
|
||||||
await self.closeChannel(closed: true)
|
NSLog("[UDPHole] channel closed")
|
||||||
self.eventContinuation.finish()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
@ -93,10 +88,6 @@ actor SDLUDPHole {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group.addTask {
|
group.addTask {
|
||||||
defer {
|
|
||||||
self.writeContinuation.finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
for try await message in self.writeStream {
|
for try await message in self.writeStream {
|
||||||
var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 1)
|
var buffer = self.asyncChannel.channel.allocator.buffer(capacity: message.data.count + 1)
|
||||||
buffer.writeBytes([message.type.rawValue])
|
buffer.writeBytes([message.type.rawValue])
|
||||||
@ -163,10 +154,6 @@ actor SDLUDPHole {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func closeChannel(closed: Bool) {
|
|
||||||
self.isClosed = closed
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: client-client apis
|
// MARK: client-client apis
|
||||||
|
|
||||||
// 发送数据包到其他session
|
// 发送数据包到其他session
|
||||||
@ -230,10 +217,6 @@ actor SDLUDPHole {
|
|||||||
|
|
||||||
// 处理写入逻辑
|
// 处理写入逻辑
|
||||||
private func send(remoteAddress: SocketAddress, type: SDLPacketType, data: Data) {
|
private func send(remoteAddress: SocketAddress, type: SDLPacketType, data: Data) {
|
||||||
guard !self.isClosed else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let message = UDPMessage(remoteAddress: remoteAddress, type: type, data: data)
|
let message = UDPMessage(remoteAddress: remoteAddress, type: type, data: data)
|
||||||
self.writeContinuation.yield(message)
|
self.writeContinuation.yield(message)
|
||||||
}
|
}
|
||||||
@ -270,5 +253,7 @@ actor SDLUDPHole {
|
|||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
try? self.group.syncShutdownGracefully()
|
try? self.group.syncShutdownGracefully()
|
||||||
|
self.writeContinuation.finish()
|
||||||
|
self.eventContinuation.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user