| 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" | 
|  12 #include "net/socket/client_socket_factory.h" |  15 #include "net/dns/dns_socket_pool.h" | 
|  13  |  16  | 
|  14 namespace net { |  17 namespace net { | 
|  15  |  18  | 
 |  19 DnsSession::SocketLease::SocketLease(scoped_refptr<DnsSession> session, | 
 |  20                                      unsigned server_index, | 
 |  21                                      scoped_ptr<DatagramClientSocket> socket) | 
 |  22     : session_(session), server_index_(server_index), socket_(socket.Pass()) {} | 
 |  23  | 
 |  24 DnsSession::SocketLease::~SocketLease() { | 
 |  25   session_->FreeSocket(server_index_, socket_.Pass()); | 
 |  26 } | 
 |  27  | 
|  16 DnsSession::DnsSession(const DnsConfig& config, |  28 DnsSession::DnsSession(const DnsConfig& config, | 
|  17                        ClientSocketFactory* factory, |  29                        scoped_ptr<DnsSocketPool> socket_pool, | 
|  18                        const RandIntCallback& rand_int_callback, |  30                        const RandIntCallback& rand_int_callback, | 
|  19                        NetLog* net_log) |  31                        NetLog* net_log) | 
|  20     : config_(config), |  32     : config_(config), | 
|  21       socket_factory_(factory), |  33       socket_pool_(socket_pool.Pass()), | 
|  22       rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), |  34       rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), | 
|  23       net_log_(net_log), |  35       net_log_(net_log), | 
|  24       server_index_(0) { |  36       server_index_(0) { | 
 |  37   socket_pool_->Initialize(&config_.nameservers, net_log); | 
 |  38 } | 
 |  39  | 
 |  40 DnsSession::~DnsSession() { | 
|  25 } |  41 } | 
|  26  |  42  | 
|  27 int DnsSession::NextQueryId() const { |  43 int DnsSession::NextQueryId() const { | 
|  28   return rand_callback_.Run(); |  44   return rand_callback_.Run(); | 
|  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); | 
 |  67   if (!socket.get()) | 
 |  68     return scoped_ptr<SocketLease>(NULL); | 
 |  69  | 
 |  70   socket->NetLog().BeginEvent( | 
 |  71       NetLog::TYPE_SOCKET_IN_USE, | 
 |  72       source.ToEventParametersCallback()); | 
 |  73  | 
 |  74   SocketLease* lease = new SocketLease(this, server_index, socket.Pass()); | 
 |  75   return scoped_ptr<SocketLease>(lease); | 
 |  76 } | 
 |  77  | 
 |  78 // Release a socket. | 
 |  79 void DnsSession::FreeSocket( | 
 |  80     unsigned server_index, | 
 |  81     scoped_ptr<DatagramClientSocket> socket) { | 
 |  82   DCHECK(socket.get()); | 
 |  83  | 
 |  84   socket->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE); | 
 |  85  | 
 |  86   socket_pool_->FreeSocket(server_index, socket.Pass()); | 
 |  87 } | 
|  45  |  88  | 
|  46 }  // namespace net |  89 }  // namespace net | 
|  47  |  | 
| OLD | NEW |