fix: client connects via interprocess on Windows, not OpenOptions

Server uses interprocess::local_socket, but client was using
std::fs::OpenOptions("\\.\pipe\wx-cli-daemon") which fails to
connect to pipes created by interprocess's tokio listener.

Use the same interprocess client API on both sides for consistency.

Verified with: cargo check --target x86_64-pc-windows-gnu (mingw-w64).
pull/9/head
jackwener 2026-04-17 16:41:16 +08:00
parent 33758671d6
commit 6a2b23486a
1 changed files with 14 additions and 13 deletions

View File

@ -39,10 +39,12 @@ pub fn is_alive() -> bool {
} }
#[cfg(windows)] #[cfg(windows)]
{ {
// 通过 named pipe 检测 use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream};
let pipe_path = r"\\.\pipe\wx-cli-daemon"; // 必须用 interprocess 自己的连接 API和 server 保持一致
use std::fs::OpenOptions; match "wx-cli-daemon".to_ns_name::<GenericNamespaced>() {
OpenOptions::new().read(true).write(true).open(pipe_path).is_ok() Ok(name) => Stream::connect(name).is_ok(),
Err(_) => false,
}
} }
#[cfg(not(any(unix, windows)))] #[cfg(not(any(unix, windows)))]
{ {
@ -174,21 +176,20 @@ fn send_unix(req: Request) -> Result<Response> {
#[cfg(windows)] #[cfg(windows)]
fn send_windows(req: Request) -> Result<Response> { fn send_windows(req: Request) -> Result<Response> {
use std::fs::OpenOptions; use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream};
use std::os::windows::fs::OpenOptionsExt;
let pipe_path = r"\\.\pipe\wx-cli-daemon"; let name = "wx-cli-daemon".to_ns_name::<GenericNamespaced>()
let mut pipe = OpenOptions::new() .context("构造 pipe name 失败")?;
.read(true) let stream = Stream::connect(name)
.write(true)
.open(pipe_path)
.context("连接 daemon named pipe 失败")?; .context("连接 daemon named pipe 失败")?;
// interprocess::Stream 同时实现 Read + Write但需要拆分读写端
let mut reader = BufReader::new(stream);
let req_str = serde_json::to_string(&req)? + "\n"; let req_str = serde_json::to_string(&req)? + "\n";
pipe.write_all(req_str.as_bytes())?; reader.get_mut().write_all(req_str.as_bytes())?;
let mut line = String::new(); let mut line = String::new();
let mut reader = BufReader::new(pipe);
reader.read_line(&mut line)?; reader.read_line(&mut line)?;
let resp: Response = serde_json::from_str(&line) let resp: Response = serde_json::from_str(&line)