2016-07-06 00:36:46 +00:00
|
|
|
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
2015-09-01 11:20:37 +00:00
|
|
|
use eventual;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{Cursor, Read, Write};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
use util::{SpotifyId, FileId};
|
2016-05-09 11:22:51 +00:00
|
|
|
use session::{Session, PacketHandler};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
pub type AudioKey = [u8; 16];
|
2015-12-29 22:12:02 +00:00
|
|
|
#[derive(Debug,Hash,PartialEq,Eq,Copy,Clone)]
|
|
|
|
pub struct AudioKeyError;
|
2015-07-02 21:05:47 +00:00
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
#[derive(Debug,Hash,PartialEq,Eq,Copy,Clone)]
|
2015-07-02 21:05:47 +00:00
|
|
|
struct AudioKeyId(SpotifyId, FileId);
|
|
|
|
|
2015-06-23 14:38:29 +00:00
|
|
|
pub struct AudioKeyManager {
|
2015-07-02 21:05:47 +00:00
|
|
|
next_seq: u32,
|
2016-01-02 15:19:39 +00:00
|
|
|
pending: HashMap<u32, AudioKeyId>,
|
2016-03-16 04:07:04 +00:00
|
|
|
cache: HashMap<AudioKeyId, Vec<eventual::Complete<AudioKey, AudioKeyError>>>,
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioKeyManager {
|
2015-07-02 17:24:25 +00:00
|
|
|
pub fn new() -> AudioKeyManager {
|
|
|
|
AudioKeyManager {
|
2015-06-23 14:38:29 +00:00
|
|
|
next_seq: 1,
|
2015-07-02 21:05:47 +00:00
|
|
|
pending: HashMap::new(),
|
2016-01-02 15:19:39 +00:00
|
|
|
cache: HashMap::new(),
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 15:48:44 +00:00
|
|
|
fn send_key_request(&mut self, session: &Session, track: SpotifyId, file: FileId) -> u32 {
|
|
|
|
let seq = self.next_seq;
|
|
|
|
self.next_seq += 1;
|
|
|
|
|
|
|
|
let mut data: Vec<u8> = Vec::new();
|
|
|
|
data.write(&file.0).unwrap();
|
|
|
|
data.write(&track.to_raw()).unwrap();
|
|
|
|
data.write_u32::<BigEndian>(seq).unwrap();
|
|
|
|
data.write_u16::<BigEndian>(0x0000).unwrap();
|
|
|
|
|
2017-01-18 05:57:36 +00:00
|
|
|
session.send_packet(0xc, data);
|
2016-01-02 15:48:44 +00:00
|
|
|
|
|
|
|
seq
|
|
|
|
}
|
|
|
|
|
2016-01-02 15:19:39 +00:00
|
|
|
pub fn request(&mut self,
|
|
|
|
session: &Session,
|
|
|
|
track: SpotifyId,
|
|
|
|
file: FileId)
|
|
|
|
-> eventual::Future<AudioKey, AudioKeyError> {
|
2015-07-02 17:24:25 +00:00
|
|
|
|
2015-07-02 21:05:47 +00:00
|
|
|
let id = AudioKeyId(track, file);
|
2016-01-02 15:19:39 +00:00
|
|
|
self.cache
|
|
|
|
.get_mut(&id)
|
2016-03-16 04:07:04 +00:00
|
|
|
.map(|ref mut requests| {
|
|
|
|
let (tx, rx) = eventual::Future::pair();
|
|
|
|
requests.push(tx);
|
|
|
|
rx
|
2016-01-02 15:19:39 +00:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
2016-01-02 15:48:44 +00:00
|
|
|
let seq = self.send_key_request(session, track, file);
|
2016-01-02 15:19:39 +00:00
|
|
|
self.pending.insert(seq, id.clone());
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2016-01-02 15:19:39 +00:00
|
|
|
let (tx, rx) = eventual::Future::pair();
|
2016-03-16 04:07:04 +00:00
|
|
|
self.cache.insert(id, vec![tx]);
|
2016-01-02 15:19:39 +00:00
|
|
|
rx
|
|
|
|
})
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
impl PacketHandler for AudioKeyManager {
|
2016-05-09 11:22:51 +00:00
|
|
|
fn handle(&mut self, cmd: u8, data: Vec<u8>, _session: &Session) {
|
2015-07-02 17:24:25 +00:00
|
|
|
let mut data = Cursor::new(data);
|
2015-06-23 14:38:29 +00:00
|
|
|
let seq = data.read_u32::<BigEndian>().unwrap();
|
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
if let Some(callbacks) = self.pending.remove(&seq).and_then(|id| self.cache.remove(&id)) {
|
2015-12-29 22:12:02 +00:00
|
|
|
if cmd == 0xd {
|
|
|
|
let mut key = [0u8; 16];
|
|
|
|
data.read_exact(&mut key).unwrap();
|
2015-07-02 21:05:47 +00:00
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
for cb in callbacks {
|
|
|
|
cb.complete(key);
|
2015-12-29 22:12:02 +00:00
|
|
|
}
|
|
|
|
} else if cmd == 0xe {
|
|
|
|
let error = AudioKeyError;
|
2016-03-16 04:07:04 +00:00
|
|
|
for cb in callbacks {
|
|
|
|
cb.fail(error);
|
2015-07-02 21:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|