| 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/base/client_socket_pool.h" | 5 #include "net/base/client_socket_pool.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "net/base/client_socket.h" | 8 #include "net/base/client_socket.h" |
| 9 #include "net/base/client_socket_handle.h" | 9 #include "net/base/client_socket_handle.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 ClientSocketPool::~ClientSocketPool() { | 36 ClientSocketPool::~ClientSocketPool() { |
| 37 // Clean up any idle sockets. Assert that we have no remaining active | 37 // Clean up any idle sockets. Assert that we have no remaining active |
| 38 // sockets or pending requests. They should have all been cleaned up prior | 38 // sockets or pending requests. They should have all been cleaned up prior |
| 39 // to the manager being destroyed. | 39 // to the manager being destroyed. |
| 40 CloseIdleSockets(); | 40 CloseIdleSockets(); |
| 41 DCHECK(group_map_.empty()); | 41 DCHECK(group_map_.empty()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // InsertRequestIntoQueue inserts the request into the queue based on |
| 45 // priority. Highest priorities are closest to the front. Older requests are |
| 46 // prioritized over requests of equal priority. |
| 47 // |
| 48 // static |
| 49 void ClientSocketPool::InsertRequestIntoQueue(const Request& r, |
| 50 RequestQueue* pending_requests) { |
| 51 RequestQueue::iterator it = pending_requests->begin(); |
| 52 while (it != pending_requests->end() && r.priority <= it->priority) |
| 53 ++it; |
| 54 pending_requests->insert(it, r); |
| 55 } |
| 56 |
| 44 int ClientSocketPool::RequestSocket(ClientSocketHandle* handle, | 57 int ClientSocketPool::RequestSocket(ClientSocketHandle* handle, |
| 58 int priority, |
| 45 CompletionCallback* callback) { | 59 CompletionCallback* callback) { |
| 46 Group& group = group_map_[handle->group_name_]; | 60 Group& group = group_map_[handle->group_name_]; |
| 47 | 61 |
| 48 // Can we make another active socket now? | 62 // Can we make another active socket now? |
| 49 if (group.active_socket_count == max_sockets_per_group_) { | 63 if (group.active_socket_count == max_sockets_per_group_) { |
| 50 Request r; | 64 Request r; |
| 51 r.handle = handle; | 65 r.handle = handle; |
| 52 DCHECK(callback); | 66 DCHECK(callback); |
| 53 r.callback = callback; | 67 r.callback = callback; |
| 54 group.pending_requests.push_back(r); | 68 r.priority = priority; |
| 69 InsertRequestIntoQueue(r, &group.pending_requests); |
| 55 return ERR_IO_PENDING; | 70 return ERR_IO_PENDING; |
| 56 } | 71 } |
| 57 | 72 |
| 58 // OK, we are going to activate one. | 73 // OK, we are going to activate one. |
| 59 group.active_socket_count++; | 74 group.active_socket_count++; |
| 60 | 75 |
| 61 // Use idle sockets in LIFO order because they're more likely to be | 76 // Use idle sockets in LIFO order because they're more likely to be |
| 62 // still reusable. | 77 // still reusable. |
| 63 while (!group.idle_sockets.empty()) { | 78 while (!group.idle_sockets.empty()) { |
| 64 IdleSocket idle_socket = group.idle_sockets.back(); | 79 IdleSocket idle_socket = group.idle_sockets.back(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 group.idle_sockets.push_back(idle_socket); | 191 group.idle_sockets.push_back(idle_socket); |
| 177 IncrementIdleCount(); | 192 IncrementIdleCount(); |
| 178 } else { | 193 } else { |
| 179 delete ptr; | 194 delete ptr; |
| 180 } | 195 } |
| 181 | 196 |
| 182 // Process one pending request. | 197 // Process one pending request. |
| 183 if (!group.pending_requests.empty()) { | 198 if (!group.pending_requests.empty()) { |
| 184 Request r = group.pending_requests.front(); | 199 Request r = group.pending_requests.front(); |
| 185 group.pending_requests.pop_front(); | 200 group.pending_requests.pop_front(); |
| 186 int rv = RequestSocket(r.handle, NULL); | 201 int rv = RequestSocket(r.handle, r.priority, NULL); |
| 187 DCHECK(rv == OK); | 202 DCHECK(rv == OK); |
| 188 r.callback->Run(rv); | 203 r.callback->Run(rv); |
| 189 return; | 204 return; |
| 190 } | 205 } |
| 191 | 206 |
| 192 // Delete group if no longer needed. | 207 // Delete group if no longer needed. |
| 193 if (group.active_socket_count == 0 && group.idle_sockets.empty()) { | 208 if (group.active_socket_count == 0 && group.idle_sockets.empty()) { |
| 194 DCHECK(group.pending_requests.empty()); | 209 DCHECK(group.pending_requests.empty()); |
| 195 group_map_.erase(i); | 210 group_map_.erase(i); |
| 196 } | 211 } |
| 197 } | 212 } |
| 198 | 213 |
| 199 } // namespace net | 214 } // namespace net |
| OLD | NEW |