| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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_CLIENT_SOCKET_POOL_BASE_H_ | 5 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 6 #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 6 #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 Delegate() {} | 36 Delegate() {} |
| 37 virtual ~Delegate() {} | 37 virtual ~Delegate() {} |
| 38 | 38 |
| 39 // Alerts the delegate that the connection completed. | 39 // Alerts the delegate that the connection completed. |
| 40 virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0; | 40 virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0; |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(Delegate); | 43 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 // A |timeout_duration| of 0 corresponds to no timeout. |
| 46 ConnectJob(const std::string& group_name, | 47 ConnectJob(const std::string& group_name, |
| 47 const ClientSocketHandle* key_handle, | 48 const ClientSocketHandle* key_handle, |
| 49 base::TimeDelta timeout_duration, |
| 48 Delegate* delegate); | 50 Delegate* delegate); |
| 49 virtual ~ConnectJob(); | 51 virtual ~ConnectJob(); |
| 50 | 52 |
| 51 // Accessors | 53 // Accessors |
| 52 const std::string& group_name() const { return group_name_; } | 54 const std::string& group_name() const { return group_name_; } |
| 53 LoadState load_state() const { return load_state_; } | 55 LoadState load_state() const { return load_state_; } |
| 54 const ClientSocketHandle* key_handle() const { return key_handle_; } | 56 const ClientSocketHandle* key_handle() const { return key_handle_; } |
| 55 | 57 |
| 56 // Releases |socket_| to the client. On connection error, this should return | 58 // Releases |socket_| to the client. On connection error, this should return |
| 57 // NULL. | 59 // NULL. |
| 58 ClientSocket* ReleaseSocket() { return socket_.release(); } | 60 ClientSocket* ReleaseSocket() { return socket_.release(); } |
| 59 | 61 |
| 60 // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it | 62 // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it |
| 61 // cannot complete synchronously without blocking, or another net error code | 63 // cannot complete synchronously without blocking, or another net error code |
| 62 // on error. In asynchronous completion, the ConnectJob will notify | 64 // on error. In asynchronous completion, the ConnectJob will notify |
| 63 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous | 65 // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
| 64 // completion, ReleaseSocket() can be called to acquire the connected socket | 66 // completion, ReleaseSocket() can be called to acquire the connected socket |
| 65 // if it succeeded. | 67 // if it succeeded. |
| 66 virtual int Connect() = 0; | 68 int Connect(); |
| 67 | 69 |
| 68 protected: | 70 protected: |
| 69 void set_load_state(LoadState load_state) { load_state_ = load_state; } | 71 void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 70 void set_socket(ClientSocket* socket) { socket_.reset(socket); } | 72 void set_socket(ClientSocket* socket) { socket_.reset(socket); } |
| 71 ClientSocket* socket() { return socket_.get(); } | 73 ClientSocket* socket() { return socket_.get(); } |
| 72 Delegate* delegate() { return delegate_; } | 74 Delegate* delegate() { return delegate_; } |
| 73 | 75 |
| 74 private: | 76 private: |
| 77 virtual int ConnectInternal() = 0; |
| 78 |
| 79 // Alerts the delegate that the ConnectJob has timed out. |
| 80 void OnTimeout(); |
| 81 |
| 75 const std::string group_name_; | 82 const std::string group_name_; |
| 76 // Temporarily needed until we switch to late binding. | 83 // Temporarily needed until we switch to late binding. |
| 77 const ClientSocketHandle* const key_handle_; | 84 const ClientSocketHandle* const key_handle_; |
| 78 Delegate* const delegate_; | 85 const base::TimeDelta timeout_duration_; |
| 86 // Timer to abort jobs that take too long. |
| 87 base::OneShotTimer<ConnectJob> timer_; |
| 88 Delegate* delegate_; |
| 79 LoadState load_state_; | 89 LoadState load_state_; |
| 80 scoped_ptr<ClientSocket> socket_; | 90 scoped_ptr<ClientSocket> socket_; |
| 81 | 91 |
| 82 DISALLOW_COPY_AND_ASSIGN(ConnectJob); | 92 DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 83 }; | 93 }; |
| 84 | 94 |
| 85 // A ClientSocketPoolBase is used to restrict the number of sockets open at | 95 // A ClientSocketPoolBase is used to restrict the number of sockets open at |
| 86 // a time. It also maintains a list of idle persistent sockets. | 96 // a time. It also maintains a list of idle persistent sockets. |
| 87 // | 97 // |
| 88 class ClientSocketPoolBase | 98 class ClientSocketPoolBase |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Enables late binding of sockets. In this mode, socket requests are | 171 // Enables late binding of sockets. In this mode, socket requests are |
| 162 // decoupled from socket connection jobs. A socket request may initiate a | 172 // decoupled from socket connection jobs. A socket request may initiate a |
| 163 // socket connection job, but there is no guarantee that that socket | 173 // socket connection job, but there is no guarantee that that socket |
| 164 // connection will service the request (for example, a released socket may | 174 // connection will service the request (for example, a released socket may |
| 165 // service the request sooner, or a higher priority request may come in | 175 // service the request sooner, or a higher priority request may come in |
| 166 // afterward and receive the socket from the job). | 176 // afterward and receive the socket from the job). |
| 167 static void EnableLateBindingOfSockets(bool enabled); | 177 static void EnableLateBindingOfSockets(bool enabled); |
| 168 | 178 |
| 169 // For testing. | 179 // For testing. |
| 170 bool may_have_stalled_group() const { return may_have_stalled_group_; } | 180 bool may_have_stalled_group() const { return may_have_stalled_group_; } |
| 181 int NumConnectJobsInGroup(const std::string& group_name) const { |
| 182 return group_map_.find(group_name)->second.jobs.size(); |
| 183 } |
| 171 | 184 |
| 172 private: | 185 private: |
| 173 // Entry for a persistent socket which became idle at time |start_time|. | 186 // Entry for a persistent socket which became idle at time |start_time|. |
| 174 struct IdleSocket { | 187 struct IdleSocket { |
| 175 IdleSocket() : socket(NULL), used(false) {} | 188 IdleSocket() : socket(NULL), used(false) {} |
| 176 ClientSocket* socket; | 189 ClientSocket* socket; |
| 177 base::TimeTicks start_time; | 190 base::TimeTicks start_time; |
| 178 bool used; // Indicates whether or not the socket has been used yet. | 191 bool used; // Indicates whether or not the socket has been used yet. |
| 179 | 192 |
| 180 // An idle socket should be removed if it can't be reused, or has been idle | 193 // An idle socket should be removed if it can't be reused, or has been idle |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 // sockets that timed out or can't be reused. | 258 // sockets that timed out or can't be reused. |
| 246 void OnCleanupTimerFired() { | 259 void OnCleanupTimerFired() { |
| 247 CleanupIdleSockets(false); | 260 CleanupIdleSockets(false); |
| 248 } | 261 } |
| 249 | 262 |
| 250 // Removes the ConnectJob corresponding to |handle| from the | 263 // Removes the ConnectJob corresponding to |handle| from the |
| 251 // |connect_job_map_| or |connect_job_set_| depending on whether or not late | 264 // |connect_job_map_| or |connect_job_set_| depending on whether or not late |
| 252 // binding is enabled. |job| must be non-NULL when late binding is | 265 // binding is enabled. |job| must be non-NULL when late binding is |
| 253 // enabled. Also updates |group| if non-NULL. | 266 // enabled. Also updates |group| if non-NULL. |
| 254 void RemoveConnectJob(const ClientSocketHandle* handle, | 267 void RemoveConnectJob(const ClientSocketHandle* handle, |
| 255 ConnectJob* job, | 268 const ConnectJob* job, |
| 256 Group* group); | 269 Group* group); |
| 257 | 270 |
| 258 // Same as OnAvailableSocketSlot except it looks up the Group first to see if | 271 // Same as OnAvailableSocketSlot except it looks up the Group first to see if |
| 259 // it's there. | 272 // it's there. |
| 260 void MaybeOnAvailableSocketSlot(const std::string& group_name); | 273 void MaybeOnAvailableSocketSlot(const std::string& group_name); |
| 261 | 274 |
| 262 // Might delete the Group from |group_map_|. | 275 // Might delete the Group from |group_map_|. |
| 263 void OnAvailableSocketSlot(const std::string& group_name, Group* group); | 276 void OnAvailableSocketSlot(const std::string& group_name, Group* group); |
| 264 | 277 |
| 265 // Process a request from a group's pending_requests queue. | 278 // Process a request from a group's pending_requests queue. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 340 |
| 328 // Controls whether or not we use late binding of sockets. | 341 // Controls whether or not we use late binding of sockets. |
| 329 static bool g_late_binding; | 342 static bool g_late_binding; |
| 330 | 343 |
| 331 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 344 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 332 }; | 345 }; |
| 333 | 346 |
| 334 } // namespace net | 347 } // namespace net |
| 335 | 348 |
| 336 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 349 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |