Returning the result of the command.

master
Tom Alexander 2 years ago
parent 132d3cc59c
commit 3732f04c36
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -15,6 +15,10 @@ impl Command {
asynchronous: false,
}
}
pub fn get_request_id(&self) -> &Option<u64> {
return &self.request_id;
}
}
impl Serialize for Command {

@ -18,6 +18,7 @@ type CommandDb = Arc<Mutex<HashMap<u64, oneshot::Sender<serde_json::Value>>>>;
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<dyn std::error::Error>> {
async fn send_command(
&mut self,
cmd: Command,
) -> Result<oneshot::Receiver<serde_json::Value>, Box<dyn std::error::Error>> {
let serialized = serde_json::to_string(&cmd)?;
println!("Sending {}", serialized);
let (response_tx, response_rx) = oneshot::channel::<serde_json::Value>();
// 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<dyn std::error::Error>> {

Loading…
Cancel
Save