fix udp on macOS

This commit is contained in:
Lutz Lang 2025-04-25 23:39:56 +02:00
parent bd300f163e
commit 0acb2a2538

View File

@ -303,12 +303,11 @@ fn send_package(ipaddress: String,
render_clock(&mut display); render_clock(&mut display);
package[1..PACKAGE_LENGTH].copy_from_slice(&display.image); package[1..PACKAGE_LENGTH].copy_from_slice(&display.image);
// client need to bind to client port (1 before 4242) let target = format!("{}:4242", ipaddress);
let socket = UdpSocket::bind("0.0.0.0:14242").expect("couldn't bind to address"); let socket = UdpSocket::bind("0.0.0.0:0").expect("couldn't bind to address");
socket socket
.send_to(&package, ipaddress + ":4242") .send_to(&package, &target)
.expect("couldn't send data"); .expect("couldn't send data");
} }
@ -323,30 +322,26 @@ LEDboardClient <ip address>"
fn check_connection(ipaddress: String) -> bool { fn check_connection(ipaddress: String) -> bool {
let device_online; let device_online;
// generate a faulty package length
let mut package: [u8; PACKAGE_LENGTH/2] = [0; PACKAGE_LENGTH/2]; let mut package: [u8; PACKAGE_LENGTH/2] = [0; PACKAGE_LENGTH/2];
// client need to bind to client port (1 before 4242)
let socket = UdpSocket::bind("0.0.0.0:14242").expect("couldn't bind to address");
socket.set_read_timeout(Some(Duration::from_secs(10))).unwrap(); /* 10 seconds timeout */
socket
.send_to(&package, ipaddress + ":4242")
.expect("couldn't send data");
// self.recv_buff is a [u8; 8092] // Use a random local port instead of hardcoding
let answer = socket.recv_from(&mut package); let socket = UdpSocket::bind("0.0.0.0:0").expect("couldn't bind to address");
match answer { socket.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
Ok((_n, _addr)) => {
//println!("{} bytes response from {:?} {:?}", n, addr, &package[..n]); let target = format!("{}:4242", ipaddress);
device_online = true; match socket.send_to(&package, &target) {
} Ok(_) => {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { // Continue with receive
device_online = false; match socket.recv_from(&mut package) {
} Ok((_n, _addr)) => device_online = true,
Err(_e) => { Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => device_online = false,
device_online = false; Err(_) => device_online = false,
} }
},
Err(_) => device_online = false,
} }
return device_online;
device_online
} }
/// Publishes weather and transit data to MQTT broker /// Publishes weather and transit data to MQTT broker
fn publish_to_mqtt(client: &Client, data: &Option<Result<Forecast, String>>, straba_res: &NextDeparture) { fn publish_to_mqtt(client: &Client, data: &Option<Result<Forecast, String>>, straba_res: &NextDeparture) {