webrtc_ice/candidate/
candidate_peer_reflexive.rs

1use portable_atomic::{AtomicU16, AtomicU8};
2
3use util::sync::Mutex as SyncMutex;
4
5use super::candidate_base::*;
6use super::*;
7use crate::error::*;
8use crate::rand::generate_cand_id;
9use crate::util::*;
10
11/// The config required to create a new `CandidatePeerReflexive`.
12#[derive(Default)]
13pub struct CandidatePeerReflexiveConfig {
14    pub base_config: CandidateBaseConfig,
15
16    pub rel_addr: String,
17    pub rel_port: u16,
18}
19
20impl CandidatePeerReflexiveConfig {
21    /// Creates a new peer reflective candidate.
22    pub fn new_candidate_peer_reflexive(self) -> Result<CandidateBase> {
23        let ip: IpAddr = match self.base_config.address.parse() {
24            Ok(ip) => ip,
25            Err(_) => return Err(Error::ErrAddressParseFailed),
26        };
27        let network_type = determine_network_type(&self.base_config.network, &ip)?;
28
29        let mut candidate_id = self.base_config.candidate_id;
30        if candidate_id.is_empty() {
31            candidate_id = generate_cand_id();
32        }
33
34        let c = CandidateBase {
35            id: candidate_id,
36            network_type: AtomicU8::new(network_type as u8),
37            candidate_type: CandidateType::PeerReflexive,
38            address: self.base_config.address,
39            port: self.base_config.port,
40            resolved_addr: SyncMutex::new(create_addr(network_type, ip, self.base_config.port)),
41            component: AtomicU16::new(self.base_config.component),
42            foundation_override: self.base_config.foundation,
43            priority_override: self.base_config.priority,
44            related_address: Some(CandidateRelatedAddress {
45                address: self.rel_addr,
46                port: self.rel_port,
47            }),
48            conn: self.base_config.conn,
49            ..CandidateBase::default()
50        };
51
52        Ok(c)
53    }
54}