OLD | NEW |
| (Empty) |
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/macros.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 Perspective perspective, | |
31 QuicStreamOffset send_window_offset, | |
32 QuicStreamOffset receive_window_offset, | |
33 bool should_auto_tune_receive_window); | |
34 | |
35 ~QuicFlowController() {} | |
36 | |
37 // Called when we see a new highest received byte offset from the peer, either | |
38 // via a data frame or a RST. | |
39 // Returns true if this call changes highest_received_byte_offset_, and false | |
40 // in the case where |new_offset| is <= highest_received_byte_offset_. | |
41 bool UpdateHighestReceivedOffset(QuicStreamOffset new_offset); | |
42 | |
43 // Called when bytes received from the peer are consumed locally. This may | |
44 // trigger the sending of a WINDOW_UPDATE frame using |connection|. | |
45 void AddBytesConsumed(QuicByteCount bytes_consumed); | |
46 | |
47 // Called when bytes are sent to the peer. | |
48 void AddBytesSent(QuicByteCount bytes_sent); | |
49 | |
50 // Set a new send window offset. | |
51 // Returns true if this increases send_window_offset_ and is now blocked. | |
52 bool UpdateSendWindowOffset(QuicStreamOffset new_send_window_offset); | |
53 | |
54 // Returns the current available send window. | |
55 QuicByteCount SendWindowSize() const; | |
56 | |
57 // Send a BLOCKED frame if appropriate. | |
58 void MaybeSendBlocked(); | |
59 | |
60 // Returns true if flow control send limits have been reached. | |
61 bool IsBlocked() const; | |
62 | |
63 // Returns true if flow control receive limits have been violated by the peer. | |
64 bool FlowControlViolation(); | |
65 | |
66 QuicByteCount bytes_consumed() const { return bytes_consumed_; } | |
67 | |
68 QuicStreamOffset highest_received_byte_offset() const { | |
69 return highest_received_byte_offset_; | |
70 } | |
71 | |
72 void set_receive_window_size_limit(QuicByteCount receive_window_size_limit) { | |
73 DCHECK_GE(receive_window_size_limit, receive_window_size_limit_); | |
74 receive_window_size_limit_ = receive_window_size_limit; | |
75 } | |
76 | |
77 void set_auto_tune_receive_window(bool enable) { | |
78 auto_tune_receive_window_ = enable; | |
79 } | |
80 | |
81 // Should only be called before any data is received. | |
82 void UpdateReceiveWindowSize(QuicStreamOffset size); | |
83 | |
84 bool auto_tune_receive_window() { return auto_tune_receive_window_; } | |
85 | |
86 private: | |
87 friend class test::QuicFlowControllerPeer; | |
88 | |
89 // Send a WINDOW_UPDATE frame if appropriate. | |
90 void MaybeSendWindowUpdate(); | |
91 | |
92 // Auto-tune the max receive window size. | |
93 void MaybeIncreaseMaxWindowSize(); | |
94 | |
95 // The parent connection, used to send connection close on flow control | |
96 // violation, and WINDOW_UPDATE and BLOCKED frames when appropriate. | |
97 // Not owned. | |
98 QuicConnection* connection_; | |
99 | |
100 // ID of stream this flow controller belongs to. This can be 0 if this is a | |
101 // connection level flow controller. | |
102 QuicStreamId id_; | |
103 | |
104 // Tracks if this is owned by a server or a client. | |
105 Perspective perspective_; | |
106 | |
107 // Tracks number of bytes sent to the peer. | |
108 QuicByteCount 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 QuicStreamOffset send_window_offset_; | |
113 | |
114 // Overview of receive flow controller. | |
115 // | |
116 // 0=...===1=======2-------3 ...... FIN | |
117 // |<--- <= 4 --->| | |
118 // | |
119 | |
120 // 1) bytes_consumed_ - moves forward when data is read out of the | |
121 // stream. | |
122 // | |
123 // 2) highest_received_byte_offset_ - moves when data is received | |
124 // from the peer. | |
125 // | |
126 // 3) receive_window_offset_ - moves when WINDOW_UPDATE is sent. | |
127 // | |
128 // 4) receive_window_size_ - maximum allowed unread data (3 - 1). | |
129 // This value may be increased by auto-tuning. | |
130 // | |
131 // 5) receive_window_size_limit_ - limit on receive_window_size_; | |
132 // auto-tuning will not increase window size beyond this limit. | |
133 | |
134 // Track number of bytes received from the peer, which have been consumed | |
135 // locally. | |
136 QuicByteCount bytes_consumed_; | |
137 | |
138 // The highest byte offset we have seen from the peer. This could be the | |
139 // highest offset in a data frame, or a final value in a RST. | |
140 QuicStreamOffset highest_received_byte_offset_; | |
141 | |
142 // The absolute offset in the incoming byte stream. The peer should never send | |
143 // us bytes which are beyond this offset. | |
144 QuicStreamOffset receive_window_offset_; | |
145 | |
146 // Largest size the receive window can grow to. | |
147 QuicByteCount receive_window_size_; | |
148 | |
149 // Upper limit on receive_window_size_; | |
150 QuicByteCount receive_window_size_limit_; | |
151 | |
152 // Used to dynamically enable receive window auto-tuning. | |
153 bool auto_tune_receive_window_; | |
154 | |
155 // Send window update when receive window size drops below this. | |
156 QuicByteCount WindowUpdateThreshold(); | |
157 | |
158 // Keep track of the last time we sent a BLOCKED frame. We should only send | |
159 // another when the number of bytes we have sent has changed. | |
160 QuicStreamOffset last_blocked_send_window_offset_; | |
161 | |
162 // Keep time of the last time a window update was sent. We use this | |
163 // as part of the receive window auto tuning. | |
164 QuicTime prev_window_update_time_; | |
165 | |
166 DISALLOW_COPY_AND_ASSIGN(QuicFlowController); | |
167 }; | |
168 | |
169 } // namespace net | |
170 | |
171 #endif // NET_QUIC_QUIC_FLOW_CONTROLLER_H_ | |
OLD | NEW |