From bf6be73caa3c16941392dcb00d3ff76186081739 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Fri, 20 Jan 2017 01:59:18 +0000 Subject: [PATCH] Move album cover to tokio --- src/album_cover.rs | 85 +++++++--------------------------------------- src/lib.rs | 1 + 2 files changed, 14 insertions(+), 72 deletions(-) diff --git a/src/album_cover.rs b/src/album_cover.rs index 3c56a46e..ae800ecb 100644 --- a/src/album_cover.rs +++ b/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, - cover_tx: eventual::Complete, ()>, -} - -impl stream::Handler for AlbumCover { - fn on_create(self, channel_id: stream::ChannelId, session: &Session) -> stream::Response { - let mut req: Vec = Vec::new(); - req.write_u16::(channel_id).unwrap(); - req.write_u16::(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 { - stream::Response::Continue(self) - } - - fn on_data(mut self, data: &[u8], _session: &Session) -> stream::Response { - self.data.extend_from_slice(data); - stream::Response::Continue(self) - } - - fn on_close(self, _session: &Session) -> stream::Response { - // 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.cover_tx.fail(()); - stream::Response::Close - } - - fn box_on_create(self: Box, channel_id: stream::ChannelId, session: &Session) -> stream::Response> { - self.on_create(channel_id, session).boxed() - } - - fn box_on_header(self: Box, header_id: u8, header_data: &[u8], session: &Session) -> stream::Response> { - self.on_header(header_id, header_data, session).boxed() - } - - fn box_on_data(self: Box, data: &[u8], session: &Session) -> stream::Response> { - self.on_data(data, session).boxed() - } - - fn box_on_error(self: Box, session: &Session) -> stream::Response> { - self.on_error(session).boxed() - } - - fn box_on_close(self: Box, session: &Session) -> stream::Response> { - self.on_close(session).boxed() - } -} - -impl AlbumCover { - pub fn get(file_id: FileId, session: &Session) -> eventual::Future, ()> { - let (tx, rx) = eventual::Future::pair(); - session.stream(Box::new(AlbumCover { - file_id: file_id, - data: Vec::new(), - cover_tx: tx, - })); - - rx - } +pub fn get(session: &Session, file: FileId) -> ChannelData { + let (channel_id, channel) = session.channel().allocate(); + let (_headers, data) = channel.split(); + + let mut packet: Vec = Vec::new(); + packet.write_u16::(channel_id).unwrap(); + packet.write_u16::(0).unwrap(); + packet.write(&file.0).unwrap(); + session.send_packet(0x19, packet); + + data } diff --git a/src/lib.rs b/src/lib.rs index e4a173e5..e6f32471 100644 --- a/src/lib.rs +++ b/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;