[mio] 适配新版vnt
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use tokio::net::UdpSocket;
|
||||
use std::net::UdpSocket;
|
||||
|
||||
use vnt::core::Vnt;
|
||||
|
||||
@@ -13,26 +13,26 @@ impl CommandServer {
|
||||
}
|
||||
|
||||
impl CommandServer {
|
||||
pub async fn start(self, vnt: Vnt) -> io::Result<()> {
|
||||
let udp = if let Ok(udp) = UdpSocket::bind("127.0.0.1:39271").await {
|
||||
pub fn start(self, vnt: Vnt) -> io::Result<()> {
|
||||
let udp = if let Ok(udp) = UdpSocket::bind("127.0.0.1:39271") {
|
||||
udp
|
||||
} else {
|
||||
UdpSocket::bind("127.0.0.1:0").await?
|
||||
UdpSocket::bind("127.0.0.1:0")?
|
||||
};
|
||||
let path_buf = crate::app_home()?.join("command-port");
|
||||
let mut file = std::fs::File::create(path_buf)?;
|
||||
let addr = udp.local_addr()?;
|
||||
file.write_all(addr.port().to_string().as_bytes())?;
|
||||
file.sync_all()?;
|
||||
log::info!("启动后台cmd:{:?}", addr);
|
||||
if let Err(e) = save_port(addr.port()) {
|
||||
log::warn!("保存后台命令端口失败:{:?}", e);
|
||||
}
|
||||
|
||||
let mut buf = [0u8; 64];
|
||||
loop {
|
||||
let (len, addr) = udp.recv_from(&mut buf).await?;
|
||||
let (len, addr) = udp.recv_from(&mut buf)?;
|
||||
match std::str::from_utf8(&buf[..len]) {
|
||||
Ok(cmd) => {
|
||||
log::info!("收到cmd={:?}", cmd);
|
||||
if let Ok(out) = command(cmd, &vnt) {
|
||||
if let Err(e) = udp.send_to(out.as_bytes(), addr).await {
|
||||
if let Err(e) = udp.send_to(out.as_bytes(), addr) {
|
||||
log::warn!("cmd={},err={:?}", cmd, e);
|
||||
}
|
||||
if "stopped" == &out {
|
||||
@@ -48,29 +48,23 @@ impl CommandServer {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn save_port(port: u16) -> io::Result<()> {
|
||||
let path_buf = crate::app_home()?.join("command-port");
|
||||
let mut file = std::fs::File::create(path_buf)?;
|
||||
file.write_all(port.to_string().as_bytes())?;
|
||||
file.sync_all()
|
||||
}
|
||||
|
||||
fn command(cmd: &str, vnt: &Vnt) -> io::Result<String> {
|
||||
let out_str = match cmd {
|
||||
"route" => match serde_json::to_string(&crate::command::command_route(vnt)) {
|
||||
Ok(str) => str,
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
},
|
||||
"list" => match serde_json::to_string(&crate::command::command_list(vnt)) {
|
||||
Ok(str) => str,
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
},
|
||||
"info" => match serde_json::to_string(&crate::command::command_info(vnt)) {
|
||||
Ok(str) => str,
|
||||
Err(e) => {
|
||||
format!("{:?}", e)
|
||||
}
|
||||
},
|
||||
"route" => serde_yaml::to_string(&crate::command::command_route(vnt))
|
||||
.unwrap_or_else(|e| format!("error {:?}", e)),
|
||||
"list" => serde_yaml::to_string(&crate::command::command_list(vnt))
|
||||
.unwrap_or_else(|e| format!("error {:?}", e)),
|
||||
"info" => serde_yaml::to_string(&crate::command::command_info(vnt))
|
||||
.unwrap_or_else(|e| format!("error {:?}", e)),
|
||||
"stop" => {
|
||||
vnt.stop()?;
|
||||
vnt.stop();
|
||||
"stopped".to_string()
|
||||
}
|
||||
_ => {
|
||||
|
||||
Reference in New Issue
Block a user