rtc_ice/candidate/
candidate_server_reflexive.rs

1use super::*;
2use crate::network_type::determine_network_type;
3use crate::rand::generate_cand_id;
4use shared::error::*;
5
6/// The config required to create a new `CandidateServerReflexive`.
7#[derive(Default)]
8pub struct CandidateServerReflexiveConfig {
9    pub base_config: CandidateConfig,
10
11    pub rel_addr: String,
12    pub rel_port: u16,
13
14    pub url: Option<String>,
15}
16
17impl CandidateServerReflexiveConfig {
18    /// Creates a new server reflective candidate.
19    pub fn new_candidate_server_reflexive(self) -> Result<Candidate> {
20        let mut candidate_id = self.base_config.candidate_id;
21        if candidate_id.is_empty() {
22            candidate_id = generate_cand_id();
23        }
24
25        let ip: IpAddr = match self.base_config.address.parse() {
26            Ok(ip) => ip,
27            Err(_) => return Err(Error::ErrAddressParseFailed),
28        };
29        let network_type = determine_network_type(&self.base_config.network, &ip)?;
30
31        Ok(Candidate {
32            id: candidate_id,
33            network_type,
34            candidate_type: CandidateType::ServerReflexive,
35            address: self.base_config.address,
36            port: self.base_config.port,
37            resolved_addr: SocketAddr::new(ip, self.base_config.port),
38            component: self.base_config.component,
39            foundation_override: self.base_config.foundation,
40            priority_override: self.base_config.priority,
41            related_address: Some(CandidateRelatedAddress {
42                address: self.rel_addr,
43                port: self.rel_port,
44            }),
45            url: self.url,
46            ..Candidate::default()
47        })
48    }
49}