| 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" |
| 11 #include "net/base/ip_endpoint.h" | 11 #include "net/base/ip_endpoint.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/rand_callback.h" | 13 #include "net/base/rand_callback.h" |
| 14 #include "net/socket/client_socket_factory.h" | 14 #include "net/socket/client_socket_factory.h" |
| 15 #include "net/socket/stream_socket.h" | 15 #include "net/socket/stream_socket.h" |
| 16 #include "net/udp/datagram_client_socket.h" | 16 #include "net/udp/datagram_client_socket.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // When we initialize the SocketPool, we allocate kInitialPoolSize sockets. | 22 // When we initialize the SocketPool, we allocate kInitialPoolSize sockets. |
| 23 // When we allocate a socket, we ensure we have at least kAllocateMinSize | 23 // When we allocate a socket, we ensure we have at least kAllocateMinSize |
| 24 // sockets to choose from. When we free a socket, we retain it if we have | 24 // sockets to choose from. Freed sockets are not retained. |
| 25 // less than kRetainMaxSize sockets in the pool. | |
| 26 | 25 |
| 27 // On Windows, we can't request specific (random) ports, since that will | 26 // On Windows, we can't request specific (random) ports, since that will |
| 28 // trigger firewall prompts, so request default ones, but keep a pile of | 27 // trigger firewall prompts, so request default ones, but keep a pile of |
| 29 // them. Everywhere else, request fresh, random ports each time. | 28 // them. Everywhere else, request fresh, random ports each time. |
| 30 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 31 const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND; | 30 const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND; |
| 32 const unsigned kInitialPoolSize = 256; | 31 const unsigned kInitialPoolSize = 256; |
| 33 const unsigned kAllocateMinSize = 256; | 32 const unsigned kAllocateMinSize = 256; |
| 34 const unsigned kRetainMaxSize = 0; | |
| 35 #else | 33 #else |
| 36 const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND; | 34 const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND; |
| 37 const unsigned kInitialPoolSize = 0; | 35 const unsigned kInitialPoolSize = 0; |
| 38 const unsigned kAllocateMinSize = 1; | 36 const unsigned kAllocateMinSize = 1; |
| 39 const unsigned kRetainMaxSize = 0; | |
| 40 #endif | 37 #endif |
| 41 | 38 |
| 42 } // namespace | 39 } // namespace |
| 43 | 40 |
| 44 DnsSocketPool::DnsSocketPool(ClientSocketFactory* socket_factory) | 41 DnsSocketPool::DnsSocketPool(ClientSocketFactory* socket_factory) |
| 45 : socket_factory_(socket_factory), | 42 : socket_factory_(socket_factory), |
| 46 net_log_(NULL), | 43 net_log_(NULL), |
| 47 nameservers_(NULL), | 44 nameservers_(NULL), |
| 48 initialized_(false) { | 45 initialized_(false) { |
| 49 } | 46 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 pool[socket_index] = pool.back(); | 198 pool[socket_index] = pool.back(); |
| 202 pool.pop_back(); | 199 pool.pop_back(); |
| 203 | 200 |
| 204 return scoped_ptr<DatagramClientSocket>(socket); | 201 return scoped_ptr<DatagramClientSocket>(socket); |
| 205 } | 202 } |
| 206 | 203 |
| 207 void DefaultDnsSocketPool::FreeSocket( | 204 void DefaultDnsSocketPool::FreeSocket( |
| 208 unsigned server_index, | 205 unsigned server_index, |
| 209 scoped_ptr<DatagramClientSocket> socket) { | 206 scoped_ptr<DatagramClientSocket> socket) { |
| 210 DCHECK_LT(server_index, pools_.size()); | 207 DCHECK_LT(server_index, pools_.size()); |
| 211 | |
| 212 // In some builds, kRetainMaxSize will be 0 if we never reuse sockets. | |
| 213 // In that case, don't compile this code to avoid a "tautological | |
| 214 // comparison" warning from clang. | |
| 215 #if kRetainMaxSize > 0 | |
| 216 SocketVector& pool = pools_[server_index]; | |
| 217 if (pool.size() < kRetainMaxSize) | |
| 218 pool.push_back(socket.release()); | |
| 219 #endif | |
| 220 } | 208 } |
| 221 | 209 |
| 222 void DefaultDnsSocketPool::FillPool(unsigned server_index, unsigned size) { | 210 void DefaultDnsSocketPool::FillPool(unsigned server_index, unsigned size) { |
| 223 SocketVector& pool = pools_[server_index]; | 211 SocketVector& pool = pools_[server_index]; |
| 224 | 212 |
| 225 for (unsigned pool_index = pool.size(); pool_index < size; ++pool_index) { | 213 for (unsigned pool_index = pool.size(); pool_index < size; ++pool_index) { |
| 226 DatagramClientSocket* socket = | 214 DatagramClientSocket* socket = |
| 227 CreateConnectedSocket(server_index).release(); | 215 CreateConnectedSocket(server_index).release(); |
| 228 if (!socket) | 216 if (!socket) |
| 229 break; | 217 break; |
| 230 pool.push_back(socket); | 218 pool.push_back(socket); |
| 231 } | 219 } |
| 232 } | 220 } |
| 233 | 221 |
| 234 } // namespace net | 222 } // namespace net |
| OLD | NEW |