summaryrefslogtreecommitdiffstats
path: root/net/websockets/websocket_throttle.h
blob: 1c646588e1f2ec3d55ae7793569157cc05d6df12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
#define NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_

#include <deque>
#include <string>

#include "base/hash_tables.h"
#include "net/base/net_export.h"

template <typename T> struct DefaultSingletonTraits;

namespace net {

class SocketStream;
class WebSocketJob;

// SocketStreamThrottle for WebSocket protocol.
// Implements the client-side requirements in the spec.
// http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
// 4.1 Handshake
//   1.   If the user agent already has a Web Socket connection to the
//        remote host (IP address) identified by /host/, even if known by
//        another name, wait until that connection has been established or
//        for that connection to have failed.
class NET_EXPORT_PRIVATE WebSocketThrottle {
 public:
  // Returns the singleton instance.
  static WebSocketThrottle* GetInstance();

  // Puts |job| in |queue_| and queues for the destination addresses
  // of |job|.
  // If other job is using the same destination address, set |job| waiting.
  void PutInQueue(WebSocketJob* job);

  // Removes |job| from |queue_| and queues for the destination addresses
  // of |job|.
  void RemoveFromQueue(WebSocketJob* job);

  // Checks sockets waiting in |queue_| and check the socket is the front of
  // every queue for the destination addresses of |socket|.
  // If so, the socket can resume estabilshing connection, so wake up
  // the socket.
  void WakeupSocketIfNecessary();

 private:
  typedef std::deque<WebSocketJob*> ConnectingQueue;
  typedef base::hash_map<std::string, ConnectingQueue*> ConnectingAddressMap;

  WebSocketThrottle();
  virtual ~WebSocketThrottle();
  friend struct DefaultSingletonTraits<WebSocketThrottle>;

  // Key: string of host's address.  Value: queue of sockets for the address.
  ConnectingAddressMap addr_map_;

  // Queue of sockets for websockets in opening state.
  ConnectingQueue queue_;
};

}  // namespace net

#endif  // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_