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()
try await quicClient.waitReady(timeout: .seconds(3))
// quic
try await Task.sleep(for: .seconds(0.3))
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)")
do {
try await quicClient.waitReady(timeout: .seconds(3))
// quic
try await Task.sleep(for: .seconds(0.3))
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)")
try await withTaskCancellationHandler{
try await withThrowingTaskGroup { group in
defer {
group.cancelAll()
}
try await withTaskCancellationHandler {
try await withThrowingTaskGroup { group in
defer {
group.cancelAll()
}
group.addTask {
for await message in await quicClient.messageStream {
group.addTask {
for await message in await quicClient.messageStream {
if Task.isCancelled {
return
}
await self.handleQUICMessage(message: message)
}
if Task.isCancelled {
return
}
await self.handleQUICMessage(message: message)
throw SDLQUICClientExit.transportClosed("messageStream finished")
}
throw SDLQUICClientExit.transportClosed("messageStream finished")
}
group.addTask {
let exit = await quicClient.run()
group.addTask {
let exit = await quicClient.run()
switch exit {
case .normal:
return
case .cancelled:
if Task.isCancelled {
switch exit {
case .normal:
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:
throw SDLQUICClientExit.cancelled
case .writeFailed(let error):
throw error
if Task.isCancelled {
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:
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 {
await withTaskCancellationHandler {
await withTaskGroup(of: SDLQUICClientExit.self) { group in
group.addTask {
await self.readLoop()
}
group.addTask {
await self.heartbeatLoop()
}
let exit = await group.next() ?? .normal
group.cancelAll()
await self.stop()
self.finishStreams()
return exit
await withTaskGroup(of: SDLQUICClientExit.self) { group in
group.addTask {
await self.readLoop()
}
} onCancel: {
Task {
await self.stop()
group.addTask {
await self.heartbeatLoop()
}
let exit = await group.next() ?? .normal
group.cancelAll()
self.finishStreams()
return exit
}
}