Chromium Code Reviews| Index: remoting/host/host_port_allocator.cc |
| diff --git a/remoting/host/host_port_allocator.cc b/remoting/host/host_port_allocator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ce967e8d0424240c2095464d531ed95e4cef6a0d |
| --- /dev/null |
| +++ b/remoting/host/host_port_allocator.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/host_port_allocator.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/stl_util.h" |
| +#include "base/string_number_conversions.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "remoting/host/network_settings.h" |
| +#include "third_party/libjingle/source/talk/base/basicpacketsocketfactory.h" |
| + |
| +namespace remoting { |
| + |
| +HostPortAllocatorSession::HostPortAllocatorSession( |
| + cricket::HttpPortAllocatorBase* allocator, |
| + const std::string& channel_name, |
| + int component, |
| + const std::vector<talk_base::SocketAddress>& stun_hosts, |
| + const std::vector<std::string>& relay_hosts, |
| + const std::string& relay, |
| + const scoped_refptr<net::URLRequestContextGetter>& url_context) |
| + : HttpPortAllocatorSessionBase( |
| + allocator, channel_name, component, stun_hosts, relay_hosts, relay, ""), |
| + url_context_(url_context) { |
| +} |
| + |
| +HostPortAllocatorSession::~HostPortAllocatorSession() { |
| + STLDeleteElements(&url_fetchers_); |
| +} |
| + |
| +void HostPortAllocatorSession::ConfigReady(cricket::PortConfiguration* config) { |
| + // Filter out non-UDP relay ports, so that we don't try to use them. |
|
Wez
2012/04/27 23:23:21
This is filtering out non-UDP relay ports from the
Sergey Ulanov
2012/04/28 00:05:27
Correct. Clarified the comment.
|
| + for (cricket::PortConfiguration::RelayList::iterator relay = |
| + config->relays.begin(); relay != config->relays.end(); ++relay) { |
| + cricket::PortConfiguration::PortList filtered_ports; |
|
Wez
2012/04/27 23:23:21
If PortList is actually an stl::list then can't yo
Sergey Ulanov
2012/04/28 00:05:27
it is stl::vector
|
| + for (cricket::PortConfiguration::PortList::iterator port = |
| + relay->ports.begin(); port != relay->ports.end(); ++port) { |
| + if (port->proto == cricket::PROTO_UDP) { |
| + filtered_ports.push_back(*port); |
| + } |
| + } |
| + relay->ports = filtered_ports; |
| + } |
| + cricket::BasicPortAllocatorSession::ConfigReady(config); |
| +} |
| + |
| +void HostPortAllocatorSession::SendSessionRequest(const std::string& host, |
| + int port) { |
| + GURL url("https://" + host + ":" + base::IntToString(port) + |
| + GetSessionRequestUrl() + "&sn=1"); |
| + scoped_ptr<UrlFetcher> url_fetcher(new UrlFetcher(url, UrlFetcher::GET)); |
| + url_fetcher->SetRequestContext(url_context_); |
| + url_fetcher->SetHeader("X-Talk-Google-Relay-Auth", relay_token()); |
| + url_fetcher->SetHeader("X-Google-Relay-Auth", relay_token()); |
| + url_fetcher->SetHeader("X-Stream-Type", channel_name()); |
|
Wez
2012/04/27 23:23:21
nit: May as well set the headers in order.
Sergey Ulanov
2012/04/28 00:05:27
That's the order in which they are specified in ot
|
| + url_fetcher->Start(base::Bind(&HostPortAllocatorSession::OnRequestDone, |
| + base::Unretained(this), url_fetcher.get())); |
| + url_fetchers_.insert(url_fetcher.release()); |
| +} |
| + |
| +void HostPortAllocatorSession::OnRequestDone( |
| + UrlFetcher* url_fetcher, |
| + const net::URLRequestStatus& status, |
| + int response_code, |
| + const std::string& response) { |
| + url_fetchers_.erase(url_fetcher); |
| + delete url_fetcher; |
| + |
| + if (response_code != net::HTTP_OK) { |
| + LOG(WARNING) << "Received error when allocating relay session: " |
| + << response_code; |
| + TryCreateRelaySession(); |
|
Wez
2012/04/27 23:23:21
Doesn't this mean we'll keep trying to create rela
Sergey Ulanov
2012/04/28 00:05:27
No HttpPortAllocatorBase limits number of retries.
|
| + } |
| + |
| + ReceiveSessionResponse(response); |
| +} |
| + |
| +// static |
| +scoped_ptr<HostPortAllocator> HostPortAllocator::Create( |
| + const scoped_refptr<net::URLRequestContextGetter>& url_context, |
| + const NetworkSettings& network_settings) { |
| + scoped_ptr<talk_base::NetworkManager> network_manager( |
| + new talk_base::BasicNetworkManager()); |
| + scoped_ptr<talk_base::PacketSocketFactory> socket_factory( |
| + new talk_base::BasicPacketSocketFactory()); |
| + scoped_ptr<HostPortAllocator> result( |
| + new HostPortAllocator(url_context, network_manager.Pass(), |
| + socket_factory.Pass())); |
| + |
| + // We always use PseudoTcp to provide a reliable channel. However |
| + // when it is used together with TCP the performance is veryey bad |
|
Wez
2012/04/27 23:23:21
typo: veryey
Sergey Ulanov
2012/04/28 00:05:27
Done.
|
| + // so we explicitly disable TCP connections. |
| + int flags = cricket::PORTALLOCATOR_DISABLE_TCP; |
| + if (network_settings.nat_traversal_mode != |
| + NetworkSettings::NAT_TRAVERSAL_ENABLED) { |
| + flags |= cricket::PORTALLOCATOR_DISABLE_STUN | |
| + cricket::PORTALLOCATOR_DISABLE_RELAY; |
| + } |
| + result->set_flags(flags); |
| + result->SetPortRange(network_settings.min_port, |
| + network_settings.max_port); |
| + |
| + return result.Pass(); |
| +} |
| + |
| +HostPortAllocator::HostPortAllocator( |
| + const scoped_refptr<net::URLRequestContextGetter>& url_context, |
| + scoped_ptr<talk_base::NetworkManager> network_manager, |
| + scoped_ptr<talk_base::PacketSocketFactory> socket_factory) |
| + : HttpPortAllocatorBase(network_manager.get(), socket_factory.get(), ""), |
| + url_context_(url_context), |
| + network_manager_(network_manager.Pass()), |
| + socket_factory_(socket_factory.Pass()) { |
| +} |
| + |
| +HostPortAllocator::~HostPortAllocator() { |
| +} |
| + |
| +cricket::PortAllocatorSession* HostPortAllocator::CreateSession( |
| + const std::string& channel_name, |
| + int component) { |
| + return new HostPortAllocatorSession( |
| + this, channel_name, component, stun_hosts(), |
| + relay_hosts(), relay_token(), url_context_); |
| +} |
| + |
| +} // namespace remoting |