| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/dns_session.h" | 5 #include "net/dns/dns_session.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/rand_util.h" |
| 10 #include "base/stl_util.h" |
| 9 #include "base/time.h" | 11 #include "base/time.h" |
| 10 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_errors.h" |
| 11 #include "net/dns/dns_config_service.h" | 14 #include "net/dns/dns_config_service.h" |
| 15 #include "net/dns/dns_socket_pool.h" |
| 12 #include "net/socket/client_socket_factory.h" | 16 #include "net/socket/client_socket_factory.h" |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 15 | 19 |
| 20 DnsSession::SocketLease::SocketLease(scoped_refptr<DnsSession> session, |
| 21 unsigned server_index, |
| 22 scoped_ptr<DatagramClientSocket> socket) |
| 23 : session_(session), server_index_(server_index), socket_(socket.Pass()) {} |
| 24 |
| 25 DnsSession::SocketLease::~SocketLease() { |
| 26 session_->FreeSocket(server_index_, socket_.Pass()); |
| 27 } |
| 28 |
| 16 DnsSession::DnsSession(const DnsConfig& config, | 29 DnsSession::DnsSession(const DnsConfig& config, |
| 17 ClientSocketFactory* factory, | 30 ClientSocketFactory* socket_factory, |
| 31 scoped_ptr<DnsSocketPool> socket_pool, |
| 18 const RandIntCallback& rand_int_callback, | 32 const RandIntCallback& rand_int_callback, |
| 19 NetLog* net_log) | 33 NetLog* net_log) |
| 20 : config_(config), | 34 : config_(config), |
| 21 socket_factory_(factory), | 35 socket_pool_(socket_pool.Pass()), |
| 22 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), | 36 rand_int_callback_(rand_int_callback), |
| 23 net_log_(net_log), | 37 net_log_(net_log), |
| 24 server_index_(0) { | 38 server_index_(0) { |
| 39 socket_pool_->Initialize(socket_factory, net_log, &config_.nameservers); |
| 25 } | 40 } |
| 26 | 41 |
| 27 int DnsSession::NextQueryId() const { | 42 int DnsSession::NextQueryId() const { |
| 28 return rand_callback_.Run(); | 43 /* NB: This callback is also used for selecting sockets from the pool. */ |
| 44 return rand_int_callback_.Run(0, kuint16max); |
| 29 } | 45 } |
| 30 | 46 |
| 31 int DnsSession::NextFirstServerIndex() { | 47 int DnsSession::NextFirstServerIndex() { |
| 32 int index = server_index_; | 48 int index = server_index_; |
| 33 if (config_.rotate) | 49 if (config_.rotate) |
| 34 server_index_ = (server_index_ + 1) % config_.nameservers.size(); | 50 server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
| 35 return index; | 51 return index; |
| 36 } | 52 } |
| 37 | 53 |
| 38 base::TimeDelta DnsSession::NextTimeout(int attempt) { | 54 base::TimeDelta DnsSession::NextTimeout(int attempt) { |
| 39 // The timeout doubles every full round (each nameserver once). | 55 // The timeout doubles every full round (each nameserver once). |
| 40 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 | 56 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 |
| 41 return config_.timeout * (1 << (attempt / config_.nameservers.size())); | 57 return config_.timeout * (1 << (attempt / config_.nameservers.size())); |
| 42 } | 58 } |
| 43 | 59 |
| 44 DnsSession::~DnsSession() {} | 60 // Allocate a socket, already connected to the server address. |
| 61 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket( |
| 62 unsigned server_index, |
| 63 const NetLog::Source& source) { |
| 64 scoped_ptr<DatagramClientSocket> socket; |
| 65 |
| 66 socket = socket_pool_->AllocateSocket(server_index).Pass(); |
| 67 if (!socket.get()) { |
| 68 return scoped_ptr<SocketLease>(NULL); |
| 69 } |
| 70 |
| 71 socket->NetLog().BeginEvent( |
| 72 NetLog::TYPE_SOCKET_IN_USE, |
| 73 source.ToEventParametersCallback()); |
| 74 |
| 75 SocketLease* lease = new SocketLease(this, server_index, socket.Pass()); |
| 76 return scoped_ptr<SocketLease>(lease); |
| 77 } |
| 78 |
| 79 // Release a socket. |
| 80 void DnsSession::FreeSocket( |
| 81 unsigned server_index, |
| 82 scoped_ptr<DatagramClientSocket> socket) { |
| 83 CHECK(socket.get()); |
| 84 |
| 85 socket->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE); |
| 86 |
| 87 socket_pool_->FreeSocket(server_index, socket.Pass()); |
| 88 } |
| 45 | 89 |
| 46 } // namespace net | 90 } // namespace net |
| 47 | |
| OLD | NEW |