fix quicClient

This commit is contained in:
anlicheng 2026-04-28 16:39:11 +08:00
parent d900fdb379
commit b37bebd7b2
2 changed files with 79 additions and 71 deletions

View File

@ -165,67 +165,7 @@ actor SDLContextActor {
await self.startDnsLocalClient() await self.startDnsLocalClient()
await self.supervisor.addWorker(name: "quicClient") { await self.supervisor.addWorker(name: "quicClient") {
SDLLogger.log("[SDLContext] try start quicClient", for: .debug) try await self.startQUICClient()
self.quicWorker?.cancel()
await self.quicClient?.stop()
// monitor
let quicClient = SDLQUICClient(host: self.config.serverHost, port: 443)
self.quicClient = quicClient
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)")
try await withThrowingTaskGroup { group in
defer {
group.cancelAll()
}
group.addTask {
for await message in await quicClient.messageStream {
await self.handleQUICMessage(message: message)
}
}
group.addTask {
let exit = await quicClient.run()
switch exit {
case .normal:
return
case .cancelled:
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
}
}
}
while let _ = try await group.next() {
()
}
}
} }
await self.supervisor.addWorker(name: "udpHole") { await self.supervisor.addWorker(name: "udpHole") {
@ -316,6 +256,78 @@ actor SDLContextActor {
try await self.setNetworkSettings(config: config, dnsServer: DNSHelper.dnsServer) try await self.setNetworkSettings(config: config, dnsServer: DNSHelper.dnsServer)
} }
private func startQUICClient() async throws {
SDLLogger.log("[SDLContext] try start quicClient", for: .debug)
self.quicWorker?.cancel()
await self.quicClient?.stop()
// monitor
let quicClient = SDLQUICClient(host: self.config.serverHost, port: 443)
self.quicClient = quicClient
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)")
try await withTaskCancellationHandler{
try await withThrowingTaskGroup { group in
defer {
group.cancelAll()
}
group.addTask {
for await message in await quicClient.messageStream {
if Task.isCancelled {
return
}
await self.handleQUICMessage(message: message)
}
throw SDLQUICClientExit.transportClosed("messageStream finished")
}
group.addTask {
let exit = await quicClient.run()
switch exit {
case .normal:
return
case .cancelled:
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
}
}
}
let _ = try await group.next()
}
} onCancel: {
Task {
await quicClient.stop()
}
}
}
private func handleQUICMessage(message: SDLQUICInboundMessage) async { private func handleQUICMessage(message: SDLQUICInboundMessage) async {
switch message { switch message {
case .welcome(let welcome): case .welcome(let welcome):

View File

@ -79,7 +79,7 @@ actor SDLQUICClient {
(self.messageStream, self.messageCont) = AsyncStream.makeStream(of: SDLQUICInboundMessage.self) (self.messageStream, self.messageCont) = AsyncStream.makeStream(of: SDLQUICInboundMessage.self)
(self.eventStream, self.eventCont) = AsyncStream.makeStream(of: SDLQUICEvent.self) (self.eventStream, self.eventCont) = AsyncStream.makeStream(of: SDLQUICEvent.self)
// TODO //
sec_protocol_options_set_verify_block( sec_protocol_options_set_verify_block(
options.securityProtocolOptions, options.securityProtocolOptions,
{ metadata, trust, complete in { metadata, trust, complete in
@ -205,10 +205,6 @@ actor SDLQUICClient {
self.eventCont.finish() self.eventCont.finish()
} }
deinit {
self.connection.cancel()
self.finishStreams()
}
} }
// --MARK: Ready // --MARK: Ready
@ -233,7 +229,7 @@ extension SDLQUICClient {
if Task.isCancelled { if Task.isCancelled {
return return
} }
await self.cancelWaiter(id: id, throwing: SDLQUICError.timeout) self.cancelWaiter(id: id, throwing: SDLQUICError.timeout)
} }
defer { defer {