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