OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_ |
| 6 #define NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/time.h" |
| 17 #include "base/timer/timer.h" |
| 18 #include "net/socket/client_socket_pool.h" |
| 19 #include "net/socket/client_socket_pool_base.h" |
| 20 #include "net/socket/transport_client_socket_pool.h" |
| 21 |
| 22 namespace net { |
| 23 |
| 24 class ClientSocketFactory; |
| 25 class ClientSocketPoolHistograms; |
| 26 class HostResolver; |
| 27 class NetLog; |
| 28 |
| 29 typedef base::Callback<int(const AddressList&, const BoundNetLog& net_log)> |
| 30 OnHostResolutionCallback; |
| 31 |
| 32 class WebSocketEndpointLockManager; |
| 33 |
| 34 // WebSocketTransportConnectJob handles the host resolution necessary for socket |
| 35 // creation and the TCP connect. WebSocketTransportConnectJob |
| 36 // also has fallback logic for IPv6 connect() timeouts (which may happen due to |
| 37 // networks / routers with broken IPv6 support). Those timeouts take 20s, so |
| 38 // rather than make the user wait 20s for the timeout to fire, we use a fallback |
| 39 // timer (kIPv6FallbackTimerInMs) and start a connect() to a IPv4 address if the |
| 40 // timer fires. Then we race the IPv4 connect(s) against the IPv6 connect(s) |
| 41 // and use the socket that completes successfully first or fails last. |
| 42 class NET_EXPORT_PRIVATE WebSocketTransportConnectJob : public ConnectJob { |
| 43 public: |
| 44 WebSocketTransportConnectJob( |
| 45 const std::string& group_name, |
| 46 RequestPriority priority, |
| 47 const scoped_refptr<TransportSocketParams>& params, |
| 48 base::TimeDelta timeout_duration, |
| 49 const CompletionCallback& callback, |
| 50 ClientSocketFactory* client_socket_factory, |
| 51 HostResolver* host_resolver, |
| 52 ClientSocketHandle* handle, |
| 53 Delegate* delegate, |
| 54 NetLog* pool_net_log, |
| 55 const BoundNetLog& request_net_log); |
| 56 virtual ~WebSocketTransportConnectJob(); |
| 57 |
| 58 // Unlike normal socket pools, the WebSocketTransportClientPool uses |
| 59 // early-binding of sockets. |
| 60 ClientSocketHandle* handle() const { return handle_; } |
| 61 |
| 62 // Stash the callback from RequestSocket() here for convenience. |
| 63 const CompletionCallback& callback() const { return callback_; } |
| 64 |
| 65 const BoundNetLog& request_net_log() const { return request_net_log_; } |
| 66 |
| 67 // ConnectJob methods. |
| 68 virtual LoadState GetLoadState() const OVERRIDE; |
| 69 |
| 70 private: |
| 71 class SubJob; |
| 72 friend class SubJob; |
| 73 friend class TransportConnectJobHelper; |
| 74 friend class WebSocketEndpointLockManager; |
| 75 |
| 76 // Although it is not strictly necessary, it makes the code simpler if each |
| 77 // subjob knows what type it is. |
| 78 enum SubJobType { SUB_JOB_IPV4, SUB_JOB_IPV6 }; |
| 79 |
| 80 int DoResolveHost(); |
| 81 int DoResolveHostComplete(int result); |
| 82 int DoTransportConnect(); |
| 83 int DoTransportConnectComplete(int result); |
| 84 |
| 85 // Called back from a SubJob when it completes. |
| 86 void OnSubJobComplete(int result, SubJob* job); |
| 87 |
| 88 // Called from fallback_timer_ |
| 89 void StartIPv4JobAsync(); |
| 90 |
| 91 // Begins the host resolution and the TCP connect. Returns OK on success |
| 92 // and ERR_IO_PENDING if it cannot immediately service the request. |
| 93 // Otherwise, it returns a net error code. |
| 94 virtual int ConnectInternal() OVERRIDE; |
| 95 |
| 96 TransportConnectJobHelper data_; |
| 97 |
| 98 // The addresses are divided into IPv4 and IPv6, which are performed partially |
| 99 // in parallel. If the list of IPv6 addresses is non-empty, then the IPv6 jobs |
| 100 // go first, followed after |kIPv6FallbackTimerInMs| by the IPV4 |
| 101 // addresses. First sub-job to establish a connection wins. |
| 102 scoped_ptr<SubJob> ipv4_job_; |
| 103 scoped_ptr<SubJob> ipv6_job_; |
| 104 |
| 105 base::OneShotTimer<WebSocketTransportConnectJob> fallback_timer_; |
| 106 TransportConnectJobHelper::ConnectionLatencyHistogram race_result_; |
| 107 ClientSocketHandle* const handle_; |
| 108 CompletionCallback callback_; |
| 109 BoundNetLog request_net_log_; |
| 110 |
| 111 bool had_ipv4_; |
| 112 bool had_ipv6_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportConnectJob); |
| 115 }; |
| 116 |
| 117 class NET_EXPORT_PRIVATE WebSocketTransportClientSocketPool |
| 118 : public TransportClientSocketPool { |
| 119 public: |
| 120 WebSocketTransportClientSocketPool(int max_sockets, |
| 121 int max_sockets_per_group, |
| 122 ClientSocketPoolHistograms* histograms, |
| 123 HostResolver* host_resolver, |
| 124 ClientSocketFactory* client_socket_factory, |
| 125 NetLog* net_log); |
| 126 |
| 127 virtual ~WebSocketTransportClientSocketPool(); |
| 128 |
| 129 // Allow another connection to be started to the IPEndPoint that this |handle| |
| 130 // is connected to. Used when the WebSocket handshake completes successfully. |
| 131 static void UnlockEndpoint(ClientSocketHandle* handle); |
| 132 |
| 133 // ClientSocketPool implementation. |
| 134 virtual int RequestSocket(const std::string& group_name, |
| 135 const void* resolve_info, |
| 136 RequestPriority priority, |
| 137 ClientSocketHandle* handle, |
| 138 const CompletionCallback& callback, |
| 139 const BoundNetLog& net_log) OVERRIDE; |
| 140 virtual void RequestSockets(const std::string& group_name, |
| 141 const void* params, |
| 142 int num_sockets, |
| 143 const BoundNetLog& net_log) OVERRIDE; |
| 144 virtual void CancelRequest(const std::string& group_name, |
| 145 ClientSocketHandle* handle) OVERRIDE; |
| 146 virtual void ReleaseSocket(const std::string& group_name, |
| 147 scoped_ptr<StreamSocket> socket, |
| 148 int id) OVERRIDE; |
| 149 virtual void FlushWithError(int error) OVERRIDE; |
| 150 virtual void CloseIdleSockets() OVERRIDE; |
| 151 virtual int IdleSocketCount() const OVERRIDE; |
| 152 virtual int IdleSocketCountInGroup(const std::string& group_name) const |
| 153 OVERRIDE; |
| 154 virtual LoadState GetLoadState(const std::string& group_name, |
| 155 const ClientSocketHandle* handle) const |
| 156 OVERRIDE; |
| 157 virtual base::DictionaryValue* GetInfoAsValue(const std::string& name, |
| 158 const std::string& type, |
| 159 bool include_nested_pools) const |
| 160 OVERRIDE; |
| 161 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; |
| 162 virtual ClientSocketPoolHistograms* histograms() const OVERRIDE; |
| 163 |
| 164 // HigherLayeredPool implementation. |
| 165 virtual bool IsStalled() const OVERRIDE; |
| 166 |
| 167 private: |
| 168 class ConnectJobDelegate : public ConnectJob::Delegate { |
| 169 public: |
| 170 explicit ConnectJobDelegate(WebSocketTransportClientSocketPool* owner); |
| 171 virtual ~ConnectJobDelegate(); |
| 172 |
| 173 virtual void OnConnectJobComplete(int result, ConnectJob* job) OVERRIDE; |
| 174 |
| 175 private: |
| 176 WebSocketTransportClientSocketPool* owner_; |
| 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(ConnectJobDelegate); |
| 179 }; |
| 180 friend class ConnectJobDelegate; |
| 181 typedef std::map<const ClientSocketHandle*, WebSocketTransportConnectJob*> |
| 182 PendingConnectsMap; |
| 183 |
| 184 void OnConnectJobComplete(int result, WebSocketTransportConnectJob* job); |
| 185 void InvokeUserCallbackLater(ClientSocketHandle* handle, |
| 186 const CompletionCallback& callback, |
| 187 int rv); |
| 188 void InvokeUserCallback(ClientSocketHandle* handle, |
| 189 const CompletionCallback& callback, |
| 190 int rv); |
| 191 bool ReachedMaxSocketsLimit() const; |
| 192 void HandOutSocket(scoped_ptr<StreamSocket> socket, |
| 193 const LoadTimingInfo::ConnectTiming& connect_timing, |
| 194 ClientSocketHandle* handle, |
| 195 const BoundNetLog& net_log); |
| 196 void AddJob(ClientSocketHandle* handle, |
| 197 scoped_ptr<WebSocketTransportConnectJob> connect_job); |
| 198 bool DeleteJob(ClientSocketHandle* handle); |
| 199 void CancelAllConnectJobs(); |
| 200 const WebSocketTransportConnectJob* LookupConnectJob( |
| 201 const ClientSocketHandle* handle) const; |
| 202 |
| 203 ConnectJobDelegate connect_job_delegate_; |
| 204 std::set<const ClientSocketHandle*> pending_callbacks_; |
| 205 PendingConnectsMap pending_connects_; |
| 206 ClientSocketPoolHistograms* const histograms_; |
| 207 NetLog* const pool_net_log_; |
| 208 ClientSocketFactory* const client_socket_factory_; |
| 209 HostResolver* const host_resolver_; |
| 210 const int max_sockets_; |
| 211 int handed_out_socket_count_; |
| 212 bool is_stalled_; |
| 213 |
| 214 base::WeakPtrFactory<WebSocketTransportClientSocketPool> weak_factory_; |
| 215 |
| 216 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportClientSocketPool); |
| 217 }; |
| 218 |
| 219 } // namespace net |
| 220 |
| 221 #endif // NET_SOCKET_WEBSOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |