Skip to content

Commit

Permalink
fix ConnectionState::Failed
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 11, 2024
1 parent 5c9d9cd commit 0ae15b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
6 changes: 5 additions & 1 deletion rtc-ice/examples/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ async fn main() -> Result<(), Error> {

let port = if cli.controlling { 4000 } else { 4001 };
let udp_socket = UdpSocket::bind(("0.0.0.0", port)).await?;
let mut ice_agent = Agent::new(AgentConfig::default())?;
let mut ice_agent = Agent::new(AgentConfig {
disconnected_timeout: Some(Duration::from_secs(5)),
failed_timeout: Some(Duration::from_secs(5)),
..Default::default()
})?;

let client = Arc::new(Client::new());

Expand Down
27 changes: 17 additions & 10 deletions rtc-ice/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct Agent {
pub(crate) keepalive_interval: Duration,
// How often should we run our internal taskLoop to check for state changes when connecting
pub(crate) check_interval: Duration,
pub(crate) checking_duration: Instant,
pub(crate) last_checking_time: Instant,

pub(crate) candidate_types: Vec<CandidateType>,
Expand Down Expand Up @@ -239,6 +240,7 @@ impl Agent {
} else {
config.check_interval
},
checking_duration: Instant::now(),
last_checking_time: Instant::now(),
last_connection_state: ConnectionState::Unspecified,

Expand Down Expand Up @@ -421,6 +423,7 @@ impl Agent {

/// Cleans up the Agent.
pub fn close(&mut self) -> Result<()> {
self.set_selected_pair(None);
self.delete_all_candidates(false);
self.update_connection_state(ConnectionState::Closed);

Expand Down Expand Up @@ -520,12 +523,12 @@ impl Agent {
if self.connection_state == ConnectionState::Checking {
// We have just entered checking for the first time so update our checking timer
if self.last_connection_state != self.connection_state {
self.last_checking_time = now;
self.checking_duration = now;
}

// We have been in checking longer then Disconnect+Failed timeout, set the connection to Failed
if now
.checked_duration_since(self.last_checking_time)
.checked_duration_since(self.checking_duration)
.unwrap_or_else(|| Duration::from_secs(0))
> self.disconnected_timeout + self.failed_timeout
{
Expand All @@ -545,6 +548,7 @@ impl Agent {
if self.connection_state != new_state {
// Connection has gone to failed, release all gathered candidates
if new_state == ConnectionState::Failed {
self.set_selected_pair(None);
self.delete_all_candidates(false);
}

Expand Down Expand Up @@ -661,13 +665,14 @@ impl Agent {
};

if valid {
// Only allow transitions to failed if a.failedTimeout is non-zero
if self.failed_timeout != Duration::from_secs(0) {
self.failed_timeout += self.disconnected_timeout;
// Only allow transitions to fail if a.failedTimeout is non-zero
let mut total_time_to_failure = self.failed_timeout;
if total_time_to_failure != Duration::from_secs(0) {
total_time_to_failure += self.disconnected_timeout;
}

if self.failed_timeout != Duration::from_secs(0)
&& disconnected_time > self.failed_timeout
if total_time_to_failure != Duration::from_secs(0)
&& disconnected_time > total_time_to_failure
{
self.update_connection_state(ConnectionState::Failed);
} else if self.disconnected_timeout != Duration::from_secs(0)
Expand Down Expand Up @@ -833,12 +838,14 @@ impl Agent {
}

*pending_binding_requests = temp;
let bind_requests_removed = initial_size - pending_binding_requests.len();
let bind_requests_remaining = pending_binding_requests.len();
let bind_requests_removed = initial_size - bind_requests_remaining;
if bind_requests_removed > 0 {
trace!(
"[{}]: Discarded {} binding requests because they expired",
"[{}]: Discarded {} binding requests because they expired, still {} remaining",
self.get_name(),
bind_requests_removed
bind_requests_removed,
bind_requests_remaining,
);
}
}
Expand Down

0 comments on commit 0ae15b1

Please sign in to comment.