• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 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.
4 
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
7 #pragma once
8 
9 #include <deque>
10 #include <string>
11 
12 #include "base/hash_tables.h"
13 
14 template <typename T> struct DefaultSingletonTraits;
15 
16 namespace net {
17 
18 class SocketStream;
19 class WebSocketJob;
20 
21 // SocketStreamThrottle for WebSocket protocol.
22 // Implements the client-side requirements in the spec.
23 // http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
24 // 4.1 Handshake
25 //   1.   If the user agent already has a Web Socket connection to the
26 //        remote host (IP address) identified by /host/, even if known by
27 //        another name, wait until that connection has been established or
28 //        for that connection to have failed.
29 class WebSocketThrottle {
30  public:
31   // Returns the singleton instance.
32   static WebSocketThrottle* GetInstance();
33 
34   // Puts |job| in |queue_| and queues for the destination addresses
35   // of |job|.
36   // If other job is using the same destination address, set |job| waiting.
37   void PutInQueue(WebSocketJob* job);
38 
39   // Removes |job| from |queue_| and queues for the destination addresses
40   // of |job|.
41   void RemoveFromQueue(WebSocketJob* job);
42 
43   // Checks sockets waiting in |queue_| and check the socket is the front of
44   // every queue for the destination addresses of |socket|.
45   // If so, the socket can resume estabilshing connection, so wake up
46   // the socket.
47   void WakeupSocketIfNecessary();
48 
49  private:
50   typedef std::deque<WebSocketJob*> ConnectingQueue;
51   typedef base::hash_map<std::string, ConnectingQueue*> ConnectingAddressMap;
52 
53   WebSocketThrottle();
54   virtual ~WebSocketThrottle();
55   friend struct DefaultSingletonTraits<WebSocketThrottle>;
56 
57   // Key: string of host's address.  Value: queue of sockets for the address.
58   ConnectingAddressMap addr_map_;
59 
60   // Queue of sockets for websockets in opening state.
61   ConnectingQueue queue_;
62 };
63 
64 }  // namespace net
65 
66 #endif  // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
67