From 6a2b23486a4e37b98c32610cb903c2033c64083c Mon Sep 17 00:00:00 2001 From: jackwener Date: Fri, 17 Apr 2026 16:41:16 +0800 Subject: [PATCH] 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). --- src/cli/transport.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/cli/transport.rs b/src/cli/transport.rs index b2e0d43..3f63d9d 100644 --- a/src/cli/transport.rs +++ b/src/cli/transport.rs @@ -39,10 +39,12 @@ pub fn is_alive() -> bool { } #[cfg(windows)] { - // 通过 named pipe 检测 - let pipe_path = r"\\.\pipe\wx-cli-daemon"; - use std::fs::OpenOptions; - OpenOptions::new().read(true).write(true).open(pipe_path).is_ok() + use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream}; + // 必须用 interprocess 自己的连接 API,和 server 保持一致 + match "wx-cli-daemon".to_ns_name::() { + Ok(name) => Stream::connect(name).is_ok(), + Err(_) => false, + } } #[cfg(not(any(unix, windows)))] { @@ -174,21 +176,20 @@ fn send_unix(req: Request) -> Result { #[cfg(windows)] fn send_windows(req: Request) -> Result { - use std::fs::OpenOptions; - use std::os::windows::fs::OpenOptionsExt; + use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream}; - let pipe_path = r"\\.\pipe\wx-cli-daemon"; - let mut pipe = OpenOptions::new() - .read(true) - .write(true) - .open(pipe_path) + let name = "wx-cli-daemon".to_ns_name::() + .context("构造 pipe name 失败")?; + let stream = Stream::connect(name) .context("连接 daemon named pipe 失败")?; + // interprocess::Stream 同时实现 Read + Write,但需要拆分读写端 + let mut reader = BufReader::new(stream); + 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 reader = BufReader::new(pipe); reader.read_line(&mut line)?; let resp: Response = serde_json::from_str(&line)