Home | History | Annotate | Download | only in websockets
      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 
      8 #include "base/hash_tables.h"
      9 #include "base/singleton.h"
     10 #include "net/socket_stream/socket_stream_throttle.h"
     11 
     12 namespace net {
     13 
     14 // SocketStreamThrottle for WebSocket protocol.
     15 // Implements the client-side requirements in the spec.
     16 // http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
     17 // 4.1 Handshake
     18 //   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
     20 //        another name, wait until that connection has been established or
     21 //        for that connection to have failed.
     22 class WebSocketThrottle : public SocketStreamThrottle {
     23  public:
     24   virtual int OnStartOpenConnection(SocketStream* socket,
     25                                     CompletionCallback* callback);
     26   virtual int OnRead(SocketStream* socket, const char* data, int len,
     27                      CompletionCallback* callback);
     28   virtual int OnWrite(SocketStream* socket, const char* data, int len,
     29                       CompletionCallback* callback);
     30   virtual void OnClose(SocketStream* socket);
     31 
     32   static void Init();
     33 
     34  private:
     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 
     52   // Checks sockets waiting in |queue_| and check the socket is the front of
     53   // every queue for the destination addresses of |socket|.
     54   // If so, the socket can resume estabilshing connection, so wake up
     55   // the socket.
     56   void WakeupSocketIfNecessary();
     57 
     58   // Key: string of host's address.  Value: queue of sockets for the address.
     59   ConnectingAddressMap addr_map_;
     60 
     61   // Queue of sockets for websockets in opening state.
     62   ConnectingQueue queue_;
     63 };
     64 
     65 }  // namespace net
     66 
     67 #endif  // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
     68