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