OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // factory. It gathers a single loopback port, using IPv6 if available and | 87 // factory. It gathers a single loopback port, using IPv6 if available and |
88 // not disabled. | 88 // not disabled. |
89 class FakePortAllocatorSession : public PortAllocatorSession { | 89 class FakePortAllocatorSession : public PortAllocatorSession { |
90 public: | 90 public: |
91 FakePortAllocatorSession(PortAllocator* allocator, | 91 FakePortAllocatorSession(PortAllocator* allocator, |
92 rtc::Thread* network_thread, | 92 rtc::Thread* network_thread, |
93 rtc::PacketSocketFactory* factory, | 93 rtc::PacketSocketFactory* factory, |
94 const std::string& content_name, | 94 const std::string& content_name, |
95 int component, | 95 int component, |
96 const std::string& ice_ufrag, | 96 const std::string& ice_ufrag, |
97 const std::string& ice_pwd, | 97 const std::string& ice_pwd) |
98 bool ipv6_enabled) | |
99 : PortAllocatorSession(content_name, | 98 : PortAllocatorSession(content_name, |
100 component, | 99 component, |
101 ice_ufrag, | 100 ice_ufrag, |
102 ice_pwd, | 101 ice_pwd, |
103 allocator->flags()), | 102 allocator->flags()), |
104 network_thread_(network_thread), | 103 network_thread_(network_thread), |
105 factory_(factory), | 104 factory_(factory), |
106 ipv4_network_("network", | 105 ipv4_network_("network", |
107 "unittest", | 106 "unittest", |
108 rtc::IPAddress(INADDR_LOOPBACK), | 107 rtc::IPAddress(INADDR_LOOPBACK), |
109 32), | 108 32), |
110 ipv6_network_("network", | 109 ipv6_network_("network", |
111 "unittest", | 110 "unittest", |
112 rtc::IPAddress(in6addr_loopback), | 111 rtc::IPAddress(in6addr_loopback), |
113 64), | 112 64), |
| 113 port_(), |
114 port_config_count_(0), | 114 port_config_count_(0), |
115 stun_servers_(allocator->stun_servers()), | 115 stun_servers_(allocator->stun_servers()), |
116 turn_servers_(allocator->turn_servers()), | 116 turn_servers_(allocator->turn_servers()) { |
117 ipv6_enabled_(ipv6_enabled) { | |
118 ipv4_network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK)); | 117 ipv4_network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK)); |
119 ipv6_network_.AddIP(rtc::IPAddress(in6addr_loopback)); | 118 ipv6_network_.AddIP(rtc::IPAddress(in6addr_loopback)); |
120 } | 119 } |
121 | 120 |
122 void SetCandidateFilter(uint32_t filter) override { | 121 void SetCandidateFilter(uint32_t filter) override { |
123 candidate_filter_ = filter; | 122 candidate_filter_ = filter; |
124 } | 123 } |
125 | 124 |
126 Port* CreatePort(rtc::Network* network) { | |
127 Port* port = TestUDPPort::Create(network_thread_, factory_, network, | |
128 network->GetBestIP(), 0, 0, username(), | |
129 password(), std::string(), false); | |
130 AddPort(port); | |
131 return port; | |
132 } | |
133 | |
134 void StartGettingPorts() override { | 125 void StartGettingPorts() override { |
135 if (!ipv4_port_) { | 126 if (!port_) { |
136 ipv4_port_.reset(CreatePort(&ipv4_network_)); | 127 rtc::Network& network = |
137 } | 128 (rtc::HasIPv6Enabled() && (flags() & PORTALLOCATOR_ENABLE_IPV6)) |
138 if (!ipv6_port_ && ipv6_enabled_ && (flags() & PORTALLOCATOR_ENABLE_IPV6)) { | 129 ? ipv6_network_ |
139 ipv6_port_.reset(CreatePort(&ipv6_network_)); | 130 : ipv4_network_; |
| 131 port_.reset(TestUDPPort::Create(network_thread_, factory_, &network, |
| 132 network.GetBestIP(), 0, 0, username(), |
| 133 password(), std::string(), false)); |
| 134 port_->SignalDestroyed.connect( |
| 135 this, &FakePortAllocatorSession::OnPortDestroyed); |
| 136 AddPort(port_.get()); |
140 } | 137 } |
141 ++port_config_count_; | 138 ++port_config_count_; |
142 running_ = true; | 139 running_ = true; |
143 } | 140 } |
144 | 141 |
145 void StopGettingPorts() override { running_ = false; } | 142 void StopGettingPorts() override { running_ = false; } |
146 bool IsGettingPorts() override { return running_; } | 143 bool IsGettingPorts() override { return running_; } |
147 void ClearGettingPorts() override {} | 144 void ClearGettingPorts() override {} |
148 | 145 |
149 std::vector<PortInterface*> ReadyPorts() const override { | 146 std::vector<PortInterface*> ReadyPorts() const override { |
150 return ready_ports_; | 147 return ready_ports_; |
151 } | 148 } |
152 std::vector<Candidate> ReadyCandidates() const override { | 149 std::vector<Candidate> ReadyCandidates() const override { |
153 return candidates_; | 150 return candidates_; |
154 } | 151 } |
155 void PruneAllPorts() override { | 152 void PruneAllPorts() override { port_->Prune(); } |
156 if (ipv4_port_) { | |
157 ipv4_port_->Prune(); | |
158 } | |
159 if (ipv6_port_) { | |
160 ipv6_port_->Prune(); | |
161 } | |
162 } | |
163 bool CandidatesAllocationDone() const override { return allocation_done_; } | 153 bool CandidatesAllocationDone() const override { return allocation_done_; } |
164 | 154 |
165 int port_config_count() { return port_config_count_; } | 155 int port_config_count() { return port_config_count_; } |
166 | 156 |
167 const ServerAddresses& stun_servers() const { return stun_servers_; } | 157 const ServerAddresses& stun_servers() const { return stun_servers_; } |
168 | 158 |
169 const std::vector<RelayServerConfig>& turn_servers() const { | 159 const std::vector<RelayServerConfig>& turn_servers() const { |
170 return turn_servers_; | 160 return turn_servers_; |
171 } | 161 } |
172 | 162 |
173 uint32_t candidate_filter() const { return candidate_filter_; } | 163 uint32_t candidate_filter() const { return candidate_filter_; } |
174 | 164 |
175 int transport_info_update_count() const { | 165 int transport_info_update_count() const { |
176 return transport_info_update_count_; | 166 return transport_info_update_count_; |
177 } | 167 } |
178 | 168 |
179 protected: | 169 protected: |
180 void UpdateIceParametersInternal() override { | 170 void UpdateIceParametersInternal() override { |
181 // Since this class is a fake and this method only is overridden for tests, | 171 // Since this class is a fake and this method only is overridden for tests, |
182 // we don't need to actually update the transport info. | 172 // we don't need to actually update the transport info. |
183 ++transport_info_update_count_; | 173 ++transport_info_update_count_; |
184 } | 174 } |
185 | 175 |
186 private: | 176 private: |
187 void AddPort(cricket::Port* port) { | 177 void AddPort(cricket::Port* port) { |
188 port->set_component(component()); | 178 port->set_component(component()); |
189 port->set_generation(generation()); | 179 port->set_generation(generation()); |
190 port->SignalPortComplete.connect(this, | 180 port->SignalPortComplete.connect(this, |
191 &FakePortAllocatorSession::OnPortComplete); | 181 &FakePortAllocatorSession::OnPortComplete); |
192 port->SignalDestroyed.connect(this, | |
193 &FakePortAllocatorSession::OnPortDestroyed); | |
194 port->PrepareAddress(); | 182 port->PrepareAddress(); |
195 ready_ports_.push_back(port); | 183 ready_ports_.push_back(port); |
196 SignalPortReady(this, port); | 184 SignalPortReady(this, port); |
197 port->KeepAliveUntilPruned(); | 185 port->KeepAliveUntilPruned(); |
198 } | 186 } |
199 void OnPortComplete(cricket::Port* port) { | 187 void OnPortComplete(cricket::Port* port) { |
200 const std::vector<Candidate>& candidates = port->Candidates(); | 188 const std::vector<Candidate>& candidates = port->Candidates(); |
201 candidates_.insert(candidates_.end(), candidates.begin(), candidates.end()); | 189 candidates_.insert(candidates_.end(), candidates.begin(), candidates.end()); |
202 SignalCandidatesReady(this, candidates); | 190 SignalCandidatesReady(this, candidates); |
203 | 191 |
204 allocation_done_ = true; | 192 allocation_done_ = true; |
205 SignalCandidatesAllocationDone(this); | 193 SignalCandidatesAllocationDone(this); |
206 } | 194 } |
207 void OnPortDestroyed(cricket::PortInterface* port) { | 195 void OnPortDestroyed(cricket::PortInterface* port) { |
208 // Don't want to double-delete port if it deletes itself. | 196 // Don't want to double-delete port if it deletes itself. |
209 if (port == ipv4_port_.get()) { | 197 port_.release(); |
210 ipv4_port_.release(); | |
211 } else if (port == ipv6_port_.get()) { | |
212 ipv6_port_.release(); | |
213 } | |
214 } | 198 } |
215 | 199 |
216 rtc::Thread* network_thread_; | 200 rtc::Thread* network_thread_; |
217 rtc::PacketSocketFactory* factory_; | 201 rtc::PacketSocketFactory* factory_; |
218 rtc::Network ipv4_network_; | 202 rtc::Network ipv4_network_; |
219 rtc::Network ipv6_network_; | 203 rtc::Network ipv6_network_; |
220 std::unique_ptr<cricket::Port> ipv4_port_; | 204 std::unique_ptr<cricket::Port> port_; |
221 std::unique_ptr<cricket::Port> ipv6_port_; | |
222 int port_config_count_; | 205 int port_config_count_; |
223 std::vector<Candidate> candidates_; | 206 std::vector<Candidate> candidates_; |
224 std::vector<PortInterface*> ready_ports_; | 207 std::vector<PortInterface*> ready_ports_; |
225 bool allocation_done_ = false; | 208 bool allocation_done_ = false; |
226 ServerAddresses stun_servers_; | 209 ServerAddresses stun_servers_; |
227 std::vector<RelayServerConfig> turn_servers_; | 210 std::vector<RelayServerConfig> turn_servers_; |
228 uint32_t candidate_filter_ = CF_ALL; | 211 uint32_t candidate_filter_ = CF_ALL; |
229 int transport_info_update_count_ = 0; | 212 int transport_info_update_count_ = 0; |
230 bool ipv6_enabled_; | |
231 bool running_ = false; | 213 bool running_ = false; |
232 }; | 214 }; |
233 | 215 |
234 class FakePortAllocator : public cricket::PortAllocator { | 216 class FakePortAllocator : public cricket::PortAllocator { |
235 public: | 217 public: |
236 FakePortAllocator(rtc::Thread* network_thread, | 218 FakePortAllocator(rtc::Thread* network_thread, |
237 rtc::PacketSocketFactory* factory) | 219 rtc::PacketSocketFactory* factory) |
238 : network_thread_(network_thread), factory_(factory) { | 220 : network_thread_(network_thread), factory_(factory) { |
239 if (factory_ == NULL) { | 221 if (factory_ == NULL) { |
240 owned_factory_.reset(new rtc::BasicPacketSocketFactory(network_thread_)); | 222 owned_factory_.reset(new rtc::BasicPacketSocketFactory(network_thread_)); |
241 factory_ = owned_factory_.get(); | 223 factory_ = owned_factory_.get(); |
242 } | 224 } |
243 ipv6_enabled_ = rtc::HasIPv6Enabled(); | |
244 } | 225 } |
245 | 226 |
246 void Initialize() override { | 227 void Initialize() override { |
247 // Port allocator should be initialized on the network thread. | 228 // Port allocator should be initialized on the network thread. |
248 RTC_CHECK(network_thread_->IsCurrent()); | 229 RTC_CHECK(network_thread_->IsCurrent()); |
249 initialized_ = true; | 230 initialized_ = true; |
250 } | 231 } |
251 | 232 |
252 void SetNetworkIgnoreMask(int network_ignore_mask) override {} | 233 void SetNetworkIgnoreMask(int network_ignore_mask) override {} |
253 | 234 |
254 // Sometimes we can ignore the value returned by rtc::HasIpv6Enabled because | |
255 // we are using the virtual socket server. | |
256 void set_ipv6_enabled(bool ipv6_enabled) { ipv6_enabled_ = ipv6_enabled; } | |
257 | |
258 cricket::PortAllocatorSession* CreateSessionInternal( | 235 cricket::PortAllocatorSession* CreateSessionInternal( |
259 const std::string& content_name, | 236 const std::string& content_name, |
260 int component, | 237 int component, |
261 const std::string& ice_ufrag, | 238 const std::string& ice_ufrag, |
262 const std::string& ice_pwd) override { | 239 const std::string& ice_pwd) override { |
263 return new FakePortAllocatorSession(this, network_thread_, factory_, | 240 return new FakePortAllocatorSession(this, network_thread_, factory_, |
264 content_name, component, ice_ufrag, | 241 content_name, component, ice_ufrag, |
265 ice_pwd, ipv6_enabled_); | 242 ice_pwd); |
266 } | 243 } |
267 | 244 |
268 bool initialized() const { return initialized_; } | 245 bool initialized() const { return initialized_; } |
269 | 246 |
270 private: | 247 private: |
271 rtc::Thread* network_thread_; | 248 rtc::Thread* network_thread_; |
272 rtc::PacketSocketFactory* factory_; | 249 rtc::PacketSocketFactory* factory_; |
273 std::unique_ptr<rtc::BasicPacketSocketFactory> owned_factory_; | 250 std::unique_ptr<rtc::BasicPacketSocketFactory> owned_factory_; |
274 bool initialized_ = false; | 251 bool initialized_ = false; |
275 bool ipv6_enabled_ = false; | |
276 }; | 252 }; |
277 | 253 |
278 } // namespace cricket | 254 } // namespace cricket |
279 | 255 |
280 #endif // WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ | 256 #endif // WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ |
OLD | NEW |