Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/host/host_port_allocator.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "googleurl/src/gurl.h" | |
| 11 #include "net/http/http_status_code.h" | |
| 12 #include "net/url_request/url_request_context_getter.h" | |
| 13 #include "remoting/host/network_settings.h" | |
| 14 #include "third_party/libjingle/source/talk/base/basicpacketsocketfactory.h" | |
| 15 | |
| 16 namespace remoting { | |
| 17 | |
| 18 HostPortAllocatorSession::HostPortAllocatorSession( | |
| 19 cricket::HttpPortAllocatorBase* allocator, | |
| 20 const std::string& channel_name, | |
| 21 int component, | |
| 22 const std::vector<talk_base::SocketAddress>& stun_hosts, | |
| 23 const std::vector<std::string>& relay_hosts, | |
| 24 const std::string& relay, | |
| 25 const scoped_refptr<net::URLRequestContextGetter>& url_context) | |
| 26 : HttpPortAllocatorSessionBase( | |
| 27 allocator, channel_name, component, stun_hosts, relay_hosts, relay, ""), | |
| 28 url_context_(url_context) { | |
| 29 } | |
| 30 | |
| 31 HostPortAllocatorSession::~HostPortAllocatorSession() { | |
| 32 STLDeleteElements(&url_fetchers_); | |
| 33 } | |
| 34 | |
| 35 void HostPortAllocatorSession::ConfigReady(cricket::PortConfiguration* config) { | |
| 36 // 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.
| |
| 37 for (cricket::PortConfiguration::RelayList::iterator relay = | |
| 38 config->relays.begin(); relay != config->relays.end(); ++relay) { | |
| 39 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
| |
| 40 for (cricket::PortConfiguration::PortList::iterator port = | |
| 41 relay->ports.begin(); port != relay->ports.end(); ++port) { | |
| 42 if (port->proto == cricket::PROTO_UDP) { | |
| 43 filtered_ports.push_back(*port); | |
| 44 } | |
| 45 } | |
| 46 relay->ports = filtered_ports; | |
| 47 } | |
| 48 cricket::BasicPortAllocatorSession::ConfigReady(config); | |
| 49 } | |
| 50 | |
| 51 void HostPortAllocatorSession::SendSessionRequest(const std::string& host, | |
| 52 int port) { | |
| 53 GURL url("https://" + host + ":" + base::IntToString(port) + | |
| 54 GetSessionRequestUrl() + "&sn=1"); | |
| 55 scoped_ptr<UrlFetcher> url_fetcher(new UrlFetcher(url, UrlFetcher::GET)); | |
| 56 url_fetcher->SetRequestContext(url_context_); | |
| 57 url_fetcher->SetHeader("X-Talk-Google-Relay-Auth", relay_token()); | |
| 58 url_fetcher->SetHeader("X-Google-Relay-Auth", relay_token()); | |
| 59 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
| |
| 60 url_fetcher->Start(base::Bind(&HostPortAllocatorSession::OnRequestDone, | |
| 61 base::Unretained(this), url_fetcher.get())); | |
| 62 url_fetchers_.insert(url_fetcher.release()); | |
| 63 } | |
| 64 | |
| 65 void HostPortAllocatorSession::OnRequestDone( | |
| 66 UrlFetcher* url_fetcher, | |
| 67 const net::URLRequestStatus& status, | |
| 68 int response_code, | |
| 69 const std::string& response) { | |
| 70 url_fetchers_.erase(url_fetcher); | |
| 71 delete url_fetcher; | |
| 72 | |
| 73 if (response_code != net::HTTP_OK) { | |
| 74 LOG(WARNING) << "Received error when allocating relay session: " | |
| 75 << response_code; | |
| 76 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.
| |
| 77 } | |
| 78 | |
| 79 ReceiveSessionResponse(response); | |
| 80 } | |
| 81 | |
| 82 // static | |
| 83 scoped_ptr<HostPortAllocator> HostPortAllocator::Create( | |
| 84 const scoped_refptr<net::URLRequestContextGetter>& url_context, | |
| 85 const NetworkSettings& network_settings) { | |
| 86 scoped_ptr<talk_base::NetworkManager> network_manager( | |
| 87 new talk_base::BasicNetworkManager()); | |
| 88 scoped_ptr<talk_base::PacketSocketFactory> socket_factory( | |
| 89 new talk_base::BasicPacketSocketFactory()); | |
| 90 scoped_ptr<HostPortAllocator> result( | |
| 91 new HostPortAllocator(url_context, network_manager.Pass(), | |
| 92 socket_factory.Pass())); | |
| 93 | |
| 94 // We always use PseudoTcp to provide a reliable channel. However | |
| 95 // 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.
| |
| 96 // so we explicitly disable TCP connections. | |
| 97 int flags = cricket::PORTALLOCATOR_DISABLE_TCP; | |
| 98 if (network_settings.nat_traversal_mode != | |
| 99 NetworkSettings::NAT_TRAVERSAL_ENABLED) { | |
| 100 flags |= cricket::PORTALLOCATOR_DISABLE_STUN | | |
| 101 cricket::PORTALLOCATOR_DISABLE_RELAY; | |
| 102 } | |
| 103 result->set_flags(flags); | |
| 104 result->SetPortRange(network_settings.min_port, | |
| 105 network_settings.max_port); | |
| 106 | |
| 107 return result.Pass(); | |
| 108 } | |
| 109 | |
| 110 HostPortAllocator::HostPortAllocator( | |
| 111 const scoped_refptr<net::URLRequestContextGetter>& url_context, | |
| 112 scoped_ptr<talk_base::NetworkManager> network_manager, | |
| 113 scoped_ptr<talk_base::PacketSocketFactory> socket_factory) | |
| 114 : HttpPortAllocatorBase(network_manager.get(), socket_factory.get(), ""), | |
| 115 url_context_(url_context), | |
| 116 network_manager_(network_manager.Pass()), | |
| 117 socket_factory_(socket_factory.Pass()) { | |
| 118 } | |
| 119 | |
| 120 HostPortAllocator::~HostPortAllocator() { | |
| 121 } | |
| 122 | |
| 123 cricket::PortAllocatorSession* HostPortAllocator::CreateSession( | |
| 124 const std::string& channel_name, | |
| 125 int component) { | |
| 126 return new HostPortAllocatorSession( | |
| 127 this, channel_name, component, stun_hosts(), | |
| 128 relay_hosts(), relay_token(), url_context_); | |
| 129 } | |
| 130 | |
| 131 } // namespace remoting | |
| OLD | NEW |