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) |
98 : PortAllocatorSession(content_name, | 99 : PortAllocatorSession(content_name, |
99 component, | 100 component, |
100 ice_ufrag, | 101 ice_ufrag, |
101 ice_pwd, | 102 ice_pwd, |
102 allocator->flags()), | 103 allocator->flags()), |
103 network_thread_(network_thread), | 104 network_thread_(network_thread), |
104 factory_(factory), | 105 factory_(factory), |
105 ipv4_network_("network", | 106 ipv4_network_("network", |
106 "unittest", | 107 "unittest", |
107 rtc::IPAddress(INADDR_LOOPBACK), | 108 rtc::IPAddress(INADDR_LOOPBACK), |
108 32), | 109 32), |
109 ipv6_network_("network", | 110 ipv6_network_("network", |
110 "unittest", | 111 "unittest", |
111 rtc::IPAddress(in6addr_loopback), | 112 rtc::IPAddress(in6addr_loopback), |
112 64), | 113 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) { |
117 ipv4_network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK)); | 118 ipv4_network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK)); |
118 ipv6_network_.AddIP(rtc::IPAddress(in6addr_loopback)); | 119 ipv6_network_.AddIP(rtc::IPAddress(in6addr_loopback)); |
119 } | 120 } |
120 | 121 |
121 void SetCandidateFilter(uint32_t filter) override { | 122 void SetCandidateFilter(uint32_t filter) override { |
122 candidate_filter_ = filter; | 123 candidate_filter_ = filter; |
123 } | 124 } |
124 | 125 |
| 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 |
125 void StartGettingPorts() override { | 134 void StartGettingPorts() override { |
126 if (!port_) { | 135 if (!ipv4_port_) { |
127 rtc::Network& network = | 136 ipv4_port_.reset(CreatePort(&ipv4_network_)); |
128 (rtc::HasIPv6Enabled() && (flags() & PORTALLOCATOR_ENABLE_IPV6)) | 137 } |
129 ? ipv6_network_ | 138 if (!ipv6_port_ && ipv6_enabled_ && (flags() & PORTALLOCATOR_ENABLE_IPV6)) { |
130 : ipv4_network_; | 139 ipv6_port_.reset(CreatePort(&ipv6_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()); | |
137 } | 140 } |
138 ++port_config_count_; | 141 ++port_config_count_; |
139 running_ = true; | 142 running_ = true; |
140 } | 143 } |
141 | 144 |
142 void StopGettingPorts() override { running_ = false; } | 145 void StopGettingPorts() override { running_ = false; } |
143 bool IsGettingPorts() override { return running_; } | 146 bool IsGettingPorts() override { return running_; } |
144 void ClearGettingPorts() override {} | 147 void ClearGettingPorts() override {} |
145 | 148 |
146 std::vector<PortInterface*> ReadyPorts() const override { | 149 std::vector<PortInterface*> ReadyPorts() const override { |
147 return ready_ports_; | 150 return ready_ports_; |
148 } | 151 } |
149 std::vector<Candidate> ReadyCandidates() const override { | 152 std::vector<Candidate> ReadyCandidates() const override { |
150 return candidates_; | 153 return candidates_; |
151 } | 154 } |
152 void PruneAllPorts() override { port_->Prune(); } | 155 void PruneAllPorts() override { |
| 156 if (ipv4_port_) { |
| 157 ipv4_port_->Prune(); |
| 158 } |
| 159 if (ipv6_port_) { |
| 160 ipv6_port_->Prune(); |
| 161 } |
| 162 } |
153 bool CandidatesAllocationDone() const override { return allocation_done_; } | 163 bool CandidatesAllocationDone() const override { return allocation_done_; } |
154 | 164 |
155 int port_config_count() { return port_config_count_; } | 165 int port_config_count() { return port_config_count_; } |
156 | 166 |
157 const ServerAddresses& stun_servers() const { return stun_servers_; } | 167 const ServerAddresses& stun_servers() const { return stun_servers_; } |
158 | 168 |
159 const std::vector<RelayServerConfig>& turn_servers() const { | 169 const std::vector<RelayServerConfig>& turn_servers() const { |
160 return turn_servers_; | 170 return turn_servers_; |
161 } | 171 } |
162 | 172 |
163 uint32_t candidate_filter() const { return candidate_filter_; } | 173 uint32_t candidate_filter() const { return candidate_filter_; } |
164 | 174 |
165 int transport_info_update_count() const { | 175 int transport_info_update_count() const { |
166 return transport_info_update_count_; | 176 return transport_info_update_count_; |
167 } | 177 } |
168 | 178 |
169 protected: | 179 protected: |
170 void UpdateIceParametersInternal() override { | 180 void UpdateIceParametersInternal() override { |
171 // Since this class is a fake and this method only is overridden for tests, | 181 // Since this class is a fake and this method only is overridden for tests, |
172 // we don't need to actually update the transport info. | 182 // we don't need to actually update the transport info. |
173 ++transport_info_update_count_; | 183 ++transport_info_update_count_; |
174 } | 184 } |
175 | 185 |
176 private: | 186 private: |
177 void AddPort(cricket::Port* port) { | 187 void AddPort(cricket::Port* port) { |
178 port->set_component(component()); | 188 port->set_component(component()); |
179 port->set_generation(generation()); | 189 port->set_generation(generation()); |
180 port->SignalPortComplete.connect(this, | 190 port->SignalPortComplete.connect(this, |
181 &FakePortAllocatorSession::OnPortComplete); | 191 &FakePortAllocatorSession::OnPortComplete); |
| 192 port->SignalDestroyed.connect(this, |
| 193 &FakePortAllocatorSession::OnPortDestroyed); |
182 port->PrepareAddress(); | 194 port->PrepareAddress(); |
183 ready_ports_.push_back(port); | 195 ready_ports_.push_back(port); |
184 SignalPortReady(this, port); | 196 SignalPortReady(this, port); |
185 port->KeepAliveUntilPruned(); | 197 port->KeepAliveUntilPruned(); |
186 } | 198 } |
187 void OnPortComplete(cricket::Port* port) { | 199 void OnPortComplete(cricket::Port* port) { |
188 const std::vector<Candidate>& candidates = port->Candidates(); | 200 const std::vector<Candidate>& candidates = port->Candidates(); |
189 candidates_.insert(candidates_.end(), candidates.begin(), candidates.end()); | 201 candidates_.insert(candidates_.end(), candidates.begin(), candidates.end()); |
190 SignalCandidatesReady(this, candidates); | 202 SignalCandidatesReady(this, candidates); |
191 | 203 |
192 allocation_done_ = true; | 204 allocation_done_ = true; |
193 SignalCandidatesAllocationDone(this); | 205 SignalCandidatesAllocationDone(this); |
194 } | 206 } |
195 void OnPortDestroyed(cricket::PortInterface* port) { | 207 void OnPortDestroyed(cricket::PortInterface* port) { |
196 // Don't want to double-delete port if it deletes itself. | 208 // Don't want to double-delete port if it deletes itself. |
197 port_.release(); | 209 if (port == ipv4_port_.get()) { |
| 210 ipv4_port_.release(); |
| 211 } else if (port == ipv6_port_.get()) { |
| 212 ipv6_port_.release(); |
| 213 } |
198 } | 214 } |
199 | 215 |
200 rtc::Thread* network_thread_; | 216 rtc::Thread* network_thread_; |
201 rtc::PacketSocketFactory* factory_; | 217 rtc::PacketSocketFactory* factory_; |
202 rtc::Network ipv4_network_; | 218 rtc::Network ipv4_network_; |
203 rtc::Network ipv6_network_; | 219 rtc::Network ipv6_network_; |
204 std::unique_ptr<cricket::Port> port_; | 220 std::unique_ptr<cricket::Port> ipv4_port_; |
| 221 std::unique_ptr<cricket::Port> ipv6_port_; |
205 int port_config_count_; | 222 int port_config_count_; |
206 std::vector<Candidate> candidates_; | 223 std::vector<Candidate> candidates_; |
207 std::vector<PortInterface*> ready_ports_; | 224 std::vector<PortInterface*> ready_ports_; |
208 bool allocation_done_ = false; | 225 bool allocation_done_ = false; |
209 ServerAddresses stun_servers_; | 226 ServerAddresses stun_servers_; |
210 std::vector<RelayServerConfig> turn_servers_; | 227 std::vector<RelayServerConfig> turn_servers_; |
211 uint32_t candidate_filter_ = CF_ALL; | 228 uint32_t candidate_filter_ = CF_ALL; |
212 int transport_info_update_count_ = 0; | 229 int transport_info_update_count_ = 0; |
| 230 bool ipv6_enabled_; |
213 bool running_ = false; | 231 bool running_ = false; |
214 }; | 232 }; |
215 | 233 |
216 class FakePortAllocator : public cricket::PortAllocator { | 234 class FakePortAllocator : public cricket::PortAllocator { |
217 public: | 235 public: |
218 FakePortAllocator(rtc::Thread* network_thread, | 236 FakePortAllocator(rtc::Thread* network_thread, |
219 rtc::PacketSocketFactory* factory) | 237 rtc::PacketSocketFactory* factory) |
220 : network_thread_(network_thread), factory_(factory) { | 238 : network_thread_(network_thread), factory_(factory) { |
221 if (factory_ == NULL) { | 239 if (factory_ == NULL) { |
222 owned_factory_.reset(new rtc::BasicPacketSocketFactory(network_thread_)); | 240 owned_factory_.reset(new rtc::BasicPacketSocketFactory(network_thread_)); |
223 factory_ = owned_factory_.get(); | 241 factory_ = owned_factory_.get(); |
224 } | 242 } |
| 243 ipv6_enabled_ = rtc::HasIPv6Enabled(); |
225 } | 244 } |
226 | 245 |
227 void Initialize() override { | 246 void Initialize() override { |
228 // Port allocator should be initialized on the network thread. | 247 // Port allocator should be initialized on the network thread. |
229 RTC_CHECK(network_thread_->IsCurrent()); | 248 RTC_CHECK(network_thread_->IsCurrent()); |
230 initialized_ = true; | 249 initialized_ = true; |
231 } | 250 } |
232 | 251 |
233 void SetNetworkIgnoreMask(int network_ignore_mask) override {} | 252 void SetNetworkIgnoreMask(int network_ignore_mask) override {} |
234 | 253 |
| 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 |
235 cricket::PortAllocatorSession* CreateSessionInternal( | 258 cricket::PortAllocatorSession* CreateSessionInternal( |
236 const std::string& content_name, | 259 const std::string& content_name, |
237 int component, | 260 int component, |
238 const std::string& ice_ufrag, | 261 const std::string& ice_ufrag, |
239 const std::string& ice_pwd) override { | 262 const std::string& ice_pwd) override { |
240 return new FakePortAllocatorSession(this, network_thread_, factory_, | 263 return new FakePortAllocatorSession(this, network_thread_, factory_, |
241 content_name, component, ice_ufrag, | 264 content_name, component, ice_ufrag, |
242 ice_pwd); | 265 ice_pwd, ipv6_enabled_); |
243 } | 266 } |
244 | 267 |
245 bool initialized() const { return initialized_; } | 268 bool initialized() const { return initialized_; } |
246 | 269 |
247 private: | 270 private: |
248 rtc::Thread* network_thread_; | 271 rtc::Thread* network_thread_; |
249 rtc::PacketSocketFactory* factory_; | 272 rtc::PacketSocketFactory* factory_; |
250 std::unique_ptr<rtc::BasicPacketSocketFactory> owned_factory_; | 273 std::unique_ptr<rtc::BasicPacketSocketFactory> owned_factory_; |
251 bool initialized_ = false; | 274 bool initialized_ = false; |
| 275 bool ipv6_enabled_ = false; |
252 }; | 276 }; |
253 | 277 |
254 } // namespace cricket | 278 } // namespace cricket |
255 | 279 |
256 #endif // WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ | 280 #endif // WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ |
OLD | NEW |