do not panic on message without event field

intead discard it and try again
This commit is contained in:
jole 2022-07-19 21:27:02 +02:00
parent fded248b7e
commit 9fde540089
1 changed files with 79 additions and 70 deletions

View File

@ -335,15 +335,27 @@ fn try_convert_property(name: &str, id: usize, data: MpvDataType) -> Event {
}
pub fn listen(instance: &mut Mpv) -> Result<Event, Error> {
let mut e;
// sometimes we get responses unrelated to events, so we read a new line until we receive one
// with an event field
let name = loop {
let mut response = String::new();
instance.reader.read_line(&mut response).unwrap();
response = response.trim_end().to_string();
debug!("Event: {}", response);
let e = serde_json::from_str::<Value>(&response)
e = serde_json::from_str::<Value>(&response)
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))?;
if let Value::String(ref name) = e["event"] {
match e["event"] {
Value::String(ref name) => break name,
_ => {
// It was not an event - try again
debug!("Bad response: {:?}", response)
}
}
};
let event = match name.as_str() {
"shutdown" => Event::Shutdown,
"start-file" => Event::StartFile,
@ -405,9 +417,6 @@ pub fn listen(instance: &mut Mpv) -> Result<Event, Error> {
_ => Event::Unimplemented,
};
Ok(event)
} else {
unreachable!();
}
}
pub fn listen_raw(instance: &mut Mpv) -> String {