فهرست منبع

Move album cover to tokio

Paul Lietar 8 سال پیش
والد
کامیت
bf6be73caa
2فایلهای تغییر یافته به همراه12 افزوده شده و 70 حذف شده
  1. 11 70
      src/album_cover.rs
  2. 1 0
      src/lib.rs

+ 11 - 70
src/album_cover.rs

@@ -1,78 +1,19 @@
-use eventual;
+use byteorder::{BigEndian, WriteBytesExt};
 use std::io::Write;
-use byteorder::{WriteBytesExt, BigEndian};
 
+use channel::ChannelData;
 use session::Session;
 use util::FileId;
-use stream;
 
-pub struct AlbumCover {
-    file_id: FileId,
-    data: Vec<u8>,
-    cover_tx: eventual::Complete<Vec<u8>, ()>,
-}
-
-impl stream::Handler for AlbumCover {
-    fn on_create(self, channel_id: stream::ChannelId, session: &Session) -> stream::Response<Self> {
-        let mut req: Vec<u8> = Vec::new();
-        req.write_u16::<BigEndian>(channel_id).unwrap();
-        req.write_u16::<BigEndian>(0).unwrap();
-        req.write(&self.file_id.0).unwrap();
-        session.send_packet(0x19, req);
-
-        stream::Response::Continue(self)
-    }
-
-    fn on_header(self, _header_id: u8, _header_data: &[u8], _session: &Session) -> stream::Response<Self> {
-        stream::Response::Continue(self)
-    }
-
-    fn on_data(mut self, data: &[u8], _session: &Session) -> stream::Response<Self> {
-        self.data.extend_from_slice(data);
-        stream::Response::Continue(self)
-    }
-
-    fn on_close(self, _session: &Session) -> stream::Response<Self> {
-        // End of chunk, request a new one
-        self.cover_tx.complete(self.data);
-        stream::Response::Close
-    }
-
-    fn on_error(self, _session: &Session) -> stream::Response<Self> {
-        self.cover_tx.fail(());
-        stream::Response::Close
-    }
-
-    fn box_on_create(self: Box<Self>, channel_id: stream::ChannelId, session: &Session) -> stream::Response<Box<stream::Handler>> {
-        self.on_create(channel_id, session).boxed()
-    }
-
-    fn box_on_header(self: Box<Self>, header_id: u8, header_data: &[u8], session: &Session) -> stream::Response<Box<stream::Handler>> {
-        self.on_header(header_id, header_data, session).boxed()
-    }
-
-    fn box_on_data(self: Box<Self>, data: &[u8], session: &Session) -> stream::Response<Box<stream::Handler>> {
-        self.on_data(data, session).boxed()
-    }
-
-    fn box_on_error(self: Box<Self>, session: &Session) -> stream::Response<Box<stream::Handler>> {
-        self.on_error(session).boxed()
-    }
-
-    fn box_on_close(self: Box<Self>, session: &Session) -> stream::Response<Box<stream::Handler>> {
-        self.on_close(session).boxed()
-    }
-}
+pub fn get(session: &Session, file: FileId) -> ChannelData {
+    let (channel_id, channel) = session.channel().allocate();
+    let (_headers, data) = channel.split();
 
-impl AlbumCover {
-    pub fn get(file_id: FileId, session: &Session) -> eventual::Future<Vec<u8>, ()> {
-        let (tx, rx) = eventual::Future::pair();
-        session.stream(Box::new(AlbumCover {
-            file_id: file_id,
-            data: Vec::new(),
-            cover_tx: tx,
-        }));
+    let mut packet: Vec<u8> = Vec::new();
+    packet.write_u16::<BigEndian>(channel_id).unwrap();
+    packet.write_u16::<BigEndian>(0).unwrap();
+    packet.write(&file.0).unwrap();
+    session.send_packet(0x19, packet);
 
-        rx
-    }
+    data
 }

+ 1 - 0
src/lib.rs

@@ -50,6 +50,7 @@ extern crate libpulse_sys;
 
 #[macro_use] mod component;
 
+pub mod album_cover;
 pub mod audio_backend;
 pub mod audio_decrypt;
 pub mod audio_file;