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