fix superClient
This commit is contained in:
parent
1c0ed04fa2
commit
e7f58dce03
53
Tun/Concurrency/AsyncPromise.swift
Normal file
53
Tun/Concurrency/AsyncPromise.swift
Normal file
@ -0,0 +1,53 @@
|
||||
//
|
||||
// AsyncPromise.swift
|
||||
// punchnet
|
||||
//
|
||||
// Created by 安礼成 on 2026/5/27.
|
||||
//
|
||||
import Foundation
|
||||
|
||||
public actor AsyncPromise<Value: Sendable> {
|
||||
private enum State {
|
||||
case pending([CheckedContinuation<Value, Error>])
|
||||
case completed(Result<Value, Error>)
|
||||
}
|
||||
|
||||
private var state: State = .pending([])
|
||||
|
||||
public init() {
|
||||
|
||||
}
|
||||
|
||||
public func value() async throws -> Value {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
switch state {
|
||||
case .pending(var continuations):
|
||||
continuations.append(continuation)
|
||||
state = .pending(continuations)
|
||||
|
||||
case .completed(let result):
|
||||
continuation.resume(with: result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func succeed(_ value: Value) {
|
||||
complete(.success(value))
|
||||
}
|
||||
|
||||
public func fail(_ error: Error) {
|
||||
complete(.failure(error))
|
||||
}
|
||||
|
||||
private func complete(_ result: Result<Value, Error>) {
|
||||
guard case .pending(let continuations) = state else {
|
||||
return
|
||||
}
|
||||
|
||||
state = .completed(result)
|
||||
|
||||
for continuation in continuations {
|
||||
continuation.resume(with: result)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,6 +13,7 @@ final class SDLSuperClient: @unchecked Sendable {
|
||||
// 数据流
|
||||
public let messageStream: AsyncThrowingStream<SDLQUICInboundMessage, Error>
|
||||
private let messageCont: AsyncThrowingStream<SDLQUICInboundMessage, Error>.Continuation
|
||||
|
||||
private let stateLock = NSLock()
|
||||
private var isStarted = false
|
||||
private var isStopped = false
|
||||
@ -61,16 +62,42 @@ final class SDLSuperClient: @unchecked Sendable {
|
||||
return
|
||||
}
|
||||
|
||||
let stateStream = Self.makeStateStream(for: self.connection)
|
||||
defer {
|
||||
self.stop()
|
||||
}
|
||||
|
||||
try await withTaskCancellationHandler {
|
||||
let stateStream = Self.makeStateStream(for: self.connection)
|
||||
let promise = AsyncPromise<Bool>()
|
||||
self.connection.start(queue: self.queue)
|
||||
try await self.runStateLoop(stateStream)
|
||||
|
||||
try await withTaskCancellationHandler {
|
||||
try await withThrowingTaskGroup { group in
|
||||
group.addTask {
|
||||
do {
|
||||
for try await state in stateStream {
|
||||
switch state {
|
||||
case .ready:
|
||||
await promise.succeed(true)
|
||||
default:
|
||||
()
|
||||
}
|
||||
}
|
||||
} catch let err {
|
||||
await promise.fail(err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
group.addTask {
|
||||
_ = try await promise.value()
|
||||
try Task.checkCancellation()
|
||||
try await self.readLoop()
|
||||
}
|
||||
|
||||
try await group.next()
|
||||
}
|
||||
} onCancel: {
|
||||
self.connection.cancel()
|
||||
self.stop()
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,15 +117,13 @@ final class SDLSuperClient: @unchecked Sendable {
|
||||
return AsyncThrowingStream(bufferingPolicy: .bufferingNewest(16)) { continuation in
|
||||
connection.stateUpdateHandler = { state in
|
||||
SDLLogger.log("[SDLSuperClient] new state: \(state)", for: .debug)
|
||||
continuation.yield(state)
|
||||
|
||||
switch state {
|
||||
case .failed(let error):
|
||||
continuation.finish(throwing: SDLSuperError.connectionFailed(error))
|
||||
continuation.finish(throwing: error)
|
||||
case .cancelled:
|
||||
continuation.finish(throwing: SDLSuperError.connectionCancelled)
|
||||
default:
|
||||
break
|
||||
continuation.yield(state)
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,38 +133,8 @@ final class SDLSuperClient: @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
private func runStateLoop(_ stateStream: AsyncThrowingStream<NWConnection.State, Error>) async throws {
|
||||
do {
|
||||
try await self.waitUntilReady(stateStream)
|
||||
self.connection.stateUpdateHandler = nil
|
||||
try await self.readLoop()
|
||||
} catch is CancellationError {
|
||||
throw CancellationError()
|
||||
} catch let error as SDLSuperError {
|
||||
self.finishMessageStream(throwing: error)
|
||||
throw error
|
||||
} catch {
|
||||
let wrappedError = SDLSuperError.internalError(error)
|
||||
self.finishMessageStream(throwing: wrappedError)
|
||||
throw wrappedError
|
||||
}
|
||||
}
|
||||
|
||||
private func waitUntilReady(_ stateStream: AsyncThrowingStream<NWConnection.State, Error>) async throws {
|
||||
for try await state in stateStream {
|
||||
try Task.checkCancellation()
|
||||
|
||||
if case .ready = state {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
throw SDLSuperError.connectionCancelled
|
||||
}
|
||||
|
||||
private func readLoop() async throws {
|
||||
let frameParser = SDLSuperFrameParser(maxBufferSize: self.maxBufferSize)
|
||||
do {
|
||||
while true {
|
||||
try Task.checkCancellation()
|
||||
let data = try await Self.readOnce(connection: self.connection)
|
||||
@ -153,17 +148,6 @@ final class SDLSuperClient: @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch is CancellationError {
|
||||
throw CancellationError()
|
||||
} catch let error as SDLSuperError {
|
||||
self.finishMessageStream(throwing: error)
|
||||
throw error
|
||||
} catch {
|
||||
let wrappedError = SDLSuperError.internalError(error)
|
||||
self.finishMessageStream(throwing: wrappedError)
|
||||
|
||||
throw wrappedError
|
||||
}
|
||||
}
|
||||
|
||||
func send(type: SDLPacketType, data: Data) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user