From 57b903ea29d5cf1327cae28816fd3e411d1b0cc8 Mon Sep 17 00:00:00 2001 From: lubeilin <1791778603@qq.com> Date: Sat, 6 Jan 2024 11:30:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E4=BB=A4=E9=BB=98=E8=AE=A4=E4=BD=BF?= =?UTF-8?q?=E7=94=A839271=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt-cli/src/command/client.rs | 23 ++++++++++++----------- vnt-cli/src/command/server.rs | 6 +++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/vnt-cli/src/command/client.rs b/vnt-cli/src/command/client.rs index 23eb7ca..65a845f 100644 --- a/vnt-cli/src/command/client.rs +++ b/vnt-cli/src/command/client.rs @@ -12,18 +12,19 @@ pub struct CommandClient { impl CommandClient { pub fn new() -> io::Result { let path_buf = crate::app_home()?.join("command-port"); - if !path_buf.exists() { - return Err(io::Error::new(io::ErrorKind::Other, "not started")); - } - let port = std::fs::read_to_string(path_buf)?; - let port = match u16::from_str(&port) { - Ok(port) => port, - Err(_) => { - return Err(io::Error::new( - io::ErrorKind::Other, - "'command-port' file error", - )); + let port = if path_buf.exists() { + let port = std::fs::read_to_string(path_buf)?; + match u16::from_str(&port) { + Ok(port) => port, + Err(_) => { + return Err(io::Error::new( + io::ErrorKind::Other, + "'command-port' file error", + )); + } } + } else { + 39271 }; let udp = UdpSocket::bind("127.0.0.1:0")?; udp.set_read_timeout(Some(Duration::from_secs(5)))?; diff --git a/vnt-cli/src/command/server.rs b/vnt-cli/src/command/server.rs index 75b0a22..603978a 100644 --- a/vnt-cli/src/command/server.rs +++ b/vnt-cli/src/command/server.rs @@ -14,7 +14,11 @@ impl CommandServer { impl CommandServer { pub async fn start(self, vnt: Vnt) -> io::Result<()> { - let udp = UdpSocket::bind("127.0.0.1:0").await?; + let udp = if let Ok(udp) = UdpSocket::bind("127.0.0.1:39271").await { + udp + } else { + UdpSocket::bind("127.0.0.1:0").await? + }; let path_buf = crate::app_home()?.join("command-port"); let mut file = std::fs::File::create(path_buf)?; let addr = udp.local_addr()?;