fix quicClient

This commit is contained in:
anlicheng 2026-04-28 17:19:55 +08:00
parent 7b549981af
commit 2b234fb5c3
2 changed files with 82 additions and 71 deletions

View File

@ -268,63 +268,81 @@ actor SDLContextActor {
await quicClient.start() await quicClient.start()
try await quicClient.waitReady(timeout: .seconds(3)) do {
// quic try await quicClient.waitReady(timeout: .seconds(3))
try await Task.sleep(for: .seconds(0.3)) // quic
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)") try await Task.sleep(for: .seconds(0.3))
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)")
try await withTaskCancellationHandler{ try await withTaskCancellationHandler {
try await withThrowingTaskGroup { group in try await withThrowingTaskGroup { group in
defer { defer {
group.cancelAll() group.cancelAll()
} }
group.addTask { group.addTask {
for await message in await quicClient.messageStream { for await message in await quicClient.messageStream {
if Task.isCancelled {
return
}
await self.handleQUICMessage(message: message)
}
if Task.isCancelled { if Task.isCancelled {
return return
} }
await self.handleQUICMessage(message: message) throw SDLQUICClientExit.transportClosed("messageStream finished")
} }
throw SDLQUICClientExit.transportClosed("messageStream finished")
}
group.addTask { group.addTask {
let exit = await quicClient.run() let exit = await quicClient.run()
switch exit { switch exit {
case .normal: case .normal:
return
case .cancelled:
if Task.isCancelled {
return return
}
throw exit
case .transportClosed, .readFailed, .writeFailed:
throw exit
}
}
group.addTask {
for await event in await quicClient.eventStream {
switch event {
case .failed(let error):
throw error
case .cancelled: case .cancelled:
throw SDLQUICClientExit.cancelled if Task.isCancelled {
case .writeFailed(let error): return
throw error }
throw exit
case .transportClosed, .readFailed, .writeFailed:
throw exit
} }
} }
group.addTask {
for await event in await quicClient.eventStream {
switch event {
case .failed(let error):
throw error
case .cancelled:
throw SDLQUICClientExit.cancelled
case .writeFailed(let error):
throw error
}
}
if Task.isCancelled {
return
}
throw SDLQUICClientExit.transportClosed("eventStream finished")
}
do {
let _ = try await group.next()
await quicClient.stop()
} catch {
await quicClient.stop()
throw error
}
} }
let _ = try await group.next() } onCancel: {
} Task {
await quicClient.stop()
} onCancel: { }
Task {
await quicClient.stop()
} }
} catch {
await quicClient.stop()
throw error
} }
} }

View File

@ -136,27 +136,20 @@ actor SDLQUICClient {
} }
func run() async -> SDLQUICClientExit { func run() async -> SDLQUICClientExit {
await withTaskCancellationHandler { await withTaskGroup(of: SDLQUICClientExit.self) { group in
await withTaskGroup(of: SDLQUICClientExit.self) { group in group.addTask {
group.addTask { await self.readLoop()
await self.readLoop()
}
group.addTask {
await self.heartbeatLoop()
}
let exit = await group.next() ?? .normal
group.cancelAll()
await self.stop()
self.finishStreams()
return exit
} }
} onCancel: {
Task { group.addTask {
await self.stop() await self.heartbeatLoop()
} }
let exit = await group.next() ?? .normal
group.cancelAll()
self.finishStreams()
return exit
} }
} }