Skip to content

Commit

Permalink
Dealer: Improve disconnect (#1420)
Browse files Browse the repository at this point in the history
* connect: adjust disconnect behavior

* update CHANGELOG.md

* core: adjust param of `set_session_id`

* connect: move unexpected disconnect outside the loop again
  • Loading branch information
photovoltex authored Dec 17, 2024
1 parent 597974f commit 755aa2e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
31 changes: 17 additions & 14 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ enum SpircCommand {
Shuffle(bool),
Repeat(bool),
RepeatTrack(bool),
Disconnect,
Disconnect { pause: bool },
SetPosition(u32),
SetVolume(u16),
Activate,
Expand Down Expand Up @@ -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)?)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 755aa2e

Please sign in to comment.