Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/protocol/transport_context.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "remoting/protocol/port_allocator_factory.h" | |
| 12 #include "third_party/webrtc/p2p/client/httpportallocator.h" | |
| 13 | |
| 14 namespace remoting { | |
| 15 namespace protocol { | |
| 16 | |
| 17 // Get fresh STUN/Relay configuration every hour. | |
| 18 static const int kJingleInfoUpdatePeriodSeconds = 3600; | |
| 19 | |
| 20 TransportContext::TransportContext( | |
| 21 SignalStrategy* signal_strategy, | |
| 22 scoped_ptr<PortAllocatorFactory> port_allocator_factory, | |
| 23 const NetworkSettings& network_settings, | |
| 24 TransportRole role) | |
| 25 : signal_strategy_(signal_strategy), | |
| 26 port_allocator_factory_(port_allocator_factory.Pass()), | |
| 27 network_settings_(network_settings), | |
| 28 role_(role) {} | |
| 29 | |
| 30 TransportContext::~TransportContext() {} | |
| 31 | |
| 32 void TransportContext::Prepare() { | |
| 33 EnsureFreshJingleInfo(); | |
| 34 } | |
| 35 | |
| 36 void TransportContext::CreatePortAllocator( | |
| 37 const CreatePortAllocatorCallback& callback) { | |
| 38 EnsureFreshJingleInfo(); | |
| 39 | |
| 40 // If there is a pending |jingle_info_request_| delay starting the new | |
| 41 // transport until the request is finished. | |
| 42 if (jingle_info_request_) { | |
| 43 pending_port_allocator_requests_.push_back(callback); | |
| 44 } else { | |
| 45 callback.Run(CreatePortAllocatorInternal()); | |
|
Jamie
2015/12/14 23:29:05
In our JS code, we try to make sure that, even in
Sergey Ulanov
2015/12/15 18:52:15
We use the same pattern for many other asynchronou
| |
| 46 } | |
| 47 } | |
| 48 | |
| 49 void TransportContext::EnsureFreshJingleInfo() { | |
| 50 uint32 stun_or_relay_flags = NetworkSettings::NAT_TRAVERSAL_STUN | | |
| 51 NetworkSettings::NAT_TRAVERSAL_RELAY; | |
| 52 if (!(network_settings_.flags & stun_or_relay_flags) || | |
|
Jamie
2015/12/14 23:29:05
I think an explicit ==0 would be clearer than a !
Sergey Ulanov
2015/12/15 18:52:15
Done.
| |
| 53 jingle_info_request_) { | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 if (last_jingle_info_update_time_.is_null() || | |
| 58 base::TimeTicks::Now() - last_jingle_info_update_time_ > | |
| 59 base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds)) { | |
| 60 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); | |
| 61 jingle_info_request_->Send(base::Bind( | |
| 62 &TransportContext::OnJingleInfo, base::Unretained(this))); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void TransportContext::OnJingleInfo( | |
| 67 const std::string& relay_token, | |
| 68 const std::vector<std::string>& relay_hosts, | |
| 69 const std::vector<rtc::SocketAddress>& stun_hosts) { | |
| 70 relay_token_ = relay_token; | |
| 71 relay_hosts_ = relay_hosts; | |
| 72 stun_hosts_ = stun_hosts; | |
| 73 | |
| 74 jingle_info_request_.reset(); | |
| 75 if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) | |
|
Jamie
2015/12/14 23:29:05
If neither relay nor stun hosts were received, sho
Sergey Ulanov
2015/12/15 18:52:15
This check is here to handle the case when the req
| |
| 76 last_jingle_info_update_time_ = base::TimeTicks::Now(); | |
|
Jamie
2015/12/14 23:29:05
Doesn't this belong in EnsureFreshJingleInfo (or m
Sergey Ulanov
2015/12/15 18:52:15
EnsureFreshJingleInfo() makes a request only if it
| |
| 77 | |
| 78 while (!pending_port_allocator_requests_.empty()) { | |
| 79 pending_port_allocator_requests_.begin()->Run( | |
| 80 CreatePortAllocatorInternal()); | |
| 81 pending_port_allocator_requests_.pop_front(); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 scoped_ptr<cricket::PortAllocator> | |
| 86 TransportContext::CreatePortAllocatorInternal() { | |
| 87 scoped_ptr<cricket::HttpPortAllocatorBase> result = | |
| 88 port_allocator_factory_->CreatePortAllocator(); | |
| 89 | |
| 90 LOG(ERROR) << relay_hosts_.size(); | |
|
Jamie
2015/12/14 23:29:05
Remove (or lower verbosity level).
Sergey Ulanov
2015/12/15 18:52:15
Sorry, I thought I removed these.
| |
| 91 if (!relay_token_.empty() && !relay_hosts_.empty()) { | |
| 92 result->SetRelayHosts(relay_hosts_); | |
| 93 result->SetRelayToken(relay_token_); | |
| 94 } | |
| 95 LOG(ERROR) << stun_hosts_.size(); | |
|
Jamie
2015/12/14 23:29:05
Ditto
Sergey Ulanov
2015/12/15 18:52:15
Done.
| |
| 96 if (!stun_hosts_.empty()) { | |
| 97 result->SetStunHosts(stun_hosts_); | |
| 98 } | |
| 99 | |
| 100 // We always use PseudoTcp to provide a reliable channel. It provides poor | |
| 101 // performance when combined with TCP-based transport, so we have to disable | |
| 102 // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username | |
| 103 // fragment is shared between all candidates. | |
| 104 int flags = cricket::PORTALLOCATOR_DISABLE_TCP | | |
| 105 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | | |
| 106 cricket::PORTALLOCATOR_ENABLE_IPV6; | |
| 107 | |
| 108 if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_STUN)) | |
| 109 flags |= cricket::PORTALLOCATOR_DISABLE_STUN; | |
| 110 | |
| 111 if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_RELAY)) | |
| 112 flags |= cricket::PORTALLOCATOR_DISABLE_RELAY; | |
| 113 | |
| 114 result->set_flags(flags); | |
| 115 result->SetPortRange(network_settings_.port_range.min_port, | |
| 116 network_settings_.port_range.max_port); | |
| 117 | |
| 118 return result.Pass(); | |
| 119 } | |
| 120 | |
| 121 } // namespace protocol | |
| 122 } // namespace remoting | |
| OLD | NEW |