OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 29 matching lines...) Expand all Loading... | |
40 MSG_CONFIG_STOP, | 40 MSG_CONFIG_STOP, |
41 }; | 41 }; |
42 | 42 |
43 const int PHASE_UDP = 0; | 43 const int PHASE_UDP = 0; |
44 const int PHASE_RELAY = 1; | 44 const int PHASE_RELAY = 1; |
45 const int PHASE_TCP = 2; | 45 const int PHASE_TCP = 2; |
46 const int PHASE_SSLTCP = 3; | 46 const int PHASE_SSLTCP = 3; |
47 | 47 |
48 const int kNumPhases = 4; | 48 const int kNumPhases = 4; |
49 | 49 |
50 // Gets address family priority: Unspecified < IPv4 < IPv6. | |
51 int GetAddressFamilyPriority(int ip_family) { | |
52 switch (ip_family) { | |
53 case AF_UNSPEC: | |
54 return 0; | |
55 case AF_INET: | |
56 return 1; | |
57 case AF_INET6: | |
58 return 2; | |
59 default: | |
60 RTC_DCHECK(false); | |
61 return 0; | |
62 } | |
63 } | |
64 | |
65 // Returns positive if a is better, negative if b is better, and 0 otherwise. | |
66 int ComparePort(const cricket::Port* a, const cricket::Port* b) { | |
67 static constexpr int a_is_better = 1; | |
68 static constexpr int b_is_better = -1; | |
69 // Protocol type is defined as UDP = 0, TCP = 1, SSLTCP = 2. | |
70 if (a->GetProtocol() < b->GetProtocol()) { | |
71 return a_is_better; | |
72 } | |
73 if (a->GetProtocol() > b->GetProtocol()) { | |
74 return b_is_better; | |
75 } | |
pthatcher1
2016/06/28 21:01:41
I think it makes sense to prefer UDP > TCP, but I
honghaiz3
2016/06/28 23:36:00
keep this based on our discussion.
| |
76 | |
77 int a_family = GetAddressFamilyPriority(a->Network()->GetBestIP().family()); | |
78 int b_family = GetAddressFamilyPriority(b->Network()->GetBestIP().family()); | |
79 return a_family - b_family; | |
80 } | |
81 | |
50 } // namespace | 82 } // namespace |
51 | 83 |
52 namespace cricket { | 84 namespace cricket { |
53 const uint32_t DISABLE_ALL_PHASES = | 85 const uint32_t DISABLE_ALL_PHASES = |
54 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP | | 86 PORTALLOCATOR_DISABLE_UDP | PORTALLOCATOR_DISABLE_TCP | |
55 PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY; | 87 PORTALLOCATOR_DISABLE_STUN | PORTALLOCATOR_DISABLE_RELAY; |
56 | 88 |
57 // BasicPortAllocator | 89 // BasicPortAllocator |
58 BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, | 90 BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, |
59 rtc::PacketSocketFactory* socket_factory) | 91 rtc::PacketSocketFactory* socket_factory) |
60 : network_manager_(network_manager), socket_factory_(socket_factory) { | 92 : network_manager_(network_manager), socket_factory_(socket_factory) { |
61 ASSERT(network_manager_ != nullptr); | 93 ASSERT(network_manager_ != nullptr); |
62 ASSERT(socket_factory_ != nullptr); | 94 ASSERT(socket_factory_ != nullptr); |
63 Construct(); | 95 Construct(); |
64 } | 96 } |
65 | 97 |
66 BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager) | 98 BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager) |
67 : network_manager_(network_manager), socket_factory_(nullptr) { | 99 : network_manager_(network_manager), socket_factory_(nullptr) { |
68 ASSERT(network_manager_ != nullptr); | 100 ASSERT(network_manager_ != nullptr); |
69 Construct(); | 101 Construct(); |
70 } | 102 } |
71 | 103 |
72 BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, | 104 BasicPortAllocator::BasicPortAllocator(rtc::NetworkManager* network_manager, |
73 rtc::PacketSocketFactory* socket_factory, | 105 rtc::PacketSocketFactory* socket_factory, |
74 const ServerAddresses& stun_servers) | 106 const ServerAddresses& stun_servers) |
75 : network_manager_(network_manager), socket_factory_(socket_factory) { | 107 : network_manager_(network_manager), socket_factory_(socket_factory) { |
76 ASSERT(socket_factory_ != NULL); | 108 ASSERT(socket_factory_ != NULL); |
77 SetConfiguration(stun_servers, std::vector<RelayServerConfig>(), 0); | 109 SetConfiguration(stun_servers, std::vector<RelayServerConfig>(), 0, false); |
78 Construct(); | 110 Construct(); |
79 } | 111 } |
80 | 112 |
81 BasicPortAllocator::BasicPortAllocator( | 113 BasicPortAllocator::BasicPortAllocator( |
82 rtc::NetworkManager* network_manager, | 114 rtc::NetworkManager* network_manager, |
83 const ServerAddresses& stun_servers, | 115 const ServerAddresses& stun_servers, |
84 const rtc::SocketAddress& relay_address_udp, | 116 const rtc::SocketAddress& relay_address_udp, |
85 const rtc::SocketAddress& relay_address_tcp, | 117 const rtc::SocketAddress& relay_address_tcp, |
86 const rtc::SocketAddress& relay_address_ssl) | 118 const rtc::SocketAddress& relay_address_ssl) |
87 : network_manager_(network_manager), socket_factory_(NULL) { | 119 : network_manager_(network_manager), socket_factory_(NULL) { |
88 std::vector<RelayServerConfig> turn_servers; | 120 std::vector<RelayServerConfig> turn_servers; |
89 RelayServerConfig config(RELAY_GTURN); | 121 RelayServerConfig config(RELAY_GTURN); |
90 if (!relay_address_udp.IsNil()) { | 122 if (!relay_address_udp.IsNil()) { |
91 config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP)); | 123 config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP)); |
92 } | 124 } |
93 if (!relay_address_tcp.IsNil()) { | 125 if (!relay_address_tcp.IsNil()) { |
94 config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP)); | 126 config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP)); |
95 } | 127 } |
96 if (!relay_address_ssl.IsNil()) { | 128 if (!relay_address_ssl.IsNil()) { |
97 config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP)); | 129 config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP)); |
98 } | 130 } |
99 | 131 |
100 if (!config.ports.empty()) { | 132 if (!config.ports.empty()) { |
101 turn_servers.push_back(config); | 133 turn_servers.push_back(config); |
102 } | 134 } |
103 | 135 |
104 SetConfiguration(stun_servers, turn_servers, 0); | 136 SetConfiguration(stun_servers, turn_servers, 0, false); |
105 Construct(); | 137 Construct(); |
106 } | 138 } |
107 | 139 |
108 void BasicPortAllocator::Construct() { | 140 void BasicPortAllocator::Construct() { |
109 allow_tcp_listen_ = true; | 141 allow_tcp_listen_ = true; |
110 } | 142 } |
111 | 143 |
112 BasicPortAllocator::~BasicPortAllocator() { | 144 BasicPortAllocator::~BasicPortAllocator() { |
113 } | 145 } |
114 | 146 |
115 PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( | 147 PortAllocatorSession* BasicPortAllocator::CreateSessionInternal( |
116 const std::string& content_name, int component, | 148 const std::string& content_name, int component, |
117 const std::string& ice_ufrag, const std::string& ice_pwd) { | 149 const std::string& ice_ufrag, const std::string& ice_pwd) { |
118 return new BasicPortAllocatorSession( | 150 return new BasicPortAllocatorSession( |
119 this, content_name, component, ice_ufrag, ice_pwd); | 151 this, content_name, component, ice_ufrag, ice_pwd); |
120 } | 152 } |
121 | 153 |
122 void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) { | 154 void BasicPortAllocator::AddTurnServer(const RelayServerConfig& turn_server) { |
123 std::vector<RelayServerConfig> new_turn_servers = turn_servers(); | 155 std::vector<RelayServerConfig> new_turn_servers = turn_servers(); |
124 new_turn_servers.push_back(turn_server); | 156 new_turn_servers.push_back(turn_server); |
125 SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size()); | 157 SetConfiguration(stun_servers(), new_turn_servers, candidate_pool_size(), |
158 prune_turn_ports()); | |
126 } | 159 } |
127 | 160 |
128 // BasicPortAllocatorSession | 161 // BasicPortAllocatorSession |
129 BasicPortAllocatorSession::BasicPortAllocatorSession( | 162 BasicPortAllocatorSession::BasicPortAllocatorSession( |
130 BasicPortAllocator *allocator, | 163 BasicPortAllocator *allocator, |
131 const std::string& content_name, | 164 const std::string& content_name, |
132 int component, | 165 int component, |
133 const std::string& ice_ufrag, | 166 const std::string& ice_ufrag, |
134 const std::string& ice_pwd) | 167 const std::string& ice_pwd) |
135 : PortAllocatorSession(content_name, component, | 168 : PortAllocatorSession(content_name, component, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 } | 243 } |
211 | 244 |
212 void BasicPortAllocatorSession::ClearGettingPorts() { | 245 void BasicPortAllocatorSession::ClearGettingPorts() { |
213 network_thread_->Clear(this, MSG_ALLOCATE); | 246 network_thread_->Clear(this, MSG_ALLOCATE); |
214 for (uint32_t i = 0; i < sequences_.size(); ++i) | 247 for (uint32_t i = 0; i < sequences_.size(); ++i) |
215 sequences_[i]->Stop(); | 248 sequences_[i]->Stop(); |
216 } | 249 } |
217 | 250 |
218 std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const { | 251 std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const { |
219 std::vector<PortInterface*> ret; | 252 std::vector<PortInterface*> ret; |
220 for (const PortData& port : ports_) { | 253 for (const PortData& data : ports_) { |
221 if (port.has_pairable_candidate() && !port.error()) { | 254 if (IsPortInUse(data)) { |
pthatcher1
2016/06/28 21:01:41
Since the existing port is ReadyPorts, why not cal
honghaiz3
2016/06/28 23:36:00
Done.
| |
222 ret.push_back(port.port()); | 255 ret.push_back(data.port()); |
223 } | 256 } |
224 } | 257 } |
225 return ret; | 258 return ret; |
226 } | 259 } |
227 | 260 |
228 std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const { | 261 std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const { |
229 std::vector<Candidate> candidates; | 262 std::vector<Candidate> candidates; |
230 for (const PortData& data : ports_) { | 263 for (const PortData& data : ports_) { |
264 if (!IsPortInUse(data)) { | |
265 continue; | |
266 } | |
267 | |
231 for (const Candidate& candidate : data.port()->Candidates()) { | 268 for (const Candidate& candidate : data.port()->Candidates()) { |
232 if (!CheckCandidateFilter(candidate)) { | 269 if (!CheckCandidateFilter(candidate)) { |
233 continue; | 270 continue; |
234 } | 271 } |
235 ProtocolType pvalue; | 272 ProtocolType pvalue; |
236 if (!StringToProto(candidate.protocol().c_str(), &pvalue) || | 273 if (!StringToProto(candidate.protocol().c_str(), &pvalue) || |
237 !data.sequence()->ProtocolEnabled(pvalue)) { | 274 !data.sequence()->ProtocolEnabled(pvalue)) { |
238 continue; | 275 continue; |
239 } | 276 } |
240 candidates.push_back(SanitizeRelatedAddress(candidate)); | 277 candidates.push_back(SanitizeRelatedAddress(candidate)); |
(...skipping 30 matching lines...) Expand all Loading... | |
271 } | 308 } |
272 | 309 |
273 // Check that all port allocation sequences are complete (not running). | 310 // Check that all port allocation sequences are complete (not running). |
274 if (std::any_of(sequences_.begin(), sequences_.end(), | 311 if (std::any_of(sequences_.begin(), sequences_.end(), |
275 [](const AllocationSequence* sequence) { | 312 [](const AllocationSequence* sequence) { |
276 return sequence->state() == AllocationSequence::kRunning; | 313 return sequence->state() == AllocationSequence::kRunning; |
277 })) { | 314 })) { |
278 return false; | 315 return false; |
279 } | 316 } |
280 | 317 |
281 // If all allocated ports are in complete state, session must have got all | 318 // If all allocated ports are in one of the terminal states, session must have |
pthatcher1
2016/06/28 21:01:41
Could say "if all allocated ports are no longer ga
honghaiz3
2016/06/28 23:36:00
Done.
| |
282 // expected candidates. Session will trigger candidates allocation complete | 319 // got all expected candidates. Session will trigger candidates allocation |
283 // signal. | 320 // complete signal. |
284 if (!std::all_of(ports_.begin(), ports_.end(), [](const PortData& port) { | 321 if (std::all_of(ports_.begin(), ports_.end(), |
pthatcher1
2016/06/28 21:01:41
Could be "std::none_of(ports_.... port.gathering()
honghaiz3
2016/06/28 23:36:00
Done used any_of ... May be more readable.
| |
285 return (port.complete() || port.error()); | 322 [](const PortData& port) { return port.terminated(); })) { |
286 })) { | 323 return true; |
287 return false; | |
288 } | 324 } |
289 | 325 |
290 return true; | 326 return false; |
pthatcher1
2016/06/28 21:01:41
Instead of
if(foo)
return true;
return false;
honghaiz3
2016/06/28 23:36:00
Done.
| |
291 } | 327 } |
292 | 328 |
293 void BasicPortAllocatorSession::OnMessage(rtc::Message *message) { | 329 void BasicPortAllocatorSession::OnMessage(rtc::Message *message) { |
294 switch (message->message_id) { | 330 switch (message->message_id) { |
295 case MSG_CONFIG_START: | 331 case MSG_CONFIG_START: |
296 ASSERT(rtc::Thread::Current() == network_thread_); | 332 ASSERT(rtc::Thread::Current() == network_thread_); |
297 GetPortConfigurations(); | 333 GetPortConfigurations(); |
298 break; | 334 break; |
299 case MSG_CONFIG_READY: | 335 case MSG_CONFIG_READY: |
300 ASSERT(rtc::Thread::Current() == network_thread_); | 336 ASSERT(rtc::Thread::Current() == network_thread_); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 | 386 |
351 void BasicPortAllocatorSession::OnConfigStop() { | 387 void BasicPortAllocatorSession::OnConfigStop() { |
352 ASSERT(rtc::Thread::Current() == network_thread_); | 388 ASSERT(rtc::Thread::Current() == network_thread_); |
353 | 389 |
354 // If any of the allocated ports have not completed the candidates allocation, | 390 // If any of the allocated ports have not completed the candidates allocation, |
355 // mark those as error. Since session doesn't need any new candidates | 391 // mark those as error. Since session doesn't need any new candidates |
356 // at this stage of the allocation, it's safe to discard any new candidates. | 392 // at this stage of the allocation, it's safe to discard any new candidates. |
357 bool send_signal = false; | 393 bool send_signal = false; |
358 for (std::vector<PortData>::iterator it = ports_.begin(); | 394 for (std::vector<PortData>::iterator it = ports_.begin(); |
359 it != ports_.end(); ++it) { | 395 it != ports_.end(); ++it) { |
360 if (!it->complete() && !it->error()) { | 396 if (!it->terminated()) { |
pthatcher1
2016/06/28 21:01:41
it->gathering()
?
honghaiz3
2016/06/28 23:36:00
Done.
| |
361 // Updating port state to error, which didn't finish allocating candidates | 397 // Updating port state to error, which didn't finish allocating candidates |
362 // yet. | 398 // yet. |
363 it->set_error(); | 399 it->set_error(); |
364 send_signal = true; | 400 send_signal = true; |
365 } | 401 } |
366 } | 402 } |
367 | 403 |
368 // Did we stop any running sequences? | 404 // Did we stop any running sequences? |
369 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin(); | 405 for (std::vector<AllocationSequence*>::iterator it = sequences_.begin(); |
370 it != sequences_.end() && !send_signal; ++it) { | 406 it != sequences_.end() && !send_signal; ++it) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 // create a new sequence to create the appropriate ports. | 472 // create a new sequence to create the appropriate ports. |
437 void BasicPortAllocatorSession::DoAllocate() { | 473 void BasicPortAllocatorSession::DoAllocate() { |
438 bool done_signal_needed = false; | 474 bool done_signal_needed = false; |
439 std::vector<rtc::Network*> networks; | 475 std::vector<rtc::Network*> networks; |
440 GetNetworks(&networks); | 476 GetNetworks(&networks); |
441 | 477 |
442 if (networks.empty()) { | 478 if (networks.empty()) { |
443 LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated"; | 479 LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated"; |
444 done_signal_needed = true; | 480 done_signal_needed = true; |
445 } else { | 481 } else { |
482 PortConfiguration* config = configs_.empty() ? nullptr : configs_.back(); | |
446 for (uint32_t i = 0; i < networks.size(); ++i) { | 483 for (uint32_t i = 0; i < networks.size(); ++i) { |
447 PortConfiguration* config = NULL; | |
448 if (configs_.size() > 0) | |
449 config = configs_.back(); | |
450 | |
451 uint32_t sequence_flags = flags(); | 484 uint32_t sequence_flags = flags(); |
452 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { | 485 if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) { |
453 // If all the ports are disabled we should just fire the allocation | 486 // If all the ports are disabled we should just fire the allocation |
454 // done event and return. | 487 // done event and return. |
455 done_signal_needed = true; | 488 done_signal_needed = true; |
456 break; | 489 break; |
457 } | 490 } |
458 | 491 |
459 if (!config || config->relays.empty()) { | 492 if (!config || config->relays.empty()) { |
460 // No relay ports specified in this config. | 493 // No relay ports specified in this config. |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
562 MaybeSignalCandidatesAllocationDone(); | 595 MaybeSignalCandidatesAllocationDone(); |
563 } | 596 } |
564 | 597 |
565 void BasicPortAllocatorSession::OnCandidateReady( | 598 void BasicPortAllocatorSession::OnCandidateReady( |
566 Port* port, const Candidate& c) { | 599 Port* port, const Candidate& c) { |
567 ASSERT(rtc::Thread::Current() == network_thread_); | 600 ASSERT(rtc::Thread::Current() == network_thread_); |
568 PortData* data = FindPort(port); | 601 PortData* data = FindPort(port); |
569 ASSERT(data != NULL); | 602 ASSERT(data != NULL); |
570 // Discarding any candidate signal if port allocation status is | 603 // Discarding any candidate signal if port allocation status is |
571 // already in completed state. | 604 // already in completed state. |
572 if (data->complete() || data->error()) { | 605 if (data->terminated()) { |
573 return; | 606 return; |
574 } | 607 } |
575 | 608 |
609 // Mark that the port has a pairable candidate, either because we have a | |
610 // usable candidate from the port, or simply because the port is bound to the | |
611 // any address and therefore has no host candidate. This will trigger the port | |
612 // to start creating candidate pairs (connections) and issue connectivity | |
613 // checks. If port has already been marked as having a pairable candidate, | |
614 // do nothing here. | |
615 // Note: Need to do MaybeSignalCandidateReady after this because we will | |
616 // check whether the candidate is generated by the port in use. | |
617 if (!data->has_pairable_candidate() && CandidatePairable(c, port)) { | |
618 OnPortPairable(data); | |
pthatcher1
2016/06/28 21:01:41
We should keep has_pairable_candidate and set_has_
honghaiz3
2016/06/28 23:36:00
Done.
| |
619 } | |
620 | |
576 ProtocolType pvalue; | 621 ProtocolType pvalue; |
577 bool candidate_protocol_enabled = | 622 bool candidate_protocol_enabled = |
578 StringToProto(c.protocol().c_str(), &pvalue) && | 623 StringToProto(c.protocol().c_str(), &pvalue) && |
579 data->sequence()->ProtocolEnabled(pvalue); | 624 data->sequence()->ProtocolEnabled(pvalue); |
580 | 625 |
581 if (CheckCandidateFilter(c) && candidate_protocol_enabled) { | 626 if (CheckCandidateFilter(c) && candidate_protocol_enabled && |
627 IsPortInUse(*data)) { | |
582 std::vector<Candidate> candidates; | 628 std::vector<Candidate> candidates; |
583 candidates.push_back(SanitizeRelatedAddress(c)); | 629 candidates.push_back(SanitizeRelatedAddress(c)); |
584 SignalCandidatesReady(this, candidates); | 630 SignalCandidatesReady(this, candidates); |
585 } | 631 } |
632 } | |
586 | 633 |
587 // Port has already been marked as having a pairable candidate. | 634 void BasicPortAllocatorSession::OnPortPairable(PortData* port_data) { |
pthatcher1
2016/06/28 21:01:41
We could call it "data" or "port_data" consistenly
honghaiz3
2016/06/28 23:36:00
Now that I have used helper methods below, it is p
| |
588 // Nothing to do here. | 635 port_data->set_has_pairable_candidate(true); |
589 if (data->has_pairable_candidate()) { | 636 if (port_data->pruned()) { |
637 // A port may have been pruned before it becomes ready. In that case, | |
638 // do not need to do anything here. | |
590 return; | 639 return; |
591 } | 640 } |
592 | 641 |
593 // Mark that the port has a pairable candidate, either because we have a | 642 Port* port = port_data->port(); |
594 // usable candidate from the port, or simply because the port is bound to the | 643 if (!allocator_->prune_turn_ports() || port->Type() != RELAY_PORT_TYPE) { |
595 // any address and therefore has no host candidate. This will trigger the port | |
596 // to start creating candidate pairs (connections) and issue connectivity | |
597 // checks. | |
598 if (CandidatePairable(c, port)) { | |
599 data->set_has_pairable_candidate(true); | |
600 SignalPortReady(this, port); | 644 SignalPortReady(this, port); |
645 return; | |
646 } | |
647 | |
648 // Note: We determine the same network based only on their network names. So | |
649 // if an IPv4 address and an IPv6 address have the same network name, they | |
650 // are considered the same network here. | |
651 Port* best_turn_port_on_same_network = nullptr; | |
652 for (const PortData& data : ports_) { | |
653 if (data.port()->Network()->name() == port->Network()->name() && | |
654 data.port()->Type() == RELAY_PORT_TYPE && IsPortInUse(data) && | |
655 (!best_turn_port_on_same_network || | |
656 ComparePort(data.port(), best_turn_port_on_same_network) > 0)) { | |
657 best_turn_port_on_same_network = data.port(); | |
658 } | |
659 } | |
pthatcher1
2016/06/28 21:01:41
Can you move this into a helper method that can be
honghaiz3
2016/06/28 23:36:00
Done.
| |
660 // |port| is already in the list of ports, so the best port cannot be nullptr. | |
661 RTC_DCHECK(best_turn_port_on_same_network != nullptr); | |
662 if (ComparePort(port, best_turn_port_on_same_network) >= 0) { | |
663 SignalPortReady(this, port); | |
664 } | |
665 // Prune existing ports that have lower priorities. | |
666 bool pruned_port = false; | |
667 for (PortData& data : ports_) { | |
668 if (data.port()->Network()->name() == port->Network()->name() && | |
669 data.port()->Type() == RELAY_PORT_TYPE && !data.pruned() && | |
670 ComparePort(data.port(), best_turn_port_on_same_network) < 0) { | |
671 data.set_pruned(); | |
672 pruned_port = true; | |
673 // Don't need to send SignalPortPruned for the current port because it was | |
674 // not signaled for being ready. | |
675 if (data.port() != port) { | |
676 SignalPortPruned(this, data.port()); | |
677 } | |
678 } | |
679 } | |
pthatcher1
2016/06/28 21:01:41
Can you move this into a helper method that can be
honghaiz3
2016/06/28 23:36:00
Done.
| |
680 if (pruned_port) { | |
681 MaybeSignalCandidatesAllocationDone(); | |
pthatcher1
2016/06/28 21:01:41
Shouldn't these just be part of the if block above
honghaiz3
2016/06/28 23:36:00
Yes we can do some optimization like that, althoug
| |
601 } | 682 } |
602 } | 683 } |
603 | 684 |
685 bool BasicPortAllocatorSession::IsPortInUse(const PortData& data) const { | |
686 return data.has_pairable_candidate() && !data.pruned() && !data.error(); | |
687 } | |
688 | |
604 void BasicPortAllocatorSession::OnPortComplete(Port* port) { | 689 void BasicPortAllocatorSession::OnPortComplete(Port* port) { |
605 ASSERT(rtc::Thread::Current() == network_thread_); | 690 ASSERT(rtc::Thread::Current() == network_thread_); |
606 PortData* data = FindPort(port); | 691 PortData* data = FindPort(port); |
607 ASSERT(data != NULL); | 692 ASSERT(data != NULL); |
608 | 693 |
609 // Ignore any late signals. | 694 // Ignore any late signals. |
610 if (data->complete() || data->error()) { | 695 if (data->terminated()) { |
611 return; | 696 return; |
612 } | 697 } |
613 | 698 |
614 // Moving to COMPLETE state. | 699 // Moving to COMPLETE state. |
615 data->set_complete(); | 700 data->set_complete(); |
616 // Send candidate allocation complete signal if this was the last port. | 701 // Send candidate allocation complete signal if this was the last port. |
617 MaybeSignalCandidatesAllocationDone(); | 702 MaybeSignalCandidatesAllocationDone(); |
618 } | 703 } |
619 | 704 |
620 void BasicPortAllocatorSession::OnPortError(Port* port) { | 705 void BasicPortAllocatorSession::OnPortError(Port* port) { |
621 ASSERT(rtc::Thread::Current() == network_thread_); | 706 ASSERT(rtc::Thread::Current() == network_thread_); |
622 PortData* data = FindPort(port); | 707 PortData* data = FindPort(port); |
623 ASSERT(data != NULL); | 708 ASSERT(data != NULL); |
624 // We might have already given up on this port and stopped it. | 709 // We might have already given up on this port and stopped it. |
625 if (data->complete() || data->error()) { | 710 if (data->terminated()) { |
626 return; | 711 return; |
627 } | 712 } |
628 | 713 |
629 // SignalAddressError is currently sent from StunPort/TurnPort. | 714 // SignalAddressError is currently sent from StunPort/TurnPort. |
630 // But this signal itself is generic. | 715 // But this signal itself is generic. |
631 data->set_error(); | 716 data->set_error(); |
632 // Send candidate allocation complete signal if this was the last port. | 717 // Send candidate allocation complete signal if this was the last port. |
633 MaybeSignalCandidatesAllocationDone(); | 718 MaybeSignalCandidatesAllocationDone(); |
634 } | 719 } |
635 | 720 |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1021 | 1106 |
1022 // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we | 1107 // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we |
1023 // ought to have a relay list for them here. | 1108 // ought to have a relay list for them here. |
1024 ASSERT(config_ && !config_->relays.empty()); | 1109 ASSERT(config_ && !config_->relays.empty()); |
1025 if (!(config_ && !config_->relays.empty())) { | 1110 if (!(config_ && !config_->relays.empty())) { |
1026 LOG(LS_WARNING) | 1111 LOG(LS_WARNING) |
1027 << "AllocationSequence: No relay server configured, skipping."; | 1112 << "AllocationSequence: No relay server configured, skipping."; |
1028 return; | 1113 return; |
1029 } | 1114 } |
1030 | 1115 |
1031 PortConfiguration::RelayList::const_iterator relay; | 1116 for (RelayServerConfig& relay : config_->relays) { |
1032 for (relay = config_->relays.begin(); | 1117 if (relay.type == RELAY_GTURN) { |
1033 relay != config_->relays.end(); ++relay) { | 1118 CreateGturnPort(relay); |
1034 if (relay->type == RELAY_GTURN) { | 1119 } else if (relay.type == RELAY_TURN) { |
1035 CreateGturnPort(*relay); | 1120 CreateTurnPort(relay); |
1036 } else if (relay->type == RELAY_TURN) { | |
1037 CreateTurnPort(*relay); | |
1038 } else { | 1121 } else { |
1039 ASSERT(false); | 1122 ASSERT(false); |
1040 } | 1123 } |
1041 } | 1124 } |
1042 } | 1125 } |
1043 | 1126 |
1044 void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) { | 1127 void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) { |
1045 // TODO(mallinath) - Rename RelayPort to GTurnPort. | 1128 // TODO(mallinath) - Rename RelayPort to GTurnPort. |
1046 RelayPort* port = RelayPort::Create(session_->network_thread(), | 1129 RelayPort* port = RelayPort::Create(session_->network_thread(), |
1047 session_->socket_factory(), | 1130 session_->socket_factory(), |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1235 ServerAddresses servers; | 1318 ServerAddresses servers; |
1236 for (size_t i = 0; i < relays.size(); ++i) { | 1319 for (size_t i = 0; i < relays.size(); ++i) { |
1237 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { | 1320 if (relays[i].type == turn_type && SupportsProtocol(relays[i], type)) { |
1238 servers.insert(relays[i].ports.front().address); | 1321 servers.insert(relays[i].ports.front().address); |
1239 } | 1322 } |
1240 } | 1323 } |
1241 return servers; | 1324 return servers; |
1242 } | 1325 } |
1243 | 1326 |
1244 } // namespace cricket | 1327 } // namespace cricket |
OLD | NEW |