@@ -33,25 +33,34 @@ async fn broadcast_chunks(
3333 let mut index = [ 0 ; 32 ] ;
3434
3535 loop {
36- while let Ok ( hash) = rx. try_recv ( ) {
37- queue. insert ( hash) ;
38- }
36+ let get_index = async {
37+ while let Ok ( hash) = rx. try_recv ( ) {
38+ queue. insert ( hash) ;
39+ }
40+
41+ let hash = queue
42+ . range ( ( Bound :: Excluded ( index) , Bound :: Unbounded ) )
43+ . next ( )
44+ . or_else ( || queue. iter ( ) . next ( ) )
45+ . copied ( ) ;
3946
40- let hash = queue
41- . range ( ( Bound :: Excluded ( index) , Bound :: Unbounded ) )
42- . next ( )
43- . or_else ( || queue. iter ( ) . next ( ) )
44- . copied ( ) ;
47+ match hash {
48+ Some ( hash) => {
49+ queue. remove ( & hash) ;
50+ Some ( hash)
51+ }
52+ None => rx. recv ( ) . await ,
53+ }
54+ } ;
4555
46- index = match hash {
47- Some ( hash) => {
48- queue. remove ( & hash) ;
49- hash
56+ tokio:: select! {
57+ hash = get_index => {
58+ let Some ( hash) = hash else {
59+ break ;
60+ } ;
61+ index = hash;
5062 }
51- None => match rx. recv ( ) . await {
52- Some ( hash) => hash,
53- None => break ,
54- } ,
63+ _ = state. cancel_token. cancelled( ) => break ,
5564 } ;
5665
5766 let Some ( data) = state
@@ -162,6 +171,10 @@ fn compute_hint(state: &State) -> Result<RegistrationInfo> {
162171
163172async fn broadcast_hint ( state : & State , socket : & UdpSocket , ip : Ipv4Addr ) -> Result < ( ) > {
164173 loop {
174+ tokio:: select! {
175+ _ = time:: sleep( Duration :: from_secs( 1 ) ) => { }
176+ _ = state. cancel_token. cancelled( ) => break ,
177+ }
165178 let hint = HintPacket {
166179 station : compute_hint ( state) ?,
167180 images : state. config . images . clone ( ) ,
@@ -170,14 +183,17 @@ async fn broadcast_hint(state: &State, socket: &UdpSocket, ip: Ipv4Addr) -> Resu
170183 let data = postcard:: to_allocvec ( & hint) ?;
171184 let hint_addr = SocketAddrV4 :: new ( ip, HINT_PORT ) ;
172185 socket. send_to ( & data, hint_addr) . await ?;
173- time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
174186 }
187+ Ok ( ( ) )
175188}
176189
177190async fn handle_requests ( state : & State , socket : & UdpSocket , tx : Sender < [ u8 ; 32 ] > ) -> Result < ( ) > {
178191 let mut buf = [ 0 ; PACKET_LEN ] ;
179- ' recv_packet: loop {
180- let ( len, addr) = socket. recv_from ( & mut buf) . await ?;
192+ loop {
193+ let ( len, addr) = tokio:: select! {
194+ x = socket. recv_from( & mut buf) => x?,
195+ _ = state. cancel_token. cancelled( ) => break ,
196+ } ;
181197 let req: postcard:: Result < UdpRequest > = postcard:: from_bytes ( & buf[ ..len] ) ;
182198 match req {
183199 Ok ( UdpRequest :: Discover ) => {
@@ -187,14 +203,14 @@ async fn handle_requests(state: &State, socket: &UdpSocket, tx: Sender<[u8; 32]>
187203 let IpAddr :: V4 ( peer_ip) = addr. ip ( ) else {
188204 bail ! ( "IPv6 is not supported" )
189205 } ;
190- let peer_mac = match find_mac ( peer_ip) {
191- Ok ( peer_mac) => peer_mac,
206+ match find_mac ( peer_ip) {
207+ Ok ( peer_mac) => {
208+ state. set_unit_progress ( UnitSelector :: MacAddr ( peer_mac) , Some ( ( frac, tot) ) ) ;
209+ }
192210 Err ( err) => {
193211 log:: error!( "Error handling udp packet: {}" , err) ;
194- continue ' recv_packet;
195212 }
196213 } ;
197- state. set_unit_progress ( UnitSelector :: MacAddr ( peer_mac) , Some ( ( frac, tot) ) ) ;
198214 }
199215 Ok ( UdpRequest :: RequestChunks ( chunks) ) => {
200216 for hash in chunks {
@@ -206,6 +222,7 @@ async fn handle_requests(state: &State, socket: &UdpSocket, tx: Sender<[u8; 32]>
206222 }
207223 }
208224 }
225+ Ok ( ( ) )
209226}
210227
211228pub async fn main ( state : Arc < State > ) -> Result < ( ) > {
0 commit comments