(源代码)main.rs:
use std::net::TcpStream;
use std::time::Duration;
use std::io::{Read, Write, Error, ErrorKind};
use chrono::{DateTime, Local};
// 交叉编译:
// cargo rustc --target=armv7-unknown-linux-gnueabi -- -Clinker=arm-linux-gcc
fn main() {
println!("shelf99 v2 started");
loop {
let _ = work().map_err(|e| println!("[{}] work failed: {e:?}", now()));
std::thread::sleep(Duration::from_secs(30));
}
}
fn work() -> std::io::Result<()> {
const ADDR: &str = "172.17.200.251:9696";
println!("[{}] connecting {ADDR}...", now());
let mut buf = vec![0; 256];
let mut stream = TcpStream::connect(ADDR)?;
loop {
let req = &[0x51, 0x4e, 0x00, 0x06, 0xf3, 0xf0, 0x80, 0x01, 0x00, 0x63, 0x00, 0x45];
stream.write_all(req)?;
let n = stream.read(&mut buf)?;
if n == 0 {
return Err(Error::new(ErrorKind::Other, "read() returns Ok(0)"));
}
let recved = &buf[0..n];
println!("[{}] recv: {recved:?}", now());
std::thread::sleep(Duration::from_secs(30));
}
}
fn now() -> DateTime<Local> {
Local::now()
}
车架主机无线驱动问题排查
客户端主机硬件平台:iMX6UL;客户端主机软件平台:嵌入式Linux。Shelf99是运行在其中的一个小软件,以虚拟精简客户端的身份与系统服务器通讯。正式客户端软件是合作方开发的QT程序,在排查之前并不能排除或确认是否有BUG。事后证明应用层没有BUG,BUG在系统层。
20230925:经过本轮测试,发现车架主机WiFi连接掉线时和后续恢复时,程序阻塞在Linux系统调用read()函数内部,均不能及时返回错误码。由此造成的问题是,一旦主机内客户端TCP断开就需要等很长时间才能重新连上。结论是怀疑无线网卡驱动不完善所致。
详情:因WiFi信号不稳定,TCP客户端报文不能及时到达服务端,服务端识别到客户端心跳超时后将连接断开了,可客户端并没有及时检测到,仍然阻塞在read函数中,过了很久(大约十几分钟二十分钟),read函数居然返回了,错误信息是 Os { code: 113, kind: HostUnreachable, message: “No route to host” },然后客户端按照它的逻辑又再次重连又正常通讯(具备断线重连功能)。可问题是,在那长期阻塞的时间里,网络早已恢复正常,同一主机另外新开的客户端早就正常通讯了。
[2023-09-25 11:32:33.031] writing...
[2023-09-25 11:32:33.032] reading...
[2023-09-25 11:32:33.036] recv: [81, 78, 0, 4, 240, 243, 255, 128, 0, 69]
[2023-09-25 11:32:33.037] sleeping 30s...
[2023-09-25 11:33:03.038] writing...
[2023-09-25 11:33:03.039] reading...
111
[2023-09-25 11:50:19.473] work failed: Os { code: 113, kind: HostUnreachable, message: "No route to host" }
[2023-09-25 11:50:49.475] connecting 172.17.200.251:9696...
[2023-09-25 11:50:49.490] writing...
[2023-09-25 11:50:49.491] reading...
[2023-09-25 11:50:49.499] recv: [81, 78, 0, 4, 240, 243, 255, 128, 0, 69]
[2023-09-25 11:50:49.500] sleeping 30s...
[2023-09-25 11:51:19.501] writing...
[2023-09-25 11:51:19.502] reading...
[2023-09-25 11:51:19.574] recv: [81, 78, 0, 4, 240, 243, 255, 128, 0, 69]
[2023-09-25 11:51:19.575] sleeping 30s...