OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ | 5 #ifndef NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ |
6 #define NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ | 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 void Initialize(RequestPriority priority, const GURL& referrer, | 41 void Initialize(RequestPriority priority, const GURL& referrer, |
42 bool disable_resolver_cache); | 42 bool disable_resolver_cache); |
43 | 43 |
44 HostResolver::RequestInfo destination_; | 44 HostResolver::RequestInfo destination_; |
45 | 45 |
46 DISALLOW_COPY_AND_ASSIGN(TCPSocketParams); | 46 DISALLOW_COPY_AND_ASSIGN(TCPSocketParams); |
47 }; | 47 }; |
48 | 48 |
49 // TCPConnectJob handles the host resolution necessary for socket creation | 49 // TCPConnectJob handles the host resolution necessary for socket creation |
50 // and the tcp connect. | 50 // and the transport (likely TCP) connect. TCPConnectJob also has fallback |
| 51 // logic for IPv6 connect() timeouts (which may happen due to networks / routers |
| 52 // with broken IPv6 support). Those timeouts take 20s, so rather than make the |
| 53 // user wait 20s for the timeout to fire, we use a fallback timer |
| 54 // (kIPv6FallbackTimerInMs) and start a connect() to a IPv4 address if the timer |
| 55 // fires. Then we race the IPv4 connect() against the IPv6 connect() (which has |
| 56 // a headstart) and return the one that completes first to the socket pool. |
51 class TCPConnectJob : public ConnectJob { | 57 class TCPConnectJob : public ConnectJob { |
52 public: | 58 public: |
53 TCPConnectJob(const std::string& group_name, | 59 TCPConnectJob(const std::string& group_name, |
54 const scoped_refptr<TCPSocketParams>& params, | 60 const scoped_refptr<TCPSocketParams>& params, |
55 base::TimeDelta timeout_duration, | 61 base::TimeDelta timeout_duration, |
56 ClientSocketFactory* client_socket_factory, | 62 ClientSocketFactory* client_socket_factory, |
57 HostResolver* host_resolver, | 63 HostResolver* host_resolver, |
58 Delegate* delegate, | 64 Delegate* delegate, |
59 NetLog* net_log); | 65 NetLog* net_log); |
60 virtual ~TCPConnectJob(); | 66 virtual ~TCPConnectJob(); |
61 | 67 |
62 // ConnectJob methods. | 68 // ConnectJob methods. |
63 virtual LoadState GetLoadState() const; | 69 virtual LoadState GetLoadState() const; |
64 | 70 |
| 71 // Makes |addrlist| start with an IPv4 address if |addrlist| contains any |
| 72 // IPv4 address. |
| 73 // |
| 74 // WARNING: this method should only be used to implement the prefer-IPv4 |
| 75 // hack. It is a public method for the unit tests. |
| 76 static void MakeAddrListStartWithIPv4(AddressList* addrlist); |
| 77 |
| 78 static const int kIPv6FallbackTimerInMs; |
| 79 |
65 private: | 80 private: |
66 enum State { | 81 enum State { |
67 STATE_RESOLVE_HOST, | 82 STATE_RESOLVE_HOST, |
68 STATE_RESOLVE_HOST_COMPLETE, | 83 STATE_RESOLVE_HOST_COMPLETE, |
69 STATE_TCP_CONNECT, | 84 STATE_TCP_CONNECT, |
70 STATE_TCP_CONNECT_COMPLETE, | 85 STATE_TCP_CONNECT_COMPLETE, |
71 STATE_NONE, | 86 STATE_NONE, |
72 }; | 87 }; |
73 | 88 |
74 void OnIOComplete(int result); | 89 void OnIOComplete(int result); |
75 | 90 |
76 // Runs the state transition loop. | 91 // Runs the state transition loop. |
77 int DoLoop(int result); | 92 int DoLoop(int result); |
78 | 93 |
79 int DoResolveHost(); | 94 int DoResolveHost(); |
80 int DoResolveHostComplete(int result); | 95 int DoResolveHostComplete(int result); |
81 int DoTCPConnect(); | 96 int DoTCPConnect(); |
82 int DoTCPConnectComplete(int result); | 97 int DoTCPConnectComplete(int result); |
83 | 98 |
| 99 // Not part of the state machine. |
| 100 void DoIPv6FallbackTCPConnect(); |
| 101 void DoIPv6FallbackTCPConnectComplete(int result); |
| 102 |
84 // Begins the host resolution and the TCP connect. Returns OK on success | 103 // Begins the host resolution and the TCP connect. Returns OK on success |
85 // and ERR_IO_PENDING if it cannot immediately service the request. | 104 // and ERR_IO_PENDING if it cannot immediately service the request. |
86 // Otherwise, it returns a net error code. | 105 // Otherwise, it returns a net error code. |
87 virtual int ConnectInternal(); | 106 virtual int ConnectInternal(); |
88 | 107 |
89 scoped_refptr<TCPSocketParams> params_; | 108 scoped_refptr<TCPSocketParams> params_; |
90 ClientSocketFactory* const client_socket_factory_; | 109 ClientSocketFactory* const client_socket_factory_; |
91 CompletionCallbackImpl<TCPConnectJob> callback_; | 110 CompletionCallbackImpl<TCPConnectJob> callback_; |
92 SingleRequestHostResolver resolver_; | 111 SingleRequestHostResolver resolver_; |
93 AddressList addresses_; | 112 AddressList addresses_; |
94 State next_state_; | 113 State next_state_; |
95 | 114 |
96 // The time Connect() was called. | 115 // The time Connect() was called. |
97 base::TimeTicks start_time_; | 116 base::TimeTicks start_time_; |
98 | 117 |
99 // The time the connect was started (after DNS finished). | 118 // The time the connect was started (after DNS finished). |
100 base::TimeTicks connect_start_time_; | 119 base::TimeTicks connect_start_time_; |
101 | 120 |
| 121 scoped_ptr<ClientSocket> transport_socket_; |
| 122 |
| 123 scoped_ptr<ClientSocket> fallback_transport_socket_; |
| 124 scoped_ptr<AddressList> fallback_addresses_; |
| 125 CompletionCallbackImpl<TCPConnectJob> fallback_callback_; |
| 126 base::TimeTicks fallback_connect_start_time_; |
| 127 base::OneShotTimer<TCPConnectJob> fallback_timer_; |
| 128 |
102 DISALLOW_COPY_AND_ASSIGN(TCPConnectJob); | 129 DISALLOW_COPY_AND_ASSIGN(TCPConnectJob); |
103 }; | 130 }; |
104 | 131 |
105 class TCPClientSocketPool : public ClientSocketPool { | 132 class TCPClientSocketPool : public ClientSocketPool { |
106 public: | 133 public: |
107 TCPClientSocketPool( | 134 TCPClientSocketPool( |
108 int max_sockets, | 135 int max_sockets, |
109 int max_sockets_per_group, | 136 int max_sockets_per_group, |
110 ClientSocketPoolHistograms* histograms, | 137 ClientSocketPoolHistograms* histograms, |
111 HostResolver* host_resolver, | 138 HostResolver* host_resolver, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 PoolBase base_; | 216 PoolBase base_; |
190 | 217 |
191 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketPool); | 218 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketPool); |
192 }; | 219 }; |
193 | 220 |
194 REGISTER_SOCKET_PARAMS_FOR_POOL(TCPClientSocketPool, TCPSocketParams); | 221 REGISTER_SOCKET_PARAMS_FOR_POOL(TCPClientSocketPool, TCPSocketParams); |
195 | 222 |
196 } // namespace net | 223 } // namespace net |
197 | 224 |
198 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ | 225 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |