| 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_socket_pool.h" | 5 #include "net/dns/dns_socket_pool.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 30 const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND; | 30 const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND; |
| 31 const unsigned kInitialPoolSize = 256; | 31 const unsigned kInitialPoolSize = 256; |
| 32 const unsigned kAllocateMinSize = 256; | 32 const unsigned kAllocateMinSize = 256; |
| 33 #else | 33 #else |
| 34 const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND; | 34 const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND; |
| 35 const unsigned kInitialPoolSize = 0; | 35 const unsigned kInitialPoolSize = 0; |
| 36 const unsigned kAllocateMinSize = 1; | 36 const unsigned kAllocateMinSize = 1; |
| 37 #endif | 37 #endif |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 DnsSocketPool::DnsSocketPool(ClientSocketFactory* socket_factory) | 41 DnsSocketPool::DnsSocketPool(ClientSocketFactory* socket_factory) |
| 42 : socket_factory_(socket_factory), | 42 : socket_factory_(socket_factory), |
| 43 net_log_(NULL), | 43 net_log_(NULL), |
| 44 nameservers_(NULL), | 44 nameservers_(NULL), |
| 45 initialized_(false) { | 45 initialized_(false) { |
| 46 } | 46 } |
| 47 | 47 |
| 48 void DnsSocketPool::InitializeInternal( | 48 void DnsSocketPool::InitializeInternal( |
| 49 const std::vector<IPEndPoint>* nameservers, | 49 const std::vector<IPEndPoint>* nameservers, |
| 50 NetLog* net_log) { | 50 NetLog* net_log) { |
| 51 DCHECK(nameservers); | 51 DCHECK(nameservers); |
| 52 DCHECK(!initialized_); | 52 DCHECK(!initialized_); |
| 53 | 53 |
| 54 net_log_ = net_log; | 54 net_log_ = net_log; |
| 55 nameservers_ = nameservers; | 55 nameservers_ = nameservers; |
| 56 initialized_ = true; | 56 initialized_ = true; |
| 57 } | 57 } |
| 58 | 58 |
| 59 scoped_ptr<StreamSocket> DnsSocketPool::CreateTCPSocket( | 59 scoped_ptr<StreamSocket> DnsSocketPool::CreateTCPSocket( |
| 60 unsigned server_index, | 60 unsigned server_index, |
| 61 const NetLog::Source& source) { | 61 const NetLog::Source& source) { |
| 62 DCHECK_LT(server_index, nameservers_->size()); | 62 DCHECK_LT(server_index, nameservers_->size()); |
| 63 | 63 |
| 64 return scoped_ptr<StreamSocket>( | 64 return scoped_ptr<StreamSocket>(socket_factory_->CreateTransportClientSocket( |
| 65 socket_factory_->CreateTransportClientSocket( | 65 AddressList((*nameservers_)[server_index]), net_log_, source)); |
| 66 AddressList((*nameservers_)[server_index]), net_log_, source)); | |
| 67 } | 66 } |
| 68 | 67 |
| 69 scoped_ptr<DatagramClientSocket> DnsSocketPool::CreateConnectedSocket( | 68 scoped_ptr<DatagramClientSocket> DnsSocketPool::CreateConnectedSocket( |
| 70 unsigned server_index) { | 69 unsigned server_index) { |
| 71 DCHECK_LT(server_index, nameservers_->size()); | 70 DCHECK_LT(server_index, nameservers_->size()); |
| 72 | 71 |
| 73 scoped_ptr<DatagramClientSocket> socket; | 72 scoped_ptr<DatagramClientSocket> socket; |
| 74 | 73 |
| 75 NetLog::Source no_source; | 74 NetLog::Source no_source; |
| 76 socket = socket_factory_->CreateDatagramClientSocket( | 75 socket = socket_factory_->CreateDatagramClientSocket( |
| 77 kBindType, base::Bind(&base::RandInt), net_log_, no_source); | 76 kBindType, base::Bind(&base::RandInt), net_log_, no_source); |
| 78 | 77 |
| 79 if (socket.get()) { | 78 if (socket.get()) { |
| 80 int rv = socket->Connect((*nameservers_)[server_index]); | 79 int rv = socket->Connect((*nameservers_)[server_index]); |
| 81 if (rv != OK) { | 80 if (rv != OK) { |
| 82 VLOG(1) << "Failed to connect socket: " << rv; | 81 VLOG(1) << "Failed to connect socket: " << rv; |
| 83 socket.reset(); | 82 socket.reset(); |
| 84 } | 83 } |
| 85 } else { | 84 } else { |
| 86 LOG(WARNING) << "Failed to create socket."; | 85 LOG(WARNING) << "Failed to create socket."; |
| 87 } | 86 } |
| 88 | 87 |
| 89 return socket.Pass(); | 88 return socket.Pass(); |
| 90 } | 89 } |
| 91 | 90 |
| 92 class NullDnsSocketPool : public DnsSocketPool { | 91 class NullDnsSocketPool : public DnsSocketPool { |
| 93 public: | 92 public: |
| 94 NullDnsSocketPool(ClientSocketFactory* factory) | 93 NullDnsSocketPool(ClientSocketFactory* factory) : DnsSocketPool(factory) {} |
| 95 : DnsSocketPool(factory) { | |
| 96 } | |
| 97 | 94 |
| 98 virtual void Initialize( | 95 virtual void Initialize(const std::vector<IPEndPoint>* nameservers, |
| 99 const std::vector<IPEndPoint>* nameservers, | 96 NetLog* net_log) OVERRIDE { |
| 100 NetLog* net_log) OVERRIDE { | |
| 101 InitializeInternal(nameservers, net_log); | 97 InitializeInternal(nameservers, net_log); |
| 102 } | 98 } |
| 103 | 99 |
| 104 virtual scoped_ptr<DatagramClientSocket> AllocateSocket( | 100 virtual scoped_ptr<DatagramClientSocket> AllocateSocket( |
| 105 unsigned server_index) OVERRIDE { | 101 unsigned server_index) OVERRIDE { |
| 106 return CreateConnectedSocket(server_index); | 102 return CreateConnectedSocket(server_index); |
| 107 } | 103 } |
| 108 | 104 |
| 109 virtual void FreeSocket( | 105 virtual void FreeSocket(unsigned server_index, |
| 110 unsigned server_index, | 106 scoped_ptr<DatagramClientSocket> socket) OVERRIDE {} |
| 111 scoped_ptr<DatagramClientSocket> socket) OVERRIDE { | |
| 112 } | |
| 113 | 107 |
| 114 private: | 108 private: |
| 115 DISALLOW_COPY_AND_ASSIGN(NullDnsSocketPool); | 109 DISALLOW_COPY_AND_ASSIGN(NullDnsSocketPool); |
| 116 }; | 110 }; |
| 117 | 111 |
| 118 // static | 112 // static |
| 119 scoped_ptr<DnsSocketPool> DnsSocketPool::CreateNull( | 113 scoped_ptr<DnsSocketPool> DnsSocketPool::CreateNull( |
| 120 ClientSocketFactory* factory) { | 114 ClientSocketFactory* factory) { |
| 121 return scoped_ptr<DnsSocketPool>(new NullDnsSocketPool(factory)); | 115 return scoped_ptr<DnsSocketPool>(new NullDnsSocketPool(factory)); |
| 122 } | 116 } |
| 123 | 117 |
| 124 class DefaultDnsSocketPool : public DnsSocketPool { | 118 class DefaultDnsSocketPool : public DnsSocketPool { |
| 125 public: | 119 public: |
| 126 DefaultDnsSocketPool(ClientSocketFactory* factory) | 120 DefaultDnsSocketPool(ClientSocketFactory* factory) |
| 127 : DnsSocketPool(factory) { | 121 : DnsSocketPool(factory) {}; |
| 128 }; | |
| 129 | 122 |
| 130 virtual ~DefaultDnsSocketPool(); | 123 virtual ~DefaultDnsSocketPool(); |
| 131 | 124 |
| 132 virtual void Initialize( | 125 virtual void Initialize(const std::vector<IPEndPoint>* nameservers, |
| 133 const std::vector<IPEndPoint>* nameservers, | 126 NetLog* net_log) OVERRIDE; |
| 134 NetLog* net_log) OVERRIDE; | |
| 135 | 127 |
| 136 virtual scoped_ptr<DatagramClientSocket> AllocateSocket( | 128 virtual scoped_ptr<DatagramClientSocket> AllocateSocket( |
| 137 unsigned server_index) OVERRIDE; | 129 unsigned server_index) OVERRIDE; |
| 138 | 130 |
| 139 virtual void FreeSocket( | 131 virtual void FreeSocket(unsigned server_index, |
| 140 unsigned server_index, | 132 scoped_ptr<DatagramClientSocket> socket) OVERRIDE; |
| 141 scoped_ptr<DatagramClientSocket> socket) OVERRIDE; | |
| 142 | 133 |
| 143 private: | 134 private: |
| 144 void FillPool(unsigned server_index, unsigned size); | 135 void FillPool(unsigned server_index, unsigned size); |
| 145 | 136 |
| 146 typedef std::vector<DatagramClientSocket*> SocketVector; | 137 typedef std::vector<DatagramClientSocket*> SocketVector; |
| 147 | 138 |
| 148 std::vector<SocketVector> pools_; | 139 std::vector<SocketVector> pools_; |
| 149 | 140 |
| 150 DISALLOW_COPY_AND_ASSIGN(DefaultDnsSocketPool); | 141 DISALLOW_COPY_AND_ASSIGN(DefaultDnsSocketPool); |
| 151 }; | 142 }; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 185 } |
| 195 | 186 |
| 196 unsigned socket_index = base::RandInt(0, pool.size() - 1); | 187 unsigned socket_index = base::RandInt(0, pool.size() - 1); |
| 197 DatagramClientSocket* socket = pool[socket_index]; | 188 DatagramClientSocket* socket = pool[socket_index]; |
| 198 pool[socket_index] = pool.back(); | 189 pool[socket_index] = pool.back(); |
| 199 pool.pop_back(); | 190 pool.pop_back(); |
| 200 | 191 |
| 201 return scoped_ptr<DatagramClientSocket>(socket); | 192 return scoped_ptr<DatagramClientSocket>(socket); |
| 202 } | 193 } |
| 203 | 194 |
| 204 void DefaultDnsSocketPool::FreeSocket( | 195 void DefaultDnsSocketPool::FreeSocket(unsigned server_index, |
| 205 unsigned server_index, | 196 scoped_ptr<DatagramClientSocket> socket) { |
| 206 scoped_ptr<DatagramClientSocket> socket) { | |
| 207 DCHECK_LT(server_index, pools_.size()); | 197 DCHECK_LT(server_index, pools_.size()); |
| 208 } | 198 } |
| 209 | 199 |
| 210 void DefaultDnsSocketPool::FillPool(unsigned server_index, unsigned size) { | 200 void DefaultDnsSocketPool::FillPool(unsigned server_index, unsigned size) { |
| 211 SocketVector& pool = pools_[server_index]; | 201 SocketVector& pool = pools_[server_index]; |
| 212 | 202 |
| 213 for (unsigned pool_index = pool.size(); pool_index < size; ++pool_index) { | 203 for (unsigned pool_index = pool.size(); pool_index < size; ++pool_index) { |
| 214 DatagramClientSocket* socket = | 204 DatagramClientSocket* socket = |
| 215 CreateConnectedSocket(server_index).release(); | 205 CreateConnectedSocket(server_index).release(); |
| 216 if (!socket) | 206 if (!socket) |
| 217 break; | 207 break; |
| 218 pool.push_back(socket); | 208 pool.push_back(socket); |
| 219 } | 209 } |
| 220 } | 210 } |
| 221 | 211 |
| 222 } // namespace net | 212 } // namespace net |
| OLD | NEW |