Sfoglia il codice sorgente

Better handling of shutdown

Paul Lietar 7 anni fa
parent
commit
9873eaf2a0
5 ha cambiato i file con 14 aggiunte e 3 eliminazioni
  1. 1 0
      src/cache/mod.rs
  2. 1 0
      src/main.rs
  3. 10 2
      src/player.rs
  4. 1 0
      src/session.rs
  5. 1 1
      src/spirc.rs

+ 1 - 0
src/cache/mod.rs

@@ -5,6 +5,7 @@ use std::fs::File;
 use util::{FileId, mkdir_existing};
 use authentication::Credentials;
 
+#[derive(Clone)]
 pub struct Cache {
     root: PathBuf,
 }

+ 1 - 0
src/main.rs

@@ -62,6 +62,7 @@ fn list_backends() {
     }
 }
 
+#[derive(Clone)]
 struct Setup {
     backend: &'static (Fn(Option<String>) -> Box<Sink> + Send + Sync),
     cache: Option<Cache>,

+ 10 - 2
src/player.rs

@@ -3,6 +3,7 @@ use futures::{future, Future};
 use std::borrow::Cow;
 use std::io::{Read, Seek};
 use std::mem;
+use std::sync::mpsc::{RecvError, TryRecvError};
 use std::thread;
 use std;
 use vorbis::{self, VorbisError};
@@ -174,9 +175,16 @@ impl PlayerInternal {
     fn run(mut self) {
         loop {
             let cmd = if self.state.is_playing() {
-                self.commands.try_recv().ok()
+                match self.commands.try_recv() {
+                    Ok(cmd) => Some(cmd),
+                    Err(TryRecvError::Empty) => None,
+                    Err(TryRecvError::Disconnected) => return,
+                }
             } else {
-                Some(self.commands.recv().unwrap())
+                match self.commands.recv() {
+                    Ok(cmd) => Some(cmd),
+                    Err(RecvError) => return,
+                }
             };
 
             if let Some(cmd) = cmd {

+ 1 - 0
src/session.rs

@@ -40,6 +40,7 @@ impl FromStr for Bitrate {
     }
 }
 
+#[derive(Clone)]
 pub struct Config {
     pub user_agent: String,
     pub name: String,

+ 1 - 1
src/spirc.rs

@@ -160,7 +160,7 @@ impl Spirc {
     }
 
     pub fn shutdown(&self) {
-        mpsc::UnboundedSender::send(&self.commands, SpircCommand::Shutdown).unwrap();
+        let _ = mpsc::UnboundedSender::send(&self.commands, SpircCommand::Shutdown);
     }
 }