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