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
15impl CandidateServerReflexiveConfig {
16    /// Creates a new server reflective candidate.
17    pub fn new_candidate_server_reflexive(self) -> Result<Candidate> {
18        let mut candidate_id = self.base_config.candidate_id;
19        if candidate_id.is_empty() {
20            candidate_id = generate_cand_id();
21        }
22
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        Ok(Candidate {
30            id: candidate_id,
31            network_type,
32            candidate_type: CandidateType::ServerReflexive,
33            address: self.base_config.address,
34            port: self.base_config.port,
35            resolved_addr: SocketAddr::new(ip, self.base_config.port),
36            component: self.base_config.component,
37            foundation_override: self.base_config.foundation,
38            priority_override: self.base_config.priority,
39            related_address: Some(CandidateRelatedAddress {
40                address: self.rel_addr,
41                port: self.rel_port,
42            }),
43            ..Candidate::default()
44        })
45    }
46}