| 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 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 const int kIdleTimeout = 300; // 5 minutes. | 27 const int kIdleTimeout = 300; // 5 minutes. |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 namespace net { | 31 namespace net { |
| 32 | 32 |
| 33 bool ClientSocketPoolBase::g_late_binding = false; | 33 bool ClientSocketPoolBase::g_late_binding = false; |
| 34 | 34 |
| 35 ConnectJob::ConnectJob(const std::string& group_name, | 35 ConnectJob::ConnectJob(const std::string& group_name, |
| 36 const ClientSocketHandle* key_handle, | 36 const ClientSocketHandle* key_handle, |
| 37 base::TimeDelta timeout_duration, |
| 37 Delegate* delegate) | 38 Delegate* delegate) |
| 38 : group_name_(group_name), | 39 : group_name_(group_name), |
| 39 key_handle_(key_handle), | 40 key_handle_(key_handle), |
| 41 timeout_duration_(timeout_duration), |
| 40 delegate_(delegate), | 42 delegate_(delegate), |
| 41 load_state_(LOAD_STATE_IDLE) { | 43 load_state_(LOAD_STATE_IDLE) { |
| 42 DCHECK(!group_name.empty()); | 44 DCHECK(!group_name.empty()); |
| 43 DCHECK(key_handle); | 45 DCHECK(key_handle); |
| 44 DCHECK(delegate); | 46 DCHECK(delegate); |
| 45 } | 47 } |
| 46 | 48 |
| 47 ConnectJob::~ConnectJob() {} | 49 ConnectJob::~ConnectJob() {} |
| 48 | 50 |
| 51 int ConnectJob::Connect() { |
| 52 if (timeout_duration_ != base::TimeDelta()) |
| 53 timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout); |
| 54 return ConnectInternal(); |
| 55 } |
| 56 |
| 57 void ConnectJob::OnTimeout() { |
| 58 // The delegate will delete |this|. |
| 59 Delegate *delegate = delegate_; |
| 60 delegate_ = NULL; |
| 61 delegate->OnConnectJobComplete(ERR_TIMED_OUT, this); |
| 62 } |
| 63 |
| 49 ClientSocketPoolBase::ClientSocketPoolBase( | 64 ClientSocketPoolBase::ClientSocketPoolBase( |
| 50 int max_sockets, | 65 int max_sockets, |
| 51 int max_sockets_per_group, | 66 int max_sockets_per_group, |
| 52 ConnectJobFactory* connect_job_factory) | 67 ConnectJobFactory* connect_job_factory) |
| 53 : idle_socket_count_(0), | 68 : idle_socket_count_(0), |
| 54 connecting_socket_count_(0), | 69 connecting_socket_count_(0), |
| 55 handed_out_socket_count_(0), | 70 handed_out_socket_count_(0), |
| 56 max_sockets_(max_sockets), | 71 max_sockets_(max_sockets), |
| 57 max_sockets_per_group_(max_sockets_per_group), | 72 max_sockets_per_group_(max_sockets_per_group), |
| 58 may_have_stalled_group_(false), | 73 may_have_stalled_group_(false), |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 const ClientSocketHandle* handle) { | 172 const ClientSocketHandle* handle) { |
| 158 CHECK(ContainsKey(group_map_, group_name)); | 173 CHECK(ContainsKey(group_map_, group_name)); |
| 159 | 174 |
| 160 Group& group = group_map_[group_name]; | 175 Group& group = group_map_[group_name]; |
| 161 | 176 |
| 162 // Search pending_requests for matching handle. | 177 // Search pending_requests for matching handle. |
| 163 RequestQueue::iterator it = group.pending_requests.begin(); | 178 RequestQueue::iterator it = group.pending_requests.begin(); |
| 164 for (; it != group.pending_requests.end(); ++it) { | 179 for (; it != group.pending_requests.end(); ++it) { |
| 165 if (it->handle == handle) { | 180 if (it->handle == handle) { |
| 166 group.pending_requests.erase(it); | 181 group.pending_requests.erase(it); |
| 182 if (g_late_binding && |
| 183 group.jobs.size() > group.pending_requests.size() + 1) { |
| 184 // TODO(willchan): Cancel the job in the earliest LoadState. |
| 185 RemoveConnectJob(handle, *group.jobs.begin(), &group); |
| 186 OnAvailableSocketSlot(group_name, &group); |
| 187 } |
| 167 return; | 188 return; |
| 168 } | 189 } |
| 169 } | 190 } |
| 170 | 191 |
| 171 if (!g_late_binding) { | 192 if (!g_late_binding) { |
| 172 // It's invalid to cancel a non-existent request. | 193 // It's invalid to cancel a non-existent request. |
| 173 CHECK(ContainsKey(group.connecting_requests, handle)); | 194 CHECK(ContainsKey(group.connecting_requests, handle)); |
| 174 | 195 |
| 175 RequestMap::iterator map_it = group.connecting_requests.find(handle); | 196 RequestMap::iterator map_it = group.connecting_requests.find(handle); |
| 176 if (map_it != group.connecting_requests.end()) { | 197 if (map_it != group.connecting_requests.end()) { |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 HandOutSocket(socket.release(), false /* not reused */, handle, &group); | 431 HandOutSocket(socket.release(), false /* not reused */, handle, &group); |
| 411 callback->Run(result); | 432 callback->Run(result); |
| 412 } | 433 } |
| 413 } | 434 } |
| 414 | 435 |
| 415 void ClientSocketPoolBase::EnableLateBindingOfSockets(bool enabled) { | 436 void ClientSocketPoolBase::EnableLateBindingOfSockets(bool enabled) { |
| 416 g_late_binding = enabled; | 437 g_late_binding = enabled; |
| 417 } | 438 } |
| 418 | 439 |
| 419 void ClientSocketPoolBase::RemoveConnectJob( | 440 void ClientSocketPoolBase::RemoveConnectJob( |
| 420 const ClientSocketHandle* handle, ConnectJob *job, Group* group) { | 441 const ClientSocketHandle* handle, const ConnectJob *job, Group* group) { |
| 421 CHECK(connecting_socket_count_ > 0); | 442 CHECK(connecting_socket_count_ > 0); |
| 422 connecting_socket_count_--; | 443 connecting_socket_count_--; |
| 423 | 444 |
| 424 if (g_late_binding) { | 445 if (g_late_binding) { |
| 425 DCHECK(job); | 446 DCHECK(job); |
| 426 delete job; | 447 delete job; |
| 427 } else { | 448 } else { |
| 428 ConnectJobMap::iterator it = connect_job_map_.find(handle); | 449 ConnectJobMap::iterator it = connect_job_map_.find(handle); |
| 429 CHECK(it != connect_job_map_.end()); | 450 CHECK(it != connect_job_map_.end()); |
| 430 job = it->second; | 451 job = it->second; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 } | 548 } |
| 528 | 549 |
| 529 bool ClientSocketPoolBase::ReachedMaxSocketsLimit() const { | 550 bool ClientSocketPoolBase::ReachedMaxSocketsLimit() const { |
| 530 // Each connecting socket will eventually connect and be handed out. | 551 // Each connecting socket will eventually connect and be handed out. |
| 531 int total = handed_out_socket_count_ + connecting_socket_count_; | 552 int total = handed_out_socket_count_ + connecting_socket_count_; |
| 532 DCHECK_LE(total, max_sockets_); | 553 DCHECK_LE(total, max_sockets_); |
| 533 return total == max_sockets_; | 554 return total == max_sockets_; |
| 534 } | 555 } |
| 535 | 556 |
| 536 } // namespace net | 557 } // namespace net |
| OLD | NEW |