1 // Copyright (c) 2006-2008 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_TCP_CLIENT_SOCKET_POOL_H_ 6 #define NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/ref_counted.h" 12 #include "base/scoped_ptr.h" 13 #include "base/time.h" 14 #include "base/timer.h" 15 #include "net/base/host_resolver.h" 16 #include "net/socket/client_socket_pool_base.h" 17 #include "net/socket/client_socket_pool.h" 18 19 namespace net { 20 21 class ClientSocketFactory; 22 23 // TCPConnectJob handles the host resolution necessary for socket creation 24 // and the tcp connect. 25 class TCPConnectJob : public ConnectJob { 26 public: 27 TCPConnectJob(const std::string& group_name, 28 const HostResolver::RequestInfo& resolve_info, 29 base::TimeDelta timeout_duration, 30 ClientSocketFactory* client_socket_factory, 31 HostResolver* host_resolver, 32 Delegate* delegate, 33 LoadLog* load_log); 34 virtual ~TCPConnectJob(); 35 36 // ConnectJob methods. 37 virtual LoadState GetLoadState() const; 38 39 private: 40 enum State { 41 kStateResolveHost, 42 kStateResolveHostComplete, 43 kStateTCPConnect, 44 kStateTCPConnectComplete, 45 kStateNone, 46 }; 47 48 // Begins the host resolution and the TCP connect. Returns OK on success 49 // and ERR_IO_PENDING if it cannot immediately service the request. 50 // Otherwise, it returns a net error code. 51 virtual int ConnectInternal(); 52 53 void OnIOComplete(int result); 54 55 // Runs the state transition loop. 56 int DoLoop(int result); 57 58 int DoResolveHost(); 59 int DoResolveHostComplete(int result); 60 int DoTCPConnect(); 61 int DoTCPConnectComplete(int result); 62 63 const HostResolver::RequestInfo resolve_info_; 64 ClientSocketFactory* const client_socket_factory_; 65 CompletionCallbackImpl<TCPConnectJob> callback_; 66 SingleRequestHostResolver resolver_; 67 AddressList addresses_; 68 State next_state_; 69 70 // The time Connect() was called. 71 base::TimeTicks start_time_; 72 73 // The time the connect was started (after DNS finished). 74 base::TimeTicks connect_start_time_; 75 76 DISALLOW_COPY_AND_ASSIGN(TCPConnectJob); 77 }; 78 79 class TCPClientSocketPool : public ClientSocketPool { 80 public: 81 TCPClientSocketPool( 82 int max_sockets, 83 int max_sockets_per_group, 84 HostResolver* host_resolver, 85 ClientSocketFactory* client_socket_factory, 86 NetworkChangeNotifier* network_change_notifier); 87 88 // ClientSocketPool methods: 89 90 virtual int RequestSocket(const std::string& group_name, 91 const void* resolve_info, 92 RequestPriority priority, 93 ClientSocketHandle* handle, 94 CompletionCallback* callback, 95 LoadLog* load_log); 96 97 virtual void CancelRequest(const std::string& group_name, 98 const ClientSocketHandle* handle); 99 100 virtual void ReleaseSocket(const std::string& group_name, 101 ClientSocket* socket); 102 103 virtual void CloseIdleSockets(); 104 105 virtual int IdleSocketCount() const { 106 return base_.idle_socket_count(); 107 } 108 109 virtual int IdleSocketCountInGroup(const std::string& group_name) const; 110 111 virtual LoadState GetLoadState(const std::string& group_name, 112 const ClientSocketHandle* handle) const; 113 114 protected: 115 virtual ~TCPClientSocketPool(); 116 117 private: 118 typedef ClientSocketPoolBase<HostResolver::RequestInfo> PoolBase; 119 120 class TCPConnectJobFactory 121 : public PoolBase::ConnectJobFactory { 122 public: 123 TCPConnectJobFactory(ClientSocketFactory* client_socket_factory, 124 HostResolver* host_resolver) 125 : client_socket_factory_(client_socket_factory), 126 host_resolver_(host_resolver) {} 127 128 virtual ~TCPConnectJobFactory() {} 129 130 // ClientSocketPoolBase::ConnectJobFactory methods. 131 132 virtual ConnectJob* NewConnectJob( 133 const std::string& group_name, 134 const PoolBase::Request& request, 135 ConnectJob::Delegate* delegate, 136 LoadLog* load_log) const; 137 138 private: 139 ClientSocketFactory* const client_socket_factory_; 140 const scoped_refptr<HostResolver> host_resolver_; 141 142 DISALLOW_COPY_AND_ASSIGN(TCPConnectJobFactory); 143 }; 144 145 PoolBase base_; 146 147 DISALLOW_COPY_AND_ASSIGN(TCPClientSocketPool); 148 }; 149 150 REGISTER_SOCKET_PARAMS_FOR_POOL(TCPClientSocketPool, HostResolver::RequestInfo) 151 152 } // namespace net 153 154 #endif // NET_SOCKET_TCP_CLIENT_SOCKET_POOL_H_ 155