| ... | ... | @@ -555,13 +555,19 @@ impl Shared { |
| 555 | 555 | let shared = Arc::downgrade(self); |
| 556 | 556 | let dialing = thread::spawn(move || { |
| 557 | 557 | let mut wait = Duration::from_secs(1); |
| 558 | let ours = |state: &State| { |
| 559 | (state.dialing.get(&address)) |
| 560 | .is_some_and(|dial| dial.id() == thread::current().id()) |
| 561 | }; |
| 558 | 562 | loop { |
| 559 | 563 | let Some(live) = shared.upgrade() else { |
| 560 | 564 | return; |
| 561 | 565 | }; |
| 562 | | if live.stopped.load(Ordering::Acquire) || live.state.lock().unwrap().burned { |
| 566 | let state = live.state.lock().unwrap(); |
| 567 | if live.stopped.load(Ordering::Acquire) || state.burned || !ours(&state) { |
| 563 | 568 | break; |
| 564 | 569 | } |
| 570 | drop(state); |
| 565 | 571 | let Some(tag) = live.tag() else { |
| 566 | 572 | break; |
| 567 | 573 | }; |
| ... | ... | @@ -582,12 +588,22 @@ impl Shared { |
| 582 | 588 | wait = (wait * 2).min(PATIENCE); |
| 583 | 589 | } |
| 584 | 590 | if let Some(live) = shared.upgrade() { |
| 585 | | live.state.lock().unwrap().dialing.remove(&address); |
| 591 | let mut state = live.state.lock().unwrap(); |
| 592 | if ours(&state) { |
| 593 | state.dialing.remove(&address); |
| 594 | } |
| 586 | 595 | } |
| 587 | 596 | }); |
| 588 | 597 | state.dialing.insert(address, dialing.thread().clone()); |
| 589 | 598 | } |
| 590 | 599 | |
| 600 | /// Stops dialling `address` once its connection, if any, ends. |
| 601 | fn undial(&self, address: SocketAddr) { |
| 602 | if let Some(dial) = self.state.lock().unwrap().dialing.remove(&address) { |
| 603 | dial.unpark(); |
| 604 | } |
| 605 | } |
| 606 | |
| 591 | 607 | /// Counts a meeting that failed on the secret: the end sharing a code burns it after too |
| 592 | 608 | /// many, and an end that typed one gives up at once. |
| 593 | 609 | fn failed(self: &Arc<Self>) { |
| ... | ... | @@ -823,9 +839,20 @@ fn discover( |
| 823 | 839 | thread::Builder::new() |
| 824 | 840 | .name("live discovery".into()) |
| 825 | 841 | .spawn(move || { |
| 842 | // Where each peer found was dialled, to stop once it is no longer advertised. |
| 843 | let mut dialled = BTreeMap::new(); |
| 826 | 844 | while let Ok(event) = found.recv() { |
| 827 | | let ServiceEvent::ServiceResolved(service) = event else { |
| 828 | | continue; |
| 845 | let service = match event { |
| 846 | ServiceEvent::ServiceResolved(service) => service, |
| 847 | ServiceEvent::ServiceRemoved(_, name) => { |
| 848 | if let (Some(address), Some(shared)) = |
| 849 | (dialled.remove(&name), shared.upgrade()) |
| 850 | { |
| 851 | shared.undial(address); |
| 852 | } |
| 853 | continue; |
| 854 | } |
| 855 | _ => continue, |
| 829 | 856 | }; |
| 830 | 857 | let Some(shared) = shared.upgrade() else { |
| 831 | 858 | return; |
| ... | ... | @@ -844,6 +871,12 @@ fn discover( |
| 844 | 871 | addresses.sort_by_key(|ip| (!ip.is_ipv4(), !ip.is_loopback())); |
| 845 | 872 | if let Some(ip) = addresses.first() { |
| 846 | 873 | let address = SocketAddr::new(*ip, service.port); |
| 874 | // A peer found again at another address is no longer at the old one. |
| 875 | if let Some(old) = dialled.insert(service.fullname.clone(), address) |
| 876 | && old != address |
| 877 | { |
| 878 | shared.undial(old); |
| 879 | } |
| 847 | 880 | shared.dial(address); |
| 848 | 881 | } |
| 849 | 882 | } |