命令默认使用39271端口

This commit is contained in:
lubeilin
2024-01-06 11:30:05 +08:00
parent d06082fc7a
commit 57b903ea29
2 changed files with 17 additions and 12 deletions
+12 -11
View File
@@ -12,18 +12,19 @@ pub struct CommandClient {
impl CommandClient { impl CommandClient {
pub fn new() -> io::Result<Self> { pub fn new() -> io::Result<Self> {
let path_buf = crate::app_home()?.join("command-port"); let path_buf = crate::app_home()?.join("command-port");
if !path_buf.exists() { let port = if path_buf.exists() {
return Err(io::Error::new(io::ErrorKind::Other, "not started")); let port = std::fs::read_to_string(path_buf)?;
} match u16::from_str(&port) {
let port = std::fs::read_to_string(path_buf)?; Ok(port) => port,
let port = match u16::from_str(&port) { Err(_) => {
Ok(port) => port, return Err(io::Error::new(
Err(_) => { io::ErrorKind::Other,
return Err(io::Error::new( "'command-port' file error",
io::ErrorKind::Other, ));
"'command-port' file error", }
));
} }
} else {
39271
}; };
let udp = UdpSocket::bind("127.0.0.1:0")?; let udp = UdpSocket::bind("127.0.0.1:0")?;
udp.set_read_timeout(Some(Duration::from_secs(5)))?; udp.set_read_timeout(Some(Duration::from_secs(5)))?;
+5 -1
View File
@@ -14,7 +14,11 @@ impl CommandServer {
impl CommandServer { impl CommandServer {
pub async fn start(self, vnt: Vnt) -> io::Result<()> { 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 path_buf = crate::app_home()?.join("command-port");
let mut file = std::fs::File::create(path_buf)?; let mut file = std::fs::File::create(path_buf)?;
let addr = udp.local_addr()?; let addr = udp.local_addr()?;