rtci_ice/agent/
restart_undo.rs1use super::*;
5
6#[derive(Clone)]
10pub(crate) struct RestartUndo {
11 connection_state: ConnectionState,
12 local_credentials: Credentials,
13 remote_credentials: Option<Credentials>,
14 local_candidates: Vec<Candidate>,
15 remote_candidates: Vec<Candidate>,
16 candidate_pairs: Vec<CandidatePair>,
17 nominated_pair: Option<usize>,
18 selected_pair: Option<usize>,
19 previous_media_path: Option<MediaPath>,
20 previous_generation: Option<PreviousGeneration>,
21 pending_binding_requests: Vec<BindingRequest>,
22 early_requests: Vec<EarlyRequest>,
23 triggered_queue: VecDeque<(usize, usize)>,
24 remote_end_of_candidates: bool,
25 local_gathering_complete: bool,
26 lite_update_raised: bool,
27 lite_update_taken: bool,
28 next_check_at: Instant,
29 next_consent_at: Option<Instant>,
30 consent_unanswered_since: Option<Instant>,
31 checking_duration: Instant,
32 last_checking_time: Instant,
33 tie_breaker: u64,
34 ice_generation: u64,
35 mdns_known_public: KnownPublicAddresses,
36}
37
38impl Agent {
39 pub fn apply_restart_revertible(
45 &mut self,
46 now: Instant,
47 keep_local_candidates: bool,
48 ) -> Result<()> {
49 let undo = match self.restart_undo.take() {
50 Some(undo) => undo,
51 None => Box::new(RestartUndo {
52 connection_state: self.connection_state,
53 local_credentials: self.ufrag_pwd.local_credentials.clone(),
54 remote_credentials: self.ufrag_pwd.remote_credentials.clone(),
55 local_candidates: self.local_candidates.clone(),
56 remote_candidates: self.remote_candidates.clone(),
57 candidate_pairs: self.candidate_pairs.clone(),
58 nominated_pair: self.nominated_pair,
59 selected_pair: self.selected_pair,
60 previous_media_path: self.previous_media_path,
61 previous_generation: self.previous_generation.clone(),
62 pending_binding_requests: self.pending_binding_requests.clone(),
63 early_requests: self.early_requests.clone(),
64 triggered_queue: self.triggered_queue.clone(),
65 remote_end_of_candidates: self.remote_end_of_candidates,
66 local_gathering_complete: self.local_gathering_complete,
67 lite_update_raised: self.lite_update_raised,
68 lite_update_taken: self.lite_update_taken,
69 next_check_at: self.next_check_at,
70 next_consent_at: self.next_consent_at,
71 consent_unanswered_since: self.consent_unanswered_since,
72 checking_duration: self.checking_duration,
73 last_checking_time: self.last_checking_time,
74 tie_breaker: self.tie_breaker,
75 ice_generation: self.ice_generation,
76 mdns_known_public: self.mdns_known_public.clone(),
77 }),
78 };
79 self.apply_restart(now, keep_local_candidates)?;
80 self.restart_undo = Some(undo);
81 Ok(())
82 }
83
84 pub fn has_revertible_restart(&self) -> bool {
87 self.restart_undo.is_some()
88 }
89
90 pub fn commit_restart(&mut self) {
93 self.restart_undo = None;
94 }
95
96 pub fn revert_restart(&mut self, now: Instant) -> bool {
103 let Some(undo) = self.restart_undo.take() else {
104 return false;
105 };
106 if self.connection_state == ConnectionState::Closed {
107 return false;
108 }
109 let undo = *undo;
110 let kept_consent = self
112 .previous_generation
113 .as_ref()
114 .map(|previous| (previous.last_consent, previous.next_consent_at));
115
116 self.announce_all_pairs_removed();
117 self.ufrag_pwd.local_credentials = undo.local_credentials;
118 self.ufrag_pwd.remote_credentials = undo.remote_credentials;
119 self.ufrag_pwd.pending_local_credentials = None;
120 self.local_candidates = undo.local_candidates;
121 for c in &mut self.local_candidates {
124 c.restart_generation = false;
125 }
126 self.remote_candidates = undo.remote_candidates;
127 self.candidate_pairs = undo.candidate_pairs;
128 self.nominated_pair = undo.nominated_pair;
129 self.selected_pair = undo.selected_pair;
130 self.previous_media_path = undo.previous_media_path;
131 self.previous_generation = undo.previous_generation;
132 self.pending_binding_requests = undo.pending_binding_requests;
133 self.early_requests = undo.early_requests;
134 self.triggered_queue = undo.triggered_queue;
135 self.remote_end_of_candidates = undo.remote_end_of_candidates;
136 self.ice_generation = undo.ice_generation;
138 self.mdns_known_public = undo.mdns_known_public;
139 self.local_gathering_complete = undo.local_gathering_complete;
140 self.lite_update_raised = undo.lite_update_raised;
141 self.lite_update_taken = undo.lite_update_taken;
142 self.next_check_at = undo.next_check_at.max(now);
143 self.next_consent_at = undo.next_consent_at;
144 self.consent_unanswered_since = undo.consent_unanswered_since;
145 self.checking_duration = undo.checking_duration;
146 self.last_checking_time = undo.last_checking_time;
147 self.tie_breaker = undo.tie_breaker;
148
149 if let (Some(pair_index), Some((last_consent, next_consent_at))) =
150 (self.selected_pair, kept_consent)
151 {
152 let remote_index = self.candidate_pairs[pair_index].remote_index;
153 let remote = &mut self.remote_candidates[remote_index];
154 if self.lite {
155 if remote.last_authenticated.is_none_or(|t| t < last_consent) {
156 remote.last_authenticated = Some(last_consent);
157 }
158 } else {
159 if remote.last_consent.is_none_or(|t| t < last_consent) {
160 remote.last_consent = Some(last_consent);
161 }
162 let pair = &mut self.candidate_pairs[pair_index];
163 if pair.last_consent.is_none_or(|t| t < last_consent) {
164 pair.last_consent = Some(last_consent);
165 }
166 }
167 self.next_consent_at = Some(next_consent_at);
168 }
169
170 for index in 0..self.candidate_pairs.len() {
171 self.announce_pair(index, true);
172 }
173 if let Some(pair_index) = self.selected_pair {
174 let pair = self.candidate_pairs[pair_index];
175 self.event_outs.push_back(TaggedEvent {
176 now,
177 event: Event::SelectedCandidatePairChange(
178 Box::new(self.local_candidates[pair.local_index].clone()),
179 Box::new(self.remote_candidates[pair.remote_index].clone()),
180 ),
181 });
182 }
183 self.update_connection_state(Some(now), undo.connection_state);
184 true
185 }
186}