| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_WEBSOCKETS_WEBSOCKET_THROTTLE_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_ |
| 6 #define NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 12 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 14 | 15 |
| 15 template <typename T> struct DefaultSingletonTraits; | 16 template <typename T> struct DefaultSingletonTraits; |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 class SocketStream; | 20 class SocketStream; |
| 20 class WebSocketJob; | 21 class WebSocketJob; |
| 21 | 22 |
| 22 // SocketStreamThrottle for WebSocket protocol. | 23 // SocketStreamThrottle for WebSocket protocol. |
| 23 // Implements the client-side requirements in the spec. | 24 // Implements the client-side requirements in the spec. |
| 24 // http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol | 25 // http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol |
| 25 // 4.1 Handshake | 26 // 4.1 Handshake |
| 26 // 1. If the user agent already has a Web Socket connection to the | 27 // 1. If the user agent already has a Web Socket connection to the |
| 27 // remote host (IP address) identified by /host/, even if known by | 28 // remote host (IP address) identified by /host/, even if known by |
| 28 // another name, wait until that connection has been established or | 29 // another name, wait until that connection has been established or |
| 29 // for that connection to have failed. | 30 // for that connection to have failed. |
| 30 class NET_EXPORT_PRIVATE WebSocketThrottle { | 31 class NET_EXPORT_PRIVATE WebSocketThrottle { |
| 31 public: | 32 public: |
| 32 // Returns the singleton instance. | 33 // Returns the singleton instance. |
| 33 static WebSocketThrottle* GetInstance(); | 34 static WebSocketThrottle* GetInstance(); |
| 34 | 35 |
| 35 // Puts |job| in |queue_| and queues for the destination addresses | 36 // Puts |job| in |queue_| and queues for the destination addresses |
| 36 // of |job|. | 37 // of |job|. |
| 37 // If other job is using the same destination address, set |job| waiting. | 38 // If other job is using the same destination address, set |job| waiting. |
| 38 void PutInQueue(WebSocketJob* job); | 39 // |
| 40 // Returns true if successful. If the number of pending jobs will exceed |
| 41 // the limit, does nothing and returns false. |
| 42 bool PutInQueue(WebSocketJob* job); |
| 39 | 43 |
| 40 // Removes |job| from |queue_| and queues for the destination addresses | 44 // Removes |job| from |queue_| and queues for the destination addresses |
| 41 // of |job|. | 45 // of |job|, and then wakes up jobs that can now resume establishing a |
| 46 // connection. |
| 42 void RemoveFromQueue(WebSocketJob* job); | 47 void RemoveFromQueue(WebSocketJob* job); |
| 43 | 48 |
| 44 // Checks sockets waiting in |queue_| and check the socket is the front of | |
| 45 // every queue for the destination addresses of |socket|. | |
| 46 // If so, the socket can resume establishing connection, so wake up | |
| 47 // the socket. | |
| 48 void WakeupSocketIfNecessary(); | |
| 49 | |
| 50 private: | 49 private: |
| 51 typedef std::deque<WebSocketJob*> ConnectingQueue; | 50 typedef std::deque<WebSocketJob*> ConnectingQueue; |
| 52 typedef std::map<IPEndPoint, ConnectingQueue*> ConnectingAddressMap; | 51 typedef std::map<IPEndPoint, ConnectingQueue> ConnectingAddressMap; |
| 53 | 52 |
| 54 WebSocketThrottle(); | 53 WebSocketThrottle(); |
| 55 ~WebSocketThrottle(); | 54 ~WebSocketThrottle(); |
| 56 friend struct DefaultSingletonTraits<WebSocketThrottle>; | 55 friend struct DefaultSingletonTraits<WebSocketThrottle>; |
| 57 | 56 |
| 57 // Examines if any of the given jobs can resume establishing a connection. If |
| 58 // for all per-address queues for each resolved addresses |
| 59 // (job->address_list()) of a job, the job is at the front of the queues, the |
| 60 // job can resume establishing a connection, so wakes up the job. |
| 61 void WakeupSocketIfNecessary( |
| 62 const std::set<WebSocketJob*>& wakeup_candidates); |
| 63 |
| 58 // Key: string of host's address. Value: queue of sockets for the address. | 64 // Key: string of host's address. Value: queue of sockets for the address. |
| 59 ConnectingAddressMap addr_map_; | 65 ConnectingAddressMap addr_map_; |
| 60 | 66 |
| 61 // Queue of sockets for websockets in opening state. | 67 // Queue of sockets for websockets in opening state. |
| 62 ConnectingQueue queue_; | 68 ConnectingQueue queue_; |
| 63 | 69 |
| 64 DISALLOW_COPY_AND_ASSIGN(WebSocketThrottle); | 70 DISALLOW_COPY_AND_ASSIGN(WebSocketThrottle); |
| 65 }; | 71 }; |
| 66 | 72 |
| 67 } // namespace net | 73 } // namespace net |
| 68 | 74 |
| 69 #endif // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_ | 75 #endif // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_ |
| OLD | NEW |