[mio] 兼容padavan路由器找不到程序当前路径的问题

This commit is contained in:
lubeilin
2024-03-03 09:45:52 +08:00
parent 1eb2a0f5cf
commit 521280537a
3 changed files with 34 additions and 22 deletions
+16 -14
View File
@@ -13,21 +13,10 @@ 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 port = read_command_port().unwrap_or_else(|e| {
let port = if path_buf.exists() { log::warn!("read_command_port:{:?}", e);
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 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)))?;
udp.connect(SocketAddr::V4(SocketAddrV4::new( udp.connect(SocketAddr::V4(SocketAddrV4::new(
@@ -40,6 +29,19 @@ impl CommandClient {
}) })
} }
} }
fn read_command_port() -> io::Result<u16> {
let path_buf = crate::app_home()?.join("command-port");
let port = std::fs::read_to_string(path_buf)?;
match u16::from_str(&port) {
Ok(port) => Ok(port),
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::Other,
"'command-port' file error",
));
}
}
}
impl CommandClient { impl CommandClient {
pub fn list(&mut self) -> io::Result<Vec<DeviceItem>> { pub fn list(&mut self) -> io::Result<Vec<DeviceItem>> {
+15 -7
View File
@@ -18,13 +18,21 @@ mod console_out;
mod root_check; mod root_check;
pub fn app_home() -> io::Result<PathBuf> { pub fn app_home() -> io::Result<PathBuf> {
let path = std::env::current_exe()? let root_path = match std::env::current_exe() {
.parent() Ok(path) => {
.ok_or(io::Error::new( if let Some(v) = path.as_path().parent() {
io::ErrorKind::Other, v.to_path_buf()
"current_exe parent error", } else {
))? log::warn!("current_exe parent none:{:?}", path);
.join("env"); PathBuf::new()
}
}
Err(e) => {
log::warn!("current_exe err:{:?}", e);
PathBuf::new()
}
};
let path = root_path.join("env");
if !path.exists() { if !path.exists() {
std::fs::create_dir_all(&path)?; std::fs::create_dir_all(&path)?;
} }
+3 -1
View File
@@ -89,7 +89,9 @@ impl<Call: VntCallback> PacketHandler for ServerPacketHandler<Call> {
context: &Context, context: &Context,
current_device: &CurrentDeviceInfo, current_device: &CurrentDeviceInfo,
) -> io::Result<()> { ) -> io::Result<()> {
context.route_table.update_read_time(&net_packet.source(), &route_key); context
.route_table
.update_read_time(&net_packet.source(), &route_key);
if net_packet.protocol() == Protocol::Error if net_packet.protocol() == Protocol::Error
&& net_packet.transport_protocol() && net_packet.transport_protocol()
== crate::protocol::error_packet::Protocol::NoKey.into() == crate::protocol::error_packet::Protocol::NoKey.into()