Home | History | Annotate | Download | only in quic
      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_QUIC_QUIC_FLOW_CONTROLLER_H_
      6 #define NET_QUIC_QUIC_FLOW_CONTROLLER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "net/base/net_export.h"
     10 #include "net/quic/quic_protocol.h"
     11 
     12 namespace net {
     13 
     14 namespace test {
     15 class QuicFlowControllerPeer;
     16 }  // namespace test
     17 
     18 class QuicConnection;
     19 
     20 const QuicStreamId kConnectionLevelId = 0;
     21 
     22 // QuicFlowController allows a QUIC stream or connection to perform flow
     23 // control. The stream/connection owns a QuicFlowController which keeps track of
     24 // bytes sent/received, can tell the owner if it is flow control blocked, and
     25 // can send WINDOW_UPDATE or BLOCKED frames when needed.
     26 class NET_EXPORT_PRIVATE QuicFlowController {
     27  public:
     28   QuicFlowController(QuicConnection* connection,
     29                      QuicStreamId id,
     30                      bool is_server,
     31                      uint64 send_window_offset,
     32                      uint64 receive_window_offset,
     33                      uint64 max_receive_window);
     34   ~QuicFlowController() {}
     35 
     36   // Called when we see a new highest received byte offset from the peer, either
     37   // via a data frame or a RST.
     38   // Returns true if this call changes highest_received_byte_offset_, and false
     39   // in the case where |new_offset| is <= highest_received_byte_offset_.
     40   bool UpdateHighestReceivedOffset(uint64 new_offset);
     41 
     42   // Called when bytes received from the peer are consumed locally. This may
     43   // trigger the sending of a WINDOW_UPDATE frame using |connection|.
     44   void AddBytesConsumed(uint64 bytes_consumed);
     45 
     46   // Called when bytes are sent to the peer.
     47   void AddBytesSent(uint64 bytes_sent);
     48 
     49   // Set a new send window offset.
     50   // Returns true if this changes send_window_offset_, and false in the case
     51   // where |new_send_window| is <= send_window_offset_.
     52   bool UpdateSendWindowOffset(uint64 new_send_window_offset);
     53 
     54   // Returns the current available send window.
     55   uint64 SendWindowSize() const;
     56 
     57   // Send a BLOCKED frame if appropriate.
     58   void MaybeSendBlocked();
     59 
     60   // Disable flow control.
     61   void Disable();
     62 
     63   // Returns true if flow control is enabled.
     64   bool IsEnabled() const;
     65 
     66   // Returns true if flow control send limits have been reached.
     67   bool IsBlocked() const;
     68 
     69   // Returns true if flow control receive limits have been violated by the peer.
     70   bool FlowControlViolation();
     71 
     72   uint64 bytes_consumed() const { return bytes_consumed_; }
     73 
     74   uint64 highest_received_byte_offset() const {
     75     return highest_received_byte_offset_;
     76   }
     77 
     78  private:
     79   friend class test::QuicFlowControllerPeer;
     80 
     81   // Send a WINDOW_UPDATE frame if appropriate.
     82   void MaybeSendWindowUpdate();
     83 
     84   // The parent connection, used to send connection close on flow control
     85   // violation, and WINDOW_UPDATE and BLOCKED frames when appropriate.
     86   // Not owned.
     87   QuicConnection* connection_;
     88 
     89   // ID of stream this flow controller belongs to. This can be 0 if this is a
     90   // connection level flow controller.
     91   QuicStreamId id_;
     92 
     93   // True if flow control is enabled.
     94   bool is_enabled_;
     95 
     96   // True if this is owned by a server.
     97   bool is_server_;
     98 
     99   // Track number of bytes received from the peer, which have been consumed
    100   // locally.
    101   uint64 bytes_consumed_;
    102 
    103   // The highest byte offset we have seen from the peer. This could be the
    104   // highest offset in a data frame, or a final value in a RST.
    105   uint64 highest_received_byte_offset_;
    106 
    107   // Tracks number of bytes sent to the peer.
    108   uint64 bytes_sent_;
    109 
    110   // The absolute offset in the outgoing byte stream. If this offset is reached
    111   // then we become flow control blocked until we receive a WINDOW_UPDATE.
    112   uint64 send_window_offset_;
    113 
    114   // The absolute offset in the incoming byte stream. The peer should never send
    115   // us bytes which are beyond this offset.
    116   uint64 receive_window_offset_;
    117 
    118   // Largest size the receive window can grow to.
    119   uint64 max_receive_window_;
    120 
    121   // Keep track of the last time we sent a BLOCKED frame. We should only send
    122   // another when the number of bytes we have sent has changed.
    123   uint64 last_blocked_send_window_offset_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(QuicFlowController);
    126 };
    127 
    128 }  // namespace net
    129 
    130 #endif  // NET_QUIC_QUIC_FLOW_CONTROLLER_H_
    131