Chromium Code Reviews| 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(net_log, &config_.nameservers); | |
| 25 } | 38 } |
| 26 | 39 |
| 27 int DnsSession::NextQueryId() const { | 40 int DnsSession::NextQueryId() const { |
| 28 return rand_callback_.Run(); | 41 return rand_callback_.Run(); |
| 29 } | 42 } |
| 30 | 43 |
| 31 int DnsSession::NextFirstServerIndex() { | 44 int DnsSession::NextFirstServerIndex() { |
| 32 int index = server_index_; | 45 int index = server_index_; |
| 33 if (config_.rotate) | 46 if (config_.rotate) |
| 34 server_index_ = (server_index_ + 1) % config_.nameservers.size(); | 47 server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
| 35 return index; | 48 return index; |
| 36 } | 49 } |
| 37 | 50 |
| 38 base::TimeDelta DnsSession::NextTimeout(int attempt) { | 51 base::TimeDelta DnsSession::NextTimeout(int attempt) { |
| 39 // The timeout doubles every full round (each nameserver once). | 52 // The timeout doubles every full round (each nameserver once). |
| 40 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 | 53 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 |
| 41 return config_.timeout * (1 << (attempt / config_.nameservers.size())); | 54 return config_.timeout * (1 << (attempt / config_.nameservers.size())); |
| 42 } | 55 } |
| 43 | 56 |
| 44 DnsSession::~DnsSession() {} | 57 // Allocate a socket, already connected to the server address. |
| 58 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket( | |
| 59 unsigned server_index, | |
| 60 const NetLog::Source& source) { | |
| 61 scoped_ptr<DatagramClientSocket> socket; | |
| 62 | |
| 63 socket = socket_pool_->AllocateSocket(server_index).Pass(); | |
|
szym
2012/09/26 11:16:12
You don't need Pass() here. It's an Rvalue.
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 64 if (!socket.get()) { | |
|
szym
2012/09/26 11:16:12
nit: no need for {}
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 65 return scoped_ptr<SocketLease>(NULL); | |
| 66 } | |
| 67 | |
| 68 socket->NetLog().BeginEvent( | |
| 69 NetLog::TYPE_SOCKET_IN_USE, | |
| 70 source.ToEventParametersCallback()); | |
| 71 | |
| 72 SocketLease* lease = new SocketLease(this, server_index, socket.Pass()); | |
| 73 return scoped_ptr<SocketLease>(lease); | |
| 74 } | |
| 75 | |
| 76 // Release a socket. | |
| 77 void DnsSession::FreeSocket( | |
| 78 unsigned server_index, | |
| 79 scoped_ptr<DatagramClientSocket> socket) { | |
| 80 CHECK(socket.get()); | |
|
szym
2012/09/26 11:16:12
I think DCHECK would suffice.
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 81 | |
| 82 socket->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE); | |
| 83 | |
| 84 socket_pool_->FreeSocket(server_index, socket.Pass()); | |
| 85 } | |
| 45 | 86 |
| 46 } // namespace net | 87 } // namespace net |
| 47 | |
| OLD | NEW |