eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4219991e1c635105fe7520085c2d5140f0ac6c30
parent fe81d1f617256b84d61934632dbfa2f075c025e8
Author: Daniel Barlow <dan@telent.net>
Date:   Fri,  2 Jan 2026 19:20:33 +0000

move probe failure handling up the call stack

instead of handling name collision inside MdnsResponder,  pass the
error back to the caller which is better placed to decide how to
change the records

Diffstat:
Mecu-esp32/src/wifi.rs | 67+++++++++++++++++++++++++++++++++++++------------------------------
1 file changed, 37 insertions(+), 30 deletions(-)

diff --git a/ecu-esp32/src/wifi.rs b/ecu-esp32/src/wifi.rs @@ -68,22 +68,21 @@ const CACHE_FLUSH : u16 = 1<<15; enum MdnsResponderState { Probing(Instant, usize), + ProbeFailed, Announcing(Instant, usize), Running } struct MdnsResponder<'a> { sock : UdpSocket<'a>, - serial: Option<u8>, database : DnsDatabase, - state : MdnsResponderState, - my_address: [u8;4] + state : MdnsResponderState } impl<'a> MdnsResponder<'a> { fn new(stack : Stack<'a>, mut sock: UdpSocket<'a>, - my_address: [u8;4]) -> MdnsResponder<'a> { + database: DnsDatabase) -> MdnsResponder<'a> { sock.set_hop_limit(Some(255)); log::info!("sock.bind(5353) res: {:?}", sock.bind(MDNS_PORT)); @@ -93,11 +92,9 @@ impl<'a> MdnsResponder<'a> { ); Self { - sock, - serial: None, - database: DnsDatabase::create(my_address, None), - state: MdnsResponderState::Probing(Instant::now(), 0), - my_address + sock: sock, + database, + state: MdnsResponderState::Probing(Instant::now(), 0) } } @@ -125,7 +122,8 @@ impl<'a> MdnsResponder<'a> { } } }, - MdnsResponderState::Running => () + MdnsResponderState::Running => (), + MdnsResponderState::ProbeFailed => (), } } @@ -180,7 +178,10 @@ impl<'a> MdnsResponder<'a> { async fn may_recv(&mut self) -> bool { self.advance_state().await; - self.sock.may_recv() + match self.state { + MdnsResponderState::ProbeFailed => false, + _ => self.sock.may_recv() + } } async fn send_response(&self, @@ -242,15 +243,6 @@ impl<'a> MdnsResponder<'a> { self.send_message(message, endpoint).await; } - pub fn increment_serial(&mut self) { - self.serial = match self.serial { - None => Some(2), - Some(n) => Some(n + 1) - }; - info!("name collision, incremented serial {:?}", self.serial); - self.database = DnsDatabase::create(self.my_address, self.serial); - } - async fn process_received_message(&mut self, message: &Message<'_,'_>, meta: &UdpMetadata) { @@ -262,13 +254,11 @@ impl<'a> MdnsResponder<'a> { if self.database.find(rr.name().to_string(), ResourceType::Wildcard) .iter().any(|record| record.is_unique()) { info!("probe failed, received answer for {:?}", rr.name()); - self.increment_serial(); - self.state = MdnsResponderState::Probing( - Instant::now() + Duration::from_millis(5000), 0 - ); + self.state = MdnsResponderState::ProbeFailed; } } }, + MdnsResponderState::ProbeFailed => (), _ => { let q_id = message.id(); @@ -596,14 +586,31 @@ pub async fn start_wifi(spawner: &Spawner, &mut tx_buffer, ); - let mut mdns_responder = MdnsResponder::new(stack, sock, my_address); + let mut serial = None; + let mut mdns_responder = MdnsResponder::new( + stack, sock, + DnsDatabase::create(my_address, serial) + ); loop { - if mdns_responder.may_recv().await { - mdns_responder.process_packet().await; - } - - Timer::after(Duration::from_millis(50)).await; + loop { + match mdns_responder.state { + MdnsResponderState::ProbeFailed => { + serial = match serial { None => Some(2), Some(n) => Some(n+1) }; + mdns_responder.database = DnsDatabase::create(my_address, serial); + mdns_responder.state = MdnsResponderState::Probing( + Instant::now() + Duration::from_millis(5000), + 0 + ); + }, + _ => () + }; + if mdns_responder.may_recv().await { + mdns_responder.process_packet().await; + } else { + Timer::after(Duration::from_millis(50)).await; + } + } } // _ = stack.leave_multicast_group(MDNS_IP);