Changes
5 changed files (+75/-4)
-
-
@@ -30,4 +30,11 @@ func connect() async throws/// Close the current connection. func disconnect() /// Waits for connection to be closed. /// /// This method returns when the connection was closed. /// If the connection is already closed at the time of this method call, /// this method immediately returns. func lifetime() async }
-
-
-
@@ -37,6 +37,8 @@private var messageContinuationID: UInt = 0 private var messageContinuations: [UInt: AsyncStream<Moo>.Continuation] = [:] private var lifetimeContinuations: [CheckedContinuation<Void, Never>] = [] private var readLoop: Task<Void, any Error>? = nil public enum InitError: Error {
-
@@ -93,6 +95,11 @@ self.readLoop = nil} self.messageContinuations = [:] for cont in self.lifetimeContinuations { cont.resume() } self.lifetimeContinuations = [] if conn.state != .cancelled { conn.cancel()
-
@@ -226,6 +233,17 @@ default:break } } catch { switch error { case NWError.posix(.ENODATA): if let self = self { logger.warning("Stream closed, disconnecting") await self.disconnect() return } default: break } if let logger = self?.logger { logger.warning("Receive failed: \(error)") }
-
@@ -245,8 +263,23 @@ }self.messageContinuations = [:] for cont in self.lifetimeContinuations { cont.resume() } self.lifetimeContinuations = [] if conn.state != .cancelled { conn.cancel() } } public func lifetime() async { guard self.conn.state != .cancelled else { return } await withCheckedContinuation { (cont: CheckedContinuation<Void, Never>) in self.lifetimeContinuations.append(cont) } }
-
-
-
@@ -143,6 +143,10 @@ connectedServerID = serverIDserverIp = host serverPort = Int(port) savedToken = await conn.token await conn.lifetime() state = .closed } catch { state = .error(error) }
-
-
-
@@ -397,6 +397,10 @@ var port: UInt16 { 9003 }func connect() async throws {} func disconnect() {} func lifetime() async { await withCheckedContinuation { (_: CheckedContinuation<Void, Never>) in } } } #Preview {
-
-
-
@@ -27,8 +27,24 @@var body: some View { VStack { switch conn.state { case .loading, .closed: // TODO: Create view for closed state case .closed: ContentUnavailableView { Label( "Connection closed", systemImage: "play.slash" ) } description: { Text("Connection to Roon server closed.") } actions: { Button { Task { await conn.connect() } } label: { Text("Connect") } } case .loading: ProgressView() case .open(let conn, let (host, port)): ConnectedScreen(conn: conn)
-
@@ -51,8 +67,15 @@ "Failed to connect",systemImage: "exclamationmark.magnifyingglass" ) } description: { // TODO: Display error? Text("Server not found") Text("Unable to connect to server due to error.") } actions: { Button { Task { await conn.connect() } } label: { Text("Connect") } } } }
-