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