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
3 // found in the LICENSE file.
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_export.h"
16 template <typename T
> struct DefaultSingletonTraits
;
23 // SocketStreamThrottle for WebSocket protocol.
24 // Implements the client-side requirements in the spec.
25 // http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
27 // 1. If the user agent already has a Web Socket connection to the
28 // remote host (IP address) identified by /host/, even if known by
29 // another name, wait until that connection has been established or
30 // for that connection to have failed.
31 class NET_EXPORT_PRIVATE WebSocketThrottle
{
33 // Returns the singleton instance.
34 static WebSocketThrottle
* GetInstance();
36 // Puts |job| in |queue_| and queues for the destination addresses
38 // If other job is using the same destination address, set |job| waiting.
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
);
44 // Removes |job| from |queue_| and queues for the destination addresses
45 // of |job|, and then wakes up jobs that can now resume establishing a
47 void RemoveFromQueue(WebSocketJob
* job
);
50 typedef std::deque
<WebSocketJob
*> ConnectingQueue
;
51 typedef std::map
<IPEndPoint
, ConnectingQueue
> ConnectingAddressMap
;
55 friend struct DefaultSingletonTraits
<WebSocketThrottle
>;
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
);
64 // Key: string of host's address. Value: queue of sockets for the address.
65 ConnectingAddressMap addr_map_
;
67 // Queue of sockets for websockets in opening state.
68 ConnectingQueue queue_
;
70 DISALLOW_COPY_AND_ASSIGN(WebSocketThrottle
);
75 #endif // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_