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