Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ice): always use latest timing config #568

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/ice/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl IceAgent {
let prio =
CandidatePair::calculate_prio(self.controlling, remote.prio(), local.prio());
let mut pair =
CandidatePair::new(*local_idx, *remote_idx, prio, self.timing_config);
CandidatePair::new(*local_idx, *remote_idx, prio, &self.timing_config);

trace!("Form pair local: {:?} remote: {:?}", local, remote);

Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl IceAgent {
let keep = if self.ice_lite {
p.has_recent_remote_binding_request(now)
} else {
p.is_still_possible(now)
p.is_still_possible(now, &self.timing_config)
};
if !keep {
debug!("Remove failed pair: {:?}", p);
Expand Down Expand Up @@ -1060,7 +1060,7 @@ impl IceAgent {
.candidate_pairs
.iter_mut()
.enumerate()
.map(|(i, c)| (i, c.next_binding_attempt(now)))
.map(|(i, c)| (i, c.next_binding_attempt(now, &self.timing_config)))
.min_by_key(|(_, t)| *t);

if let Some((idx, deadline)) = next {
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl IceAgent {
} else {
self.candidate_pairs
.iter_mut()
.map(|c| c.next_binding_attempt(last_now))
.map(|c| c.next_binding_attempt(last_now, &self.timing_config))
.min()
};

Expand Down Expand Up @@ -1347,7 +1347,7 @@ impl IceAgent {
// * Its state is set to Waiting. (this is the default)
// * The pair is inserted into the checklist based on its priority.
// * The pair is enqueued into the triggered-check queue.
let pair = CandidatePair::new(local_idx, remote_idx, prio, self.timing_config);
let pair = CandidatePair::new(local_idx, remote_idx, prio, &self.timing_config);

debug!("Created new pair for STUN request: {:?}", pair);

Expand Down Expand Up @@ -1421,7 +1421,7 @@ impl IceAgent {
// Only the controlling side sends USE-CANDIDATE.
let use_candidate = self.controlling && pair.is_nominated();

let trans_id = pair.new_attempt(now);
let trans_id = pair.new_attempt(now, &self.timing_config);

self.stats.bind_request_sent += 1;

Expand Down Expand Up @@ -1637,7 +1637,7 @@ impl IceAgent {
for p in &self.candidate_pairs {
if p.is_nominated() {
any_nomination = true;
} else if p.is_still_possible(now) {
} else if p.is_still_possible(now, &self.timing_config) {
any_still_possible = true;
}
}
Expand Down
25 changes: 11 additions & 14 deletions src/ice/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ pub struct CandidatePair {

/// State of nomination for this candidate pair.
nomination_state: NominationState,

timing_config: StunTiming,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -114,13 +112,12 @@ impl Default for PairId {
}

impl CandidatePair {
pub fn new(local_idx: usize, remote_idx: usize, prio: u64, timing_config: StunTiming) -> Self {
pub fn new(local_idx: usize, remote_idx: usize, prio: u64, timing_config: &StunTiming) -> Self {
CandidatePair {
local_idx,
remote_idx,
prio,
binding_attempts: VecDeque::with_capacity(timing_config.max_retransmits() + 1),
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
timing_config,
id: Default::default(),
valid_idx: Default::default(),
state: Default::default(),
Expand Down Expand Up @@ -221,7 +218,7 @@ impl CandidatePair {
/// Records a new binding request attempt.
///
/// Returns the transaction id to use in the STUN message.
pub fn new_attempt(&mut self, now: Instant) -> TransId {
pub fn new_attempt(&mut self, now: Instant, timing_config: &StunTiming) -> TransId {
// calculate a new time
self.cached_next_attempt_time = None;

Expand All @@ -240,7 +237,7 @@ impl CandidatePair {
self.binding_attempts.push_back(attempt);

// Never keep more than STUN_MAX_RETRANS attempts.
while self.binding_attempts.len() > self.timing_config.max_retransmits() {
while self.binding_attempts.len() > timing_config.max_retransmits() {
self.binding_attempts.pop_front();
}

Expand Down Expand Up @@ -322,7 +319,7 @@ impl CandidatePair {
/// When we should do the next retry.
///
/// Returns `None` if we are not to attempt this pair anymore.
pub fn next_binding_attempt(&mut self, now: Instant) -> Instant {
pub fn next_binding_attempt(&mut self, now: Instant, timing_config: &StunTiming) -> Instant {
if let Some(cached) = self.cached_next_attempt_time {
return cached;
}
Expand All @@ -335,19 +332,19 @@ impl CandidatePair {
// checking more often.
let unanswered_count = self
.unanswered()
.filter(|(_, since)| now - *since > self.timing_config.max_rto() / 2)
.filter(|(_, since)| now - *since > timing_config.max_rto() / 2)
.map(|(count, _)| count);

let send_count = unanswered_count.unwrap_or(self.binding_attempts.len());

last + self.timing_config.stun_resend_delay(send_count)
last + timing_config.stun_resend_delay(send_count)
} else {
// No previous attempt, do next retry straight away.
now
};

// At least do a check at this time.
let min = now + self.timing_config.max_rto();
let min = now + timing_config.max_rto();

let at_least = next.min(min);

Expand All @@ -360,19 +357,19 @@ impl CandidatePair {
/// Tells if this candidate pair is still possible to use for connectivity.
///
/// Returns `false` if the candidate has failed.
pub fn is_still_possible(&self, now: Instant) -> bool {
pub fn is_still_possible(&self, now: Instant, timing_config: &StunTiming) -> bool {
let attempts = self.binding_attempts.len();
let unanswered = self.unanswered().map(|b| b.0).unwrap_or(0);

if attempts < self.timing_config.max_retransmits()
|| unanswered < self.timing_config.max_retransmits()
if attempts < timing_config.max_retransmits()
|| unanswered < timing_config.max_retransmits()
{
true
} else {
// check to see if we are still waiting for the last attempt
// this unwrap is fine because unanswered count > 0
let last = self.last_attempt_time().unwrap();
let cutoff = last + self.timing_config.stun_last_resend_delay();
let cutoff = last + timing_config.stun_last_resend_delay();
now < cutoff
}
}
Expand Down
Loading