use eventual; use std::io::Write; use byteorder::{WriteBytesExt, BigEndian}; 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 } }