summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_socket_pool.h
blob: 195393866d87afafa30dfbb81e6046e985c0593b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DNS_DNS_SOCKET_POOL_H_
#define NET_DNS_DNS_SOCKET_POOL_H_

#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/net_export.h"
#include "net/log/net_log.h"

namespace net {

class ClientSocketFactory;
class DatagramClientSocket;
class IPEndPoint;
class NetLog;
class StreamSocket;

// A DnsSocketPool is an abstraction layer around a ClientSocketFactory that
// allows preallocation, reuse, or other strategies to manage sockets connected
// to DNS servers.
class NET_EXPORT_PRIVATE DnsSocketPool {
 public:
  virtual ~DnsSocketPool() { }

  // Creates a DnsSocketPool that implements the default strategy for managing
  // sockets.  (This varies by platform; see DnsSocketPoolImpl in
  // dns_socket_pool.cc for details.)
  static scoped_ptr<DnsSocketPool> CreateDefault(
      ClientSocketFactory* factory);

  // Creates a DnsSocketPool that implements a "null" strategy -- no sockets are
  // preallocated, allocation requests are satisfied by calling the factory
  // directly, and returned sockets are deleted immediately.
  static scoped_ptr<DnsSocketPool> CreateNull(
      ClientSocketFactory* factory);

  // Initializes the DnsSocketPool.  |nameservers| is the list of nameservers
  // for which the DnsSocketPool will manage sockets; |net_log| is the NetLog
  // used when constructing sockets with the factory.
  //
  // Initialize may not be called more than once, and must be called before
  // calling AllocateSocket or FreeSocket.
  virtual void Initialize(
      const std::vector<IPEndPoint>* nameservers,
      NetLog* net_log) = 0;

  // Allocates a socket that is already connected to the nameserver referenced
  // by |server_index|.  May return a scoped_ptr to NULL if no sockets are
  // available to reuse and the factory fails to produce a socket (or produces
  // one on which Connect fails).
  virtual scoped_ptr<DatagramClientSocket> AllocateSocket(
      unsigned server_index) = 0;

  // Frees a socket allocated by AllocateSocket.  |server_index| must be the
  // same index passed to AllocateSocket.
  virtual void FreeSocket(
      unsigned server_index,
      scoped_ptr<DatagramClientSocket> socket) = 0;

  // Creates a StreamSocket from the factory for a transaction over TCP. These
  // sockets are not pooled.
  scoped_ptr<StreamSocket> CreateTCPSocket(
      unsigned server_index,
      const NetLog::Source& source);

 protected:
  DnsSocketPool(ClientSocketFactory* socket_factory);

  void InitializeInternal(
      const std::vector<IPEndPoint>* nameservers,
      NetLog* net_log);

  scoped_ptr<DatagramClientSocket> CreateConnectedSocket(
      unsigned server_index);

 private:
  ClientSocketFactory* socket_factory_;
  NetLog* net_log_;
  const std::vector<IPEndPoint>* nameservers_;
  bool initialized_;

  DISALLOW_COPY_AND_ASSIGN(DnsSocketPool);
};

} // namespace net

#endif // NET_DNS_DNS_SOCKET_POOL_H_