Home | History | Annotate | Download | only in websockets
      1 // Copyright (c) 2011 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 <deque>
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 
     13 #include "net/base/ip_endpoint.h"
     14 #include "net/base/net_export.h"
     15 
     16 template <typename T> struct DefaultSingletonTraits;
     17 
     18 namespace net {
     19 
     20 class SocketStream;
     21 class WebSocketJob;
     22 
     23 // SocketStreamThrottle for WebSocket protocol.
     24 // Implements the client-side requirements in the spec.
     25 // http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol
     26 // 4.1 Handshake
     27 //   1.   If the user agent already has a Web Socket connection to the
     28 //        remote host (IP address) identified by /host/, even if known by
     29 //        another name, wait until that connection has been established or
     30 //        for that connection to have failed.
     31 class NET_EXPORT_PRIVATE WebSocketThrottle {
     32  public:
     33   // Returns the singleton instance.
     34   static WebSocketThrottle* GetInstance();
     35 
     36   // Puts |job| in |queue_| and queues for the destination addresses
     37   // of |job|.
     38   // If other job is using the same destination address, set |job| waiting.
     39   //
     40   // Returns true if successful. If the number of pending jobs will exceed
     41   // the limit, does nothing and returns false.
     42   bool PutInQueue(WebSocketJob* job);
     43 
     44   // Removes |job| from |queue_| and queues for the destination addresses
     45   // of |job|, and then wakes up jobs that can now resume establishing a
     46   // connection.
     47   void RemoveFromQueue(WebSocketJob* job);
     48 
     49  private:
     50   typedef std::deque<WebSocketJob*> ConnectingQueue;
     51   typedef std::map<IPEndPoint, ConnectingQueue> ConnectingAddressMap;
     52 
     53   WebSocketThrottle();
     54   ~WebSocketThrottle();
     55   friend struct DefaultSingletonTraits<WebSocketThrottle>;
     56 
     57   // Examines if any of the given jobs can resume establishing a connection. If
     58   // for all per-address queues for each resolved addresses
     59   // (job->address_list()) of a job, the job is at the front of the queues, the
     60   // job can resume establishing a connection, so wakes up the job.
     61   void WakeupSocketIfNecessary(
     62       const std::set<WebSocketJob*>& wakeup_candidates);
     63 
     64   // Key: string of host's address.  Value: queue of sockets for the address.
     65   ConnectingAddressMap addr_map_;
     66 
     67   // Queue of sockets for websockets in opening state.
     68   ConnectingQueue queue_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(WebSocketThrottle);
     71 };
     72 
     73 }  // namespace net
     74 
     75 #endif  // NET_WEBSOCKETS_WEBSOCKET_THROTTLE_H_
     76