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