OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ | |
6 #define NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/containers/linked_list.h" | |
11 #include "base/logging.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/time/time.h" | |
15 #include "net/base/ip_endpoint.h" | |
16 #include "net/base/net_export.h" | |
17 #include "net/socket/websocket_transport_client_socket_pool.h" | |
18 | |
19 namespace net { | |
20 | |
21 class StreamSocket; | |
22 | |
23 // Keep track of ongoing WebSocket connections in order to satisfy the WebSocket | |
24 // connection throttling requirements described in RFC6455 4.1.2: | |
25 // | |
26 // 2. If the client already has a WebSocket connection to the remote | |
27 // host (IP address) identified by /host/ and port /port/ pair, even | |
28 // if the remote host is known by another name, the client MUST wait | |
29 // until that connection has been established or for that connection | |
30 // to have failed. There MUST be no more than one connection in a | |
31 // CONNECTING state. If multiple connections to the same IP address | |
32 // are attempted simultaneously, the client MUST serialize them so | |
33 // that there is no more than one connection at a time running | |
34 // through the following steps. | |
35 // | |
36 // This class is neither thread-safe nor thread-compatible. | |
37 // TODO(ricea): Make this class thread-compatible by making it not be a | |
38 // singleton. | |
39 class NET_EXPORT_PRIVATE WebSocketEndpointLockManager { | |
40 public: | |
41 // Implement this interface to wait for an endpoint to be available. | |
42 class NET_EXPORT_PRIVATE Waiter : public base::LinkNode<Waiter> { | |
43 public: | |
44 // If the node is in a list, removes it. | |
45 virtual ~Waiter(); | |
46 | |
47 virtual void GotEndpointLock() = 0; | |
48 }; | |
49 | |
50 static WebSocketEndpointLockManager* GetInstance(); | |
51 | |
52 // Returns OK if lock was acquired immediately, ERR_IO_PENDING if not. If the | |
53 // lock was not acquired, then |waiter->GotEndpointLock()| will be called when | |
54 // it is. A Waiter automatically removes itself from the list of waiters when | |
55 // its destructor is called. | |
56 int LockEndpoint(const IPEndPoint& endpoint, Waiter* waiter); | |
57 | |
58 // Records the IPEndPoint associated with a particular socket. This is | |
59 // necessary because TCPClientSocket refuses to return the PeerAddress after | |
60 // the connection is disconnected. The association will be forgotten when | |
61 // UnlockSocket() or UnlockEndpoint() is called. The |socket| pointer must not | |
62 // be deleted between the call to RememberSocket() and the call to | |
63 // UnlockSocket(). | |
64 void RememberSocket(StreamSocket* socket, const IPEndPoint& endpoint); | |
65 | |
66 // Removes the socket association that was recorded by RememberSocket(), then | |
67 // asynchronously releases the lock on the endpoint after a delay. If | |
68 // appropriate, calls |waiter->GetEndpointLock()| when the lock is | |
69 // released. Should be called exactly once for each |socket| that was passed | |
70 // to RememberSocket(). Does nothing if UnlockEndpoint() has been called since | |
71 // the call to RememberSocket(). | |
72 void UnlockSocket(StreamSocket* socket); | |
73 | |
74 // Asynchronously releases the lock on |endpoint| after a delay. Does nothing | |
75 // if |endpoint| is not locked. Removes any socket association that was | |
76 // recorded with RememberSocket(). If appropriate, calls | |
77 // |waiter->GotEndpointLock()| when the lock is released. | |
78 void UnlockEndpoint(const IPEndPoint& endpoint); | |
79 | |
80 // Checks that |lock_info_map_| and |socket_lock_info_map_| are empty. For | |
81 // tests. | |
82 bool IsEmpty() const; | |
83 | |
84 // Changes the value of the unlock delay. Returns the previous value of the | |
85 // delay. | |
86 base::TimeDelta SetUnlockDelayForTesting(base::TimeDelta new_delay); | |
87 | |
88 private: | |
89 struct LockInfo { | |
90 typedef base::LinkedList<Waiter> WaiterQueue; | |
91 | |
92 LockInfo(); | |
93 ~LockInfo(); | |
94 | |
95 // This object must be copyable to be placed in the map, but it cannot be | |
96 // copied after |queue| has been assigned to. | |
97 LockInfo(const LockInfo& rhs); | |
98 | |
99 // Not used. | |
100 LockInfo& operator=(const LockInfo& rhs); | |
101 | |
102 // Must be NULL to copy this object into the map. Must be set to non-NULL | |
103 // after the object is inserted into the map then point to the same list | |
104 // until this object is deleted. | |
105 scoped_ptr<WaiterQueue> queue; | |
106 | |
107 // This pointer is only used to identify the last instance of StreamSocket | |
108 // that was passed to RememberSocket() for this endpoint. It should only be | |
109 // compared with other pointers. It is never dereferenced and not owned. It | |
110 // is non-NULL if RememberSocket() has been called for this endpoint since | |
111 // the last call to UnlockSocket() or UnlockEndpoint(). | |
112 StreamSocket* socket; | |
113 }; | |
114 | |
115 // SocketLockInfoMap requires std::map iterator semantics for LockInfoMap | |
116 // (ie. that the iterator will remain valid as long as the entry is not | |
117 // deleted). | |
118 typedef std::map<IPEndPoint, LockInfo> LockInfoMap; | |
119 typedef std::map<StreamSocket*, LockInfoMap::iterator> SocketLockInfoMap; | |
120 | |
121 WebSocketEndpointLockManager(); | |
122 ~WebSocketEndpointLockManager(); | |
123 | |
124 void UnlockEndpointAfterDelay(const IPEndPoint& endpoint); | |
125 void DelayedUnlockEndpoint(const IPEndPoint& endpoint); | |
126 void EraseSocket(LockInfoMap::iterator lock_info_it); | |
127 | |
128 // If an entry is present in the map for a particular endpoint, then that | |
129 // endpoint is locked. If LockInfo.queue is non-empty, then one or more | |
130 // Waiters are waiting for the lock. | |
131 LockInfoMap lock_info_map_; | |
132 | |
133 // Store sockets remembered by RememberSocket() and not yet unlocked by | |
134 // UnlockSocket() or UnlockEndpoint(). Every entry in this map always | |
135 // references a live entry in lock_info_map_, and the LockInfo::socket member | |
136 // is non-NULL if and only if there is an entry in this map for the socket. | |
137 SocketLockInfoMap socket_lock_info_map_; | |
138 | |
139 // Time to wait between a call to Unlock* and actually unlocking the socket. | |
140 base::TimeDelta unlock_delay_; | |
141 | |
142 // Number of sockets currently pending unlock. | |
143 size_t pending_unlock_count_; | |
144 | |
145 // The messsage loop holding the unlock delay callback may outlive this | |
146 // object. | |
147 base::WeakPtrFactory<WebSocketEndpointLockManager> weak_factory_; | |
148 | |
149 friend struct DefaultSingletonTraits<WebSocketEndpointLockManager>; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(WebSocketEndpointLockManager); | |
152 }; | |
153 | |
154 } // namespace net | |
155 | |
156 #endif // NET_SOCKET_WEBSOCKET_ENDPOINT_LOCK_MANAGER_H_ | |
OLD | NEW |