fix task group
This commit is contained in:
parent
2d6adba8a8
commit
6cb8641869
@ -63,13 +63,14 @@ actor SDLSuperClient {
|
||||
group.addTask {
|
||||
try await self.asyncChannel.channel.closeFuture.get()
|
||||
NSLog("[SDLSuperClient] socket closed")
|
||||
self.writeContinuation.finish()
|
||||
self.inboundContinuation.finish()
|
||||
|
||||
throw SDLError.socketClosed
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
defer {
|
||||
self.inboundContinuation.finish()
|
||||
}
|
||||
|
||||
for try await var packet in inbound {
|
||||
if let message = SDLSuperClientDecoder.decode(buffer: &packet) {
|
||||
SDLLogger.log("[SDLSuperTransport] read message: \(message)", level: .warning)
|
||||
@ -87,6 +88,10 @@ 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)
|
||||
@ -100,22 +105,21 @@ actor SDLSuperClient {
|
||||
// --MARK: 心跳机制
|
||||
group.addTask {
|
||||
while true {
|
||||
|
||||
do {
|
||||
|
||||
try Task.checkCancellation()
|
||||
//await self.ping()
|
||||
try Task.checkCancellation()
|
||||
await self.ping()
|
||||
try await Task.sleep(nanoseconds: 5 * 1_000_000_000)
|
||||
try Task.checkCancellation()
|
||||
} catch let err {
|
||||
NSLog("[SDLSuperClient] heartbeat cancelled")
|
||||
throw err
|
||||
NSLog("[SDLSuperClient] heartbeat cancelled with error: \(err)")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try await group.waitForAll()
|
||||
// 迭代等待所有任务的退出, 第一个异常会被抛出
|
||||
for try await _ in group {
|
||||
|
||||
}
|
||||
|
||||
NSLog("[SDLSuperClient] group closed")
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,7 +100,11 @@ actor SDLUDPHole {
|
||||
}
|
||||
}
|
||||
|
||||
try await group.waitForAll()
|
||||
for try await _ in group {
|
||||
|
||||
}
|
||||
|
||||
SDLLogger.log("[SDLUDPHole] group closed", level: .debug)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user