Chromium Code Reviews| 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_SESSION_H_ | 5 #ifndef NET_DNS_DNS_SESSION_H_ |
| 6 #define NET_DNS_DNS_SESSION_H_ | 6 #define NET_DNS_DNS_SESSION_H_ |
| 7 | 7 |
| 8 #include <vector> | |
| 9 | |
| 8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/time.h" | 12 #include "base/time.h" |
| 10 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
| 11 #include "net/base/rand_callback.h" | 14 #include "net/base/rand_callback.h" |
| 12 #include "net/dns/dns_config_service.h" | 15 #include "net/dns/dns_config_service.h" |
| 16 #include "net/udp/datagram_client_socket.h" | |
| 13 | 17 |
| 14 namespace net { | 18 namespace net { |
| 15 | 19 |
| 16 class ClientSocketFactory; | 20 class ClientSocketFactory; |
| 17 class NetLog; | 21 class NetLog; |
| 18 | 22 |
| 19 // Session parameters and state shared between DNS transactions. | 23 // Session parameters and state shared between DNS transactions. |
| 20 // Ref-counted so that DnsClient::Request can keep working in absence of | 24 // Ref-counted so that DnsClient::Request can keep working in absence of |
| 21 // DnsClient. A DnsSession must be recreated when DnsConfig changes. | 25 // DnsClient. A DnsSession must be recreated when DnsConfig changes. |
| 22 class NET_EXPORT_PRIVATE DnsSession | 26 class NET_EXPORT_PRIVATE DnsSession |
| 23 : NON_EXPORTED_BASE(public base::RefCounted<DnsSession>) { | 27 : NON_EXPORTED_BASE(public base::RefCounted<DnsSession>) { |
| 24 public: | 28 public: |
| 29 class SocketPool; | |
| 30 | |
| 25 typedef base::Callback<int()> RandCallback; | 31 typedef base::Callback<int()> RandCallback; |
| 26 | 32 |
| 33 //typedef DatagramClientSocket* SocketFactoryFunction(const IPEndPoint&); | |
| 34 //typedef base::Callback<SocketFactoryFunction> SocketFactoryCallback; | |
|
szym
2012/09/10 19:31:00
What are these comments for?
| |
| 35 | |
| 36 typedef DatagramClientSocket* EndPointSocketFactoryFunction(void); | |
|
szym
2012/09/10 19:31:00
Do you really need this typedef? Is it used only i
| |
| 37 typedef base::Callback<EndPointSocketFactoryFunction> | |
| 38 EndPointSocketFactoryCallback; | |
| 39 | |
| 40 typedef SocketPool* SocketPoolFactoryFunction( | |
| 41 const EndPointSocketFactoryCallback&); | |
| 42 typedef base::Callback<SocketPoolFactoryFunction> SocketPoolFactoryCallback; | |
|
szym
2012/09/10 19:31:00
A factory function is an unusual pattern. I think
| |
| 43 | |
| 44 class SocketLease { | |
| 45 public: | |
| 46 SocketLease(scoped_refptr<DnsSession> session, | |
| 47 int server_index, | |
| 48 scoped_ptr<DatagramClientSocket> socket); | |
| 49 ~SocketLease(); | |
| 50 | |
| 51 DatagramClientSocket* socket() { return socket_.get(); } | |
| 52 | |
| 53 private: | |
| 54 scoped_refptr<DnsSession> session_; | |
| 55 int server_index_; | |
| 56 scoped_ptr<DatagramClientSocket> socket_; | |
| 57 }; | |
| 58 | |
| 59 class SocketPool { | |
| 60 public: | |
| 61 virtual scoped_ptr<DatagramClientSocket> GetSocket() = 0; | |
| 62 virtual void PutSocket(scoped_ptr<DatagramClientSocket> socket) = 0; | |
|
szym
2012/09/10 19:31:00
I think AllocateSocket/ReleaseSocket would be more
| |
| 63 }; | |
| 64 | |
| 65 // Use the built-in socket pool class with the default size | |
|
szym
2012/09/10 19:31:00
nit: End sentences with periods.
| |
| 27 DnsSession(const DnsConfig& config, | 66 DnsSession(const DnsConfig& config, |
| 28 ClientSocketFactory* factory, | 67 ClientSocketFactory* socket_factory, |
| 29 const RandIntCallback& rand_int_callback, | 68 const RandIntCallback& rand_int_callback, |
| 30 NetLog* net_log); | 69 NetLog* net_log); |
| 31 | 70 |
| 71 // Use the built-in socket pool class, but with a custom size | |
| 72 DnsSession(const DnsConfig& config, | |
| 73 ClientSocketFactory* socket_factory, | |
| 74 const RandIntCallback& rand_int_callback, | |
| 75 int pool_size, | |
| 76 NetLog* net_log); | |
|
szym
2012/09/10 19:31:00
What do you need this constructor for? If only for
| |
| 77 | |
| 78 // Use a custom socket pool factory | |
| 79 DnsSession(const DnsConfig& config, | |
| 80 ClientSocketFactory* socket_factory, | |
| 81 const RandIntCallback& rand_int_callback, | |
| 82 const SocketPoolFactoryCallback& pool_factory, | |
| 83 NetLog* net_log); | |
| 84 | |
| 32 const DnsConfig& config() const { return config_; } | 85 const DnsConfig& config() const { return config_; } |
| 33 NetLog* net_log() const { return net_log_; } | 86 NetLog* net_log() const { return net_log_; } |
| 34 | 87 |
| 35 ClientSocketFactory* socket_factory() { return socket_factory_; } | |
| 36 | |
| 37 // Return the next random query ID. | 88 // Return the next random query ID. |
| 38 int NextQueryId() const; | 89 int NextQueryId() const; |
| 39 | 90 |
| 40 // Return the index of the first configured server to use on first attempt. | 91 // Return the index of the first configured server to use on first attempt. |
| 41 int NextFirstServerIndex(); | 92 int NextFirstServerIndex(); |
| 42 | 93 |
| 43 // Return the timeout for the next query. | 94 // Return the timeout for the next query. |
| 44 base::TimeDelta NextTimeout(int attempt); | 95 base::TimeDelta NextTimeout(int attempt); |
| 45 | 96 |
| 97 // Allocate a socket, already connected to the server address. | |
| 98 // TODO(szym): add NetLog param? | |
| 99 scoped_ptr<SocketLease> AllocateSocket(int server_index); | |
| 100 | |
| 46 private: | 101 private: |
| 47 friend class base::RefCounted<DnsSession>; | 102 friend class base::RefCounted<DnsSession>; |
| 48 ~DnsSession(); | 103 ~DnsSession(); |
| 49 | 104 |
| 105 DatagramClientSocket* CreateConnectedSocket(const IPEndPoint& endpoint); | |
| 106 | |
| 107 class SocketPoolImpl : public SocketPool { | |
| 108 public: | |
| 109 static SocketPool* CreateSocketPool( | |
| 110 unsigned pool_size, | |
| 111 const RandIntCallback& rand_int_callback, | |
| 112 const EndPointSocketFactoryCallback& factory_callback); | |
| 113 ~SocketPoolImpl(); | |
| 114 virtual scoped_ptr<DatagramClientSocket> GetSocket() OVERRIDE; | |
| 115 virtual void PutSocket(scoped_ptr<DatagramClientSocket> socket) OVERRIDE; | |
| 116 | |
| 117 private: | |
| 118 SocketPoolImpl( | |
| 119 unsigned pool_size, | |
| 120 const RandIntCallback& rand_int_callback, | |
| 121 const EndPointSocketFactoryCallback& factory_callback); | |
| 122 void FillPool(); | |
| 123 | |
| 124 std::vector<DatagramClientSocket*> pool_; | |
| 125 unsigned pool_size_; | |
| 126 RandIntCallback rand_int_callback_; | |
| 127 EndPointSocketFactoryCallback factory_callback_; | |
| 128 }; | |
| 129 | |
| 130 void CreatePools(const SocketPoolFactoryCallback& pool_factory_callback); | |
| 131 void CreateDefaultPools(int pool_size); | |
| 132 | |
| 133 // Release a socket. | |
| 134 void FreeSocket(int server_index, scoped_ptr<DatagramClientSocket> socket); | |
| 135 | |
| 50 const DnsConfig config_; | 136 const DnsConfig config_; |
| 51 ClientSocketFactory* socket_factory_; | 137 ClientSocketFactory* socket_factory_; |
| 52 RandCallback rand_callback_; | 138 RandIntCallback rand_int_callback_; |
| 53 NetLog* net_log_; | 139 NetLog* net_log_; |
| 54 | 140 |
| 141 std::vector<SocketPool*> pools_; | |
| 142 | |
| 55 // Current index into |config_.nameservers| to begin resolution with. | 143 // Current index into |config_.nameservers| to begin resolution with. |
| 56 int server_index_; | 144 int server_index_; |
| 57 | 145 |
| 58 // TODO(szym): Add current RTT estimate. | 146 // TODO(szym): Add current RTT estimate. |
| 59 // TODO(szym): Add TCP connection pool to support DNS over TCP. | 147 // TODO(szym): Add TCP connection pool to support DNS over TCP. |
| 60 // TODO(szym): Add UDP port pool to avoid NAT table overload. | |
| 61 | 148 |
| 62 DISALLOW_COPY_AND_ASSIGN(DnsSession); | 149 DISALLOW_COPY_AND_ASSIGN(DnsSession); |
| 63 }; | 150 }; |
| 64 | 151 |
| 65 } // namespace net | 152 } // namespace net |
| 66 | 153 |
| 67 #endif // NET_DNS_DNS_SESSION_H_ | 154 #endif // NET_DNS_DNS_SESSION_H_ |
| OLD | NEW |