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