Skip to content

Commit

Permalink
add poll_transmit for Candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 9, 2024
1 parent 3779d3c commit 8b1adff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions rtc-ice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ log = "0.4.21"
rand = "0.8.5"
serde = { version = "1.0.197", features = ["derive"] }
url = "2.5"
bytes = "1.5.0"

[dev-dependencies]
regex = "1"
Expand Down
6 changes: 3 additions & 3 deletions rtc-ice/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl Agent {

/// Returns the selected pair or none if there is none
pub fn get_selected_candidate_pair(&self) -> Option<usize> {
//TODO:
self.agent_conn.get_selected_pair()
}

Expand Down Expand Up @@ -973,9 +974,8 @@ impl Agent {
}

pub(crate) fn send_stun(&mut self, msg: &Message, local: usize, remote: usize) {
if let Err(err) =
self.local_candidates[local].write_to(&msg.raw, &*self.remote_candidates[remote])
{
let remote_addr = self.remote_candidates[remote].addr();
if let Err(err) = self.local_candidates[local].write(&msg.raw, remote_addr) {
log::trace!(
"[{}]: failed to send STUN message: {}",
self.get_name(),
Expand Down
27 changes: 19 additions & 8 deletions rtc-ice/src/candidate/candidate_base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use bytes::BytesMut;
use crc::{Crc, CRC_32_ISCSI};
use std::collections::VecDeque;
use std::fmt;
use std::time::Instant;

Expand All @@ -9,6 +11,7 @@ use crate::candidate::candidate_relay::CandidateRelayConfig;
use crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig;
use crate::network_type::determine_network_type;
use shared::error::*;
use stun::Transmit;

#[derive(Default)]
pub struct CandidateBaseConfig {
Expand All @@ -34,6 +37,7 @@ pub struct CandidateBase {
pub(crate) tcp_type: TcpType,

pub(crate) resolved_addr: SocketAddr,
pub(crate) transmits: VecDeque<Transmit>,

pub(crate) last_sent: Instant,
pub(crate) last_received: Instant,
Expand Down Expand Up @@ -62,11 +66,11 @@ impl Default for CandidateBase {
tcp_type: TcpType::default(),

resolved_addr: SocketAddr::new(IpAddr::from([0, 0, 0, 0]), 0),
transmits: VecDeque::new(),

last_sent: Instant::now(),
last_received: Instant::now(),

//todo: closed_ch: Arc::new(Mutex::new(None)),
foundation_override: String::new(),
priority_override: 0,
network: String::new(),
Expand Down Expand Up @@ -242,17 +246,24 @@ impl Candidate for CandidateBase {
}
}

fn write_to(&mut self, _raw: &[u8], _dst: &dyn Candidate) -> Result<usize> {
let n = /*TODO: if let Some(conn) = &self.conn {
let addr = dst.addr();
conn.send_to(raw, addr).await?
} else */{
0
};
fn write(&mut self, raw: &[u8], remote: SocketAddr) -> Result<usize> {
let n = raw.len();
self.transmits.push_back(Transmit {
now: Instant::now(),
remote,
ecn: None,
local_ip: Some(self.resolved_addr.ip()),
payload: BytesMut::from(raw),
});

self.seen(true);
Ok(n)
}

fn poll_transmit(&mut self) -> Option<Transmit> {
self.transmits.pop_front()
}

/// Used to compare two candidateBases.
fn equal(&self, other: &dyn Candidate) -> bool {
self.network_type() == other.network_type()
Expand Down
5 changes: 4 additions & 1 deletion rtc-ice/src/candidate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use shared::error::*;
use std::fmt;
use std::net::{IpAddr, SocketAddr};
use std::time::Instant;
use stun::Transmit;

pub(crate) const RECEIVE_MTU: usize = 8192;
pub(crate) const DEFAULT_LOCAL_PREFERENCE: u16 = 65535;
Expand Down Expand Up @@ -75,7 +76,9 @@ pub trait Candidate: fmt::Display {
fn close(&self) -> Result<()>;
fn seen(&mut self, outbound: bool);

fn write_to(&mut self, raw: &[u8], dst: &dyn Candidate) -> Result<usize>;
fn write(&mut self, raw: &[u8], remote_addr: SocketAddr) -> Result<usize>;
fn poll_transmit(&mut self) -> Option<Transmit>;

fn equal(&self, other: &dyn Candidate) -> bool;
fn set_ip(&mut self, ip: &IpAddr) -> Result<()>;
}
Expand Down

0 comments on commit 8b1adff

Please sign in to comment.