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