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 #ifndef NET_DNS_DNS_SOCKET_POOL_H_ | 5 #ifndef NET_DNS_DNS_SOCKET_POOL_H_ |
6 #define NET_DNS_DNS_SOCKET_POOL_H_ | 6 #define NET_DNS_DNS_SOCKET_POOL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 13 #include "net/base/rand_callback.h" |
13 #include "net/log/net_log.h" | 14 #include "net/log/net_log.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
17 class ClientSocketFactory; | 18 class ClientSocketFactory; |
18 class DatagramClientSocket; | 19 class DatagramClientSocket; |
19 class IPEndPoint; | 20 class IPEndPoint; |
20 class NetLog; | 21 class NetLog; |
21 class StreamSocket; | 22 class StreamSocket; |
22 | 23 |
23 // A DnsSocketPool is an abstraction layer around a ClientSocketFactory that | 24 // A DnsSocketPool is an abstraction layer around a ClientSocketFactory that |
24 // allows preallocation, reuse, or other strategies to manage sockets connected | 25 // allows preallocation, reuse, or other strategies to manage sockets connected |
25 // to DNS servers. | 26 // to DNS servers. |
26 class NET_EXPORT_PRIVATE DnsSocketPool { | 27 class NET_EXPORT_PRIVATE DnsSocketPool { |
27 public: | 28 public: |
28 virtual ~DnsSocketPool() { } | 29 virtual ~DnsSocketPool() { } |
29 | 30 |
30 // Creates a DnsSocketPool that implements the default strategy for managing | 31 // Creates a DnsSocketPool that implements the default strategy for managing |
31 // sockets. (This varies by platform; see DnsSocketPoolImpl in | 32 // sockets. (This varies by platform; see DnsSocketPoolImpl in |
32 // dns_socket_pool.cc for details.) | 33 // dns_socket_pool.cc for details.) |
33 static std::unique_ptr<DnsSocketPool> CreateDefault( | 34 static std::unique_ptr<DnsSocketPool> CreateDefault( |
34 ClientSocketFactory* factory); | 35 ClientSocketFactory* factory, |
| 36 const RandIntCallback& rand_int_callback); |
35 | 37 |
36 // Creates a DnsSocketPool that implements a "null" strategy -- no sockets are | 38 // Creates a DnsSocketPool that implements a "null" strategy -- no sockets are |
37 // preallocated, allocation requests are satisfied by calling the factory | 39 // preallocated, allocation requests are satisfied by calling the factory |
38 // directly, and returned sockets are deleted immediately. | 40 // directly, and returned sockets are deleted immediately. |
39 static std::unique_ptr<DnsSocketPool> CreateNull( | 41 static std::unique_ptr<DnsSocketPool> CreateNull( |
40 ClientSocketFactory* factory); | 42 ClientSocketFactory* factory, |
| 43 const RandIntCallback& rand_int_callback); |
41 | 44 |
42 // Initializes the DnsSocketPool. |nameservers| is the list of nameservers | 45 // Initializes the DnsSocketPool. |nameservers| is the list of nameservers |
43 // for which the DnsSocketPool will manage sockets; |net_log| is the NetLog | 46 // for which the DnsSocketPool will manage sockets; |net_log| is the NetLog |
44 // used when constructing sockets with the factory. | 47 // used when constructing sockets with the factory. |
45 // | 48 // |
46 // Initialize may not be called more than once, and must be called before | 49 // Initialize may not be called more than once, and must be called before |
47 // calling AllocateSocket or FreeSocket. | 50 // calling AllocateSocket or FreeSocket. |
48 virtual void Initialize( | 51 virtual void Initialize( |
49 const std::vector<IPEndPoint>* nameservers, | 52 const std::vector<IPEndPoint>* nameservers, |
50 NetLog* net_log) = 0; | 53 NetLog* net_log) = 0; |
51 | 54 |
52 // Allocates a socket that is already connected to the nameserver referenced | 55 // Allocates a socket that is already connected to the nameserver referenced |
53 // by |server_index|. May return a scoped_ptr to NULL if no sockets are | 56 // by |server_index|. May return a scoped_ptr to NULL if no sockets are |
54 // available to reuse and the factory fails to produce a socket (or produces | 57 // available to reuse and the factory fails to produce a socket (or produces |
55 // one on which Connect fails). | 58 // one on which Connect fails). |
56 virtual std::unique_ptr<DatagramClientSocket> AllocateSocket( | 59 virtual std::unique_ptr<DatagramClientSocket> AllocateSocket( |
57 unsigned server_index) = 0; | 60 unsigned server_index) = 0; |
58 | 61 |
59 // Frees a socket allocated by AllocateSocket. |server_index| must be the | 62 // Frees a socket allocated by AllocateSocket. |server_index| must be the |
60 // same index passed to AllocateSocket. | 63 // same index passed to AllocateSocket. |
61 virtual void FreeSocket(unsigned server_index, | 64 virtual void FreeSocket(unsigned server_index, |
62 std::unique_ptr<DatagramClientSocket> socket) = 0; | 65 std::unique_ptr<DatagramClientSocket> socket) = 0; |
63 | 66 |
64 // Creates a StreamSocket from the factory for a transaction over TCP. These | 67 // Creates a StreamSocket from the factory for a transaction over TCP. These |
65 // sockets are not pooled. | 68 // sockets are not pooled. |
66 std::unique_ptr<StreamSocket> CreateTCPSocket(unsigned server_index, | 69 std::unique_ptr<StreamSocket> CreateTCPSocket(unsigned server_index, |
67 const NetLog::Source& source); | 70 const NetLog::Source& source); |
68 | 71 |
69 protected: | 72 protected: |
70 DnsSocketPool(ClientSocketFactory* socket_factory); | 73 DnsSocketPool(ClientSocketFactory* socket_factory, |
| 74 const RandIntCallback& rand_int_callback); |
71 | 75 |
72 void InitializeInternal( | 76 void InitializeInternal( |
73 const std::vector<IPEndPoint>* nameservers, | 77 const std::vector<IPEndPoint>* nameservers, |
74 NetLog* net_log); | 78 NetLog* net_log); |
75 | 79 |
76 std::unique_ptr<DatagramClientSocket> CreateConnectedSocket( | 80 std::unique_ptr<DatagramClientSocket> CreateConnectedSocket( |
77 unsigned server_index); | 81 unsigned server_index); |
78 | 82 |
| 83 const RandIntCallback& rand_int_callback() const { |
| 84 return rand_int_callback_; |
| 85 } |
| 86 |
79 private: | 87 private: |
80 ClientSocketFactory* socket_factory_; | 88 ClientSocketFactory* socket_factory_; |
| 89 const RandIntCallback& rand_int_callback_; |
81 NetLog* net_log_; | 90 NetLog* net_log_; |
82 const std::vector<IPEndPoint>* nameservers_; | 91 const std::vector<IPEndPoint>* nameservers_; |
83 bool initialized_; | 92 bool initialized_; |
84 | 93 |
85 DISALLOW_COPY_AND_ASSIGN(DnsSocketPool); | 94 DISALLOW_COPY_AND_ASSIGN(DnsSocketPool); |
86 }; | 95 }; |
87 | 96 |
88 } // namespace net | 97 } // namespace net |
89 | 98 |
90 #endif // NET_DNS_DNS_SOCKET_POOL_H_ | 99 #endif // NET_DNS_DNS_SOCKET_POOL_H_ |
OLD | NEW |