diff --git a/Tun/Super/SDLSuperClient.swift b/Tun/Super/SDLSuperClient.swift index 1dcbb7c..f4cbe1e 100644 --- a/Tun/Super/SDLSuperClient.swift +++ b/Tun/Super/SDLSuperClient.swift @@ -22,7 +22,6 @@ actor SDLSuperClient { // 数据流 public let messageStream: AsyncThrowingStream private let messageCont: AsyncThrowingStream.Continuation - private var isMessageContinuationFinished: Bool = false private var pendingConnectionError: Error? @@ -107,28 +106,15 @@ actor SDLSuperClient { self.state = .running case .failed(let error): self.pendingConnectionError = SDLSuperError.connectionFailed(error) - self.finishMessageContinuationIfNeed(throwing: .connectionFailed(error)) + self.messageCont.finish(throwing: SDLSuperError.connectionFailed(error)) case .cancelled: self.pendingConnectionError = SDLSuperError.connectionCancelled - self.finishMessageContinuationIfNeed(throwing: .connectionCancelled) + self.messageCont.finish(throwing: SDLSuperError.connectionCancelled) default: () } } - private func finishMessageContinuationIfNeed(throwing error: SDLSuperError?) { - guard !self.isMessageContinuationFinished else { - return - } - - self.isMessageContinuationFinished = true - if let error { - self.messageCont.finish(throwing: error) - } else { - self.messageCont.finish() - } - } - private func waitUntilReady() async throws { while true { try Task.checkCancellation() @@ -164,11 +150,12 @@ actor SDLSuperClient { } catch is CancellationError { throw CancellationError() } catch let error as SDLSuperError { - self.finishMessageContinuationIfNeed(throwing: error) + self.messageCont.finish(throwing: error) throw error } catch { let wrappedError = SDLSuperError.internalError(error) - self.finishMessageContinuationIfNeed(throwing: wrappedError) + self.messageCont.finish(throwing: wrappedError) + throw wrappedError } } @@ -187,7 +174,7 @@ actor SDLSuperClient { if let error { Task { SDLLogger.log("[SDLSuperClient] send data get error: \(error)", for: .debug) - await self?.finishMessageContinuationIfNeed(throwing: .writeFailed(error)) + self?.messageCont.finish(throwing: SDLSuperError.writeFailed(error)) } } }) @@ -231,8 +218,7 @@ actor SDLSuperClient { let connection = self.connection connection.stateUpdateHandler = nil connection.cancel() - - self.finishMessageContinuationIfNeed(throwing: nil) + self.messageCont.finish() SDLLogger.log("[SDLSuperClient] stopped") }