From 755aa2e5a0c8c0e10be2dd4c518bd201f13a6300 Mon Sep 17 00:00:00 2001 From: Felix Prillwitz Date: Tue, 17 Dec 2024 18:57:02 +0100 Subject: [PATCH] Dealer: Improve disconnect (#1420) * connect: adjust disconnect behavior * update CHANGELOG.md * core: adjust param of `set_session_id` * connect: move unexpected disconnect outside the loop again --- CHANGELOG.md | 1 + connect/src/spirc.rs | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2611eb0..72710acc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [connect] Add `seek_to` field to `SpircLoadCommand` (breaking) - [connect] Add `repeat_track` field to `SpircLoadCommand` (breaking) +- [connect] Add `pause` parameter to `Spirc::disconnect` method (breaking) - [playback] Add `track` field to `PlayerEvent::RepeatChanged` (breaking) - [core] Add `request_with_options` and `request_with_protobuf_and_options` to `SpClient` diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index 1ed9bd12..32124570 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -129,7 +129,7 @@ enum SpircCommand { Shuffle(bool), Repeat(bool), RepeatTrack(bool), - Disconnect, + Disconnect { pause: bool }, SetPosition(u32), SetVolume(u16), Activate, @@ -311,8 +311,8 @@ impl Spirc { pub fn set_position_ms(&self, position_ms: u32) -> Result<(), Error> { Ok(self.commands.send(SpircCommand::SetPosition(position_ms))?) } - pub fn disconnect(&self) -> Result<(), Error> { - Ok(self.commands.send(SpircCommand::Disconnect)?) + pub fn disconnect(&self, pause: bool) -> Result<(), Error> { + Ok(self.commands.send(SpircCommand::Disconnect { pause })?) } pub fn activate(&self) -> Result<(), Error> { Ok(self.commands.send(SpircCommand::Activate)?) @@ -438,20 +438,17 @@ impl SpircTask { error!("error updating connect state for volume update: {why}") } }, - else => break + else => break, } } if !self.shutdown && self.connect_state.is_active() { - if let Err(why) = self.notify().await { - warn!("notify before unexpected shutdown couldn't be send: {why}") + warn!("unexpected shutdown"); + if let Err(why) = self.handle_disconnect().await { + error!("error during disconnecting: {why}") } } - // clears the session id, leaving an empty state - if let Err(why) = self.session.spclient().delete_connect_state_request().await { - warn!("deleting connect_state failed before unexpected shutdown: {why}") - } self.session.dealer().close().await; } @@ -651,7 +648,10 @@ impl SpircTask { self.handle_volume_down(); self.notify().await } - SpircCommand::Disconnect => { + SpircCommand::Disconnect { pause } => { + if pause { + self.handle_pause() + } self.handle_disconnect().await?; self.notify().await } @@ -1142,15 +1142,18 @@ impl SpircTask { } async fn handle_disconnect(&mut self) -> Result<(), Error> { - self.handle_stop(); - - self.play_status = SpircPlayStatus::Stopped {}; self.connect_state .update_position_in_relation(self.now_ms()); self.notify().await?; self.connect_state.became_inactive(&self.session).await?; + // this should clear the active session id, leaving an empty state + self.session + .spclient() + .delete_connect_state_request() + .await?; + self.player .emit_session_disconnected_event(self.session.connection_id(), self.session.username());