[mio] 兼容padavan路由器找不到程序当前路径的问题
This commit is contained in:
@@ -13,21 +13,10 @@ pub struct CommandClient {
|
||||
|
||||
impl CommandClient {
|
||||
pub fn new() -> io::Result<Self> {
|
||||
let path_buf = crate::app_home()?.join("command-port");
|
||||
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 {
|
||||
let port = read_command_port().unwrap_or_else(|e| {
|
||||
log::warn!("read_command_port:{:?}", e);
|
||||
39271
|
||||
};
|
||||
});
|
||||
let udp = UdpSocket::bind("127.0.0.1:0")?;
|
||||
udp.set_read_timeout(Some(Duration::from_secs(5)))?;
|
||||
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 {
|
||||
pub fn list(&mut self) -> io::Result<Vec<DeviceItem>> {
|
||||
|
||||
+15
-7
@@ -18,13 +18,21 @@ mod console_out;
|
||||
mod root_check;
|
||||
|
||||
pub fn app_home() -> io::Result<PathBuf> {
|
||||
let path = std::env::current_exe()?
|
||||
.parent()
|
||||
.ok_or(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"current_exe parent error",
|
||||
))?
|
||||
.join("env");
|
||||
let root_path = match std::env::current_exe() {
|
||||
Ok(path) => {
|
||||
if let Some(v) = path.as_path().parent() {
|
||||
v.to_path_buf()
|
||||
} else {
|
||||
log::warn!("current_exe parent none:{:?}", path);
|
||||
PathBuf::new()
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("current_exe err:{:?}", e);
|
||||
PathBuf::new()
|
||||
}
|
||||
};
|
||||
let path = root_path.join("env");
|
||||
if !path.exists() {
|
||||
std::fs::create_dir_all(&path)?;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,9 @@ impl<Call: VntCallback> PacketHandler for ServerPacketHandler<Call> {
|
||||
context: &Context,
|
||||
current_device: &CurrentDeviceInfo,
|
||||
) -> 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
|
||||
&& net_packet.transport_protocol()
|
||||
== crate::protocol::error_packet::Protocol::NoKey.into()
|
||||
|
||||
Reference in New Issue
Block a user