Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealer: Improve disconnect #1420

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 20 additions & 19 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,15 @@ impl SpircTask {
error!("error updating connect state for volume update: {why}")
}
},
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}")
else => {
photovoltex marked this conversation as resolved.
Show resolved Hide resolved
if let Err(why) = self.handle_disconnect().await {
error!("error during disconnect: {why}")
}
break
}
}
}

// 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 +646,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 +1140,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 Expand Up @@ -1554,7 +1555,7 @@ impl SpircTask {
);

if self.session.session_id() != session.session_id {
self.session.set_session_id(session.session_id.clone());
self.session.set_session_id(&session.session_id);
self.connect_state.set_session_id(session.session_id);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl Session {
self.0.data.read().session_id.clone()
}

pub fn set_session_id(&self, session_id: String) {
pub fn set_session_id(&self, session_id: &str) {
session_id.clone_into(&mut self.0.data.write().session_id);
}

Expand Down
Loading