diff --git a/src/mpvctl/command.rs b/src/mpvctl/command.rs index 10428a9..2521991 100644 --- a/src/mpvctl/command.rs +++ b/src/mpvctl/command.rs @@ -15,6 +15,10 @@ impl Command { asynchronous: false, } } + + pub fn get_request_id(&self) -> &Option { + return &self.request_id; + } } impl Serialize for Command { diff --git a/src/mpvctl/mpvctl.rs b/src/mpvctl/mpvctl.rs index 8e6c2c7..6671221 100644 --- a/src/mpvctl/mpvctl.rs +++ b/src/mpvctl/mpvctl.rs @@ -18,6 +18,7 @@ type CommandDb = Arc>>>; pub struct MpvCtl { socket: OwnedWriteHalf, next_request_id: u64, + in_flight_requests: CommandDb, } impl MpvCtl { @@ -28,6 +29,7 @@ impl MpvCtl { let (socket_read, socket_write) = socket.into_split(); let mut framed_read = MpvFramed::new(socket_read); let db: CommandDb = Arc::new(Mutex::new(HashMap::new())); + let read_db_handle = db.clone(); tokio::spawn(async move { loop { @@ -54,7 +56,7 @@ impl MpvCtl { }; { - let mut db_handle = db.lock().unwrap(); + let mut db_handle = read_db_handle.lock().unwrap(); if let Entry::Occupied(o) = (*db_handle).entry(reqid) { o.remove() .send(frame) @@ -77,6 +79,7 @@ impl MpvCtl { Ok(MpvCtl { socket: socket_write, next_request_id: 0, + in_flight_requests: db, }) } @@ -86,14 +89,26 @@ impl MpvCtl { ret } - async fn send_command(&mut self, cmd: Command) -> Result<(), Box> { + async fn send_command( + &mut self, + cmd: Command, + ) -> Result, Box> { let serialized = serde_json::to_string(&cmd)?; println!("Sending {}", serialized); let (response_tx, response_rx) = oneshot::channel::(); - // TODO: store one stream where the reading thread can get it? Then wait for response on this end and return it from this function. + match cmd.get_request_id() { + Some(reqid) => { + self.in_flight_requests + .lock() + .unwrap() + .insert(*reqid, response_tx); + } + None => (), + }; self.socket.write_all(serialized.as_bytes()).await?; - Ok(()) + + Ok(response_rx) } pub async fn get_client_name(&mut self) -> Result<(), Box> {