summaryrefslogtreecommitdiffstats
path: root/net/websockets/websocket_throttle.h
diff options
context:
space:
mode:
authorukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-04 10:02:28 +0000
committerukai@chromium.org <ukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-04 10:02:28 +0000
commit4c4eac006bdbf1ad0f3e37db47c488f7c3ee4949 (patch)
tree16237134b9e037cf609ca4211b5c9efbcd8759ee /net/websockets/websocket_throttle.h
parent8ecd3aade85b825c8206735f16c23880023782db (diff)
downloadchromium_src-4c4eac006bdbf1ad0f3e37db47c488f7c3ee4949.zip
chromium_src-4c4eac006bdbf1ad0f3e37db47c488f7c3ee4949.tar.gz
chromium_src-4c4eac006bdbf1ad0f3e37db47c488f7c3ee4949.tar.bz2
Implement websocket throttling.
Implement the client-side requirements in the spec. 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. BUG=none TEST=net_unittests passes Review URL: http://codereview.chromium.org/342052 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30949 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/websockets/websocket_throttle.h')
-rw-r--r--net/websockets/websocket_throttle.h67
1 files changed, 67 insertions, 0 deletions
diff --git a/net/websockets/websocket_throttle.h b/net/websockets/websocket_throttle.h
new file mode 100644
index 0000000..279aea2
--- /dev/null
+++ b/net/websockets/websocket_throttle.h
@@ -0,0 +1,67 @@
+// Copyright (c) 2009 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 "base/hash_tables.h"
+#include "base/singleton.h"
+#include "net/socket_stream/socket_stream_throttle.h"
+
+namespace net {
+
+// 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 WebSocketThrottle : public SocketStreamThrottle {
+ public:
+ virtual int OnStartOpenConnection(SocketStream* socket,
+ CompletionCallback* callback);
+ virtual int OnRead(SocketStream* socket, const char* data, int len,
+ CompletionCallback* callback);
+ virtual int OnWrite(SocketStream* socket, const char* data, int len,
+ CompletionCallback* callback);
+ virtual void OnClose(SocketStream* socket);
+
+ static void Init();
+
+ private:
+ class WebSocketState;
+ typedef std::deque<WebSocketState*> ConnectingQueue;
+ typedef base::hash_map<std::string, ConnectingQueue*> ConnectingAddressMap;
+
+ WebSocketThrottle();
+ virtual ~WebSocketThrottle();
+ friend struct DefaultSingletonTraits<WebSocketThrottle>;
+
+ // Puts |socket| in |queue_| and queues for the destination addresses
+ // of |socket|. Also sets |state| as UserData of |socket|.
+ // If other socket is using the same destination address, set |state| waiting.
+ void PutInQueue(SocketStream* socket, WebSocketState* state);
+
+ // Removes |socket| from |queue_| and queues for the destination addresses
+ // of |socket|. Also releases |state| from UserData of |socket|.
+ void RemoveFromQueue(SocketStream* socket, WebSocketState* state);
+
+ // 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();
+
+ // 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_