mirror of
https://github.com/NotAShelf/mpvrc.git
synced 2026-04-18 00:38:14 +00:00
terminate IPC messages with newlines
MPV will not accept them otherwise.
This commit is contained in:
parent
56328c71f9
commit
af697ff594
1 changed files with 32 additions and 14 deletions
46
src/main.rs
46
src/main.rs
|
|
@ -37,25 +37,43 @@ enum CommandOptions {
|
||||||
const SOCKET_PATH: &str = "/tmp/mpvsocket";
|
const SOCKET_PATH: &str = "/tmp/mpvsocket";
|
||||||
async fn send_ipc_command(command: &str, args: &[serde_json::Value]) -> io::Result<Option<serde_json::Value>> {
|
async fn send_ipc_command(command: &str, args: &[serde_json::Value]) -> io::Result<Option<serde_json::Value>> {
|
||||||
debug!("Sending IPC command: {} with arguments: {:?}", command, args);
|
debug!("Sending IPC command: {} with arguments: {:?}", command, args);
|
||||||
let mut socket = UnixStream::connect(SOCKET_PATH).await?;
|
|
||||||
let message = json!({ "command": [command, args] });
|
|
||||||
let message_str = serde_json::to_string(&message)?;
|
|
||||||
|
|
||||||
socket.write_all(message_str.as_bytes()).await?;
|
match UnixStream::connect(SOCKET_PATH).await {
|
||||||
socket.flush().await?;
|
Ok(mut socket) => {
|
||||||
|
debug!("Connected to MPV socket successfully");
|
||||||
|
|
||||||
let mut response = vec![0; 1024];
|
let mut command_array = vec![json!(command)];
|
||||||
let n = socket.read(&mut response).await?;
|
command_array.extend_from_slice(args);
|
||||||
let response_str = String::from_utf8_lossy(&response[..n]);
|
let message = json!({ "command": command_array });
|
||||||
|
|
||||||
match serde_json::from_str::<serde_json::Value>(&response_str) {
|
let message_str = format!("{}\n", serde_json::to_string(&message)?);
|
||||||
Ok(json_response) => {
|
debug!("Serialized message to send with newline: {}", message_str);
|
||||||
debug!("Received response: {}", response_str);
|
|
||||||
Ok(json_response.get("data").cloned())
|
socket.write_all(message_str.as_bytes()).await?;
|
||||||
|
socket.flush().await?;
|
||||||
|
debug!("Message sent and flushed");
|
||||||
|
|
||||||
|
let mut response = vec![0; 1024];
|
||||||
|
let n = socket.read(&mut response).await?;
|
||||||
|
let response_str = String::from_utf8_lossy(&response[..n]);
|
||||||
|
debug!("Raw response: {}", response_str);
|
||||||
|
|
||||||
|
match serde_json::from_str::<serde_json::Value>(&response_str) {
|
||||||
|
Ok(json_response) => {
|
||||||
|
debug!("Parsed IPC response: {:?}", json_response);
|
||||||
|
Ok(json_response.get("data").cloned())
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to parse response: {}", e);
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to parse response: {}", e);
|
error!("Failed to connect to MPV socket: {}", e);
|
||||||
Ok(None)
|
Err(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue