Returning the result of the command.
This commit is contained in:
parent
132d3cc59c
commit
3732f04c36
@ -15,6 +15,10 @@ impl Command {
|
|||||||
asynchronous: false,
|
asynchronous: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_request_id(&self) -> &Option<u64> {
|
||||||
|
return &self.request_id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Command {
|
impl Serialize for Command {
|
||||||
|
@ -18,6 +18,7 @@ type CommandDb = Arc<Mutex<HashMap<u64, oneshot::Sender<serde_json::Value>>>>;
|
|||||||
pub struct MpvCtl {
|
pub struct MpvCtl {
|
||||||
socket: OwnedWriteHalf,
|
socket: OwnedWriteHalf,
|
||||||
next_request_id: u64,
|
next_request_id: u64,
|
||||||
|
in_flight_requests: CommandDb,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MpvCtl {
|
impl MpvCtl {
|
||||||
@ -28,6 +29,7 @@ impl MpvCtl {
|
|||||||
let (socket_read, socket_write) = socket.into_split();
|
let (socket_read, socket_write) = socket.into_split();
|
||||||
let mut framed_read = MpvFramed::new(socket_read);
|
let mut framed_read = MpvFramed::new(socket_read);
|
||||||
let db: CommandDb = Arc::new(Mutex::new(HashMap::new()));
|
let db: CommandDb = Arc::new(Mutex::new(HashMap::new()));
|
||||||
|
let read_db_handle = db.clone();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
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) {
|
if let Entry::Occupied(o) = (*db_handle).entry(reqid) {
|
||||||
o.remove()
|
o.remove()
|
||||||
.send(frame)
|
.send(frame)
|
||||||
@ -77,6 +79,7 @@ impl MpvCtl {
|
|||||||
Ok(MpvCtl {
|
Ok(MpvCtl {
|
||||||
socket: socket_write,
|
socket: socket_write,
|
||||||
next_request_id: 0,
|
next_request_id: 0,
|
||||||
|
in_flight_requests: db,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,14 +89,26 @@ impl MpvCtl {
|
|||||||
ret
|
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)?;
|
let serialized = serde_json::to_string(&cmd)?;
|
||||||
println!("Sending {}", serialized);
|
println!("Sending {}", serialized);
|
||||||
let (response_tx, response_rx) = oneshot::channel::<serde_json::Value>();
|
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?;
|
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>> {
|
pub async fn get_client_name(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user