OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // The base class for client/server reliable streams. | |
6 | |
7 // It does not contain the entire interface needed by an application to interact | |
8 // with a QUIC stream. Some parts of the interface must be obtained by | |
9 // accessing the owning session object. A subclass of ReliableQuicStream | |
10 // connects the object and the application that generates and consumes the data | |
11 // of the stream. | |
12 | |
13 // The ReliableQuicStream object has a dependent QuicStreamSequencer object, | |
14 // which is given the stream frames as they arrive, and provides stream data in | |
15 // order by invoking ProcessRawData(). | |
16 | |
17 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_ | |
18 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_ | |
19 | |
20 #include <stddef.h> | |
21 #include <stdint.h> | |
22 #include <sys/types.h> | |
23 | |
24 #include <list> | |
25 #include <string> | |
26 | |
27 #include "base/macros.h" | |
28 #include "base/memory/ref_counted.h" | |
29 #include "base/strings/string_piece.h" | |
30 #include "net/base/iovec.h" | |
31 #include "net/base/net_export.h" | |
32 #include "net/quic/quic_flow_controller.h" | |
33 #include "net/quic/quic_protocol.h" | |
34 #include "net/quic/quic_stream_sequencer.h" | |
35 #include "net/quic/quic_types.h" | |
36 | |
37 namespace net { | |
38 | |
39 namespace test { | |
40 class ReliableQuicStreamPeer; | |
41 } // namespace test | |
42 | |
43 class QuicSession; | |
44 | |
45 class NET_EXPORT_PRIVATE ReliableQuicStream { | |
46 public: | |
47 ReliableQuicStream(QuicStreamId id, QuicSession* session); | |
48 | |
49 virtual ~ReliableQuicStream(); | |
50 | |
51 // Not in use currently. | |
52 void SetFromConfig(); | |
53 | |
54 // Called by the session when a (potentially duplicate) stream frame has been | |
55 // received for this stream. | |
56 virtual void OnStreamFrame(const QuicStreamFrame& frame); | |
57 | |
58 // Called by the session when the connection becomes writeable to allow the | |
59 // stream to write any pending data. | |
60 virtual void OnCanWrite(); | |
61 | |
62 // Called by the session just before the object is destroyed. | |
63 // The object should not be accessed after OnClose is called. | |
64 // Sends a RST_STREAM with code QUIC_RST_ACKNOWLEDGEMENT if neither a FIN nor | |
65 // a RST_STREAM has been sent. | |
66 virtual void OnClose(); | |
67 | |
68 // Called by the session when the endpoint receives a RST_STREAM from the | |
69 // peer. | |
70 virtual void OnStreamReset(const QuicRstStreamFrame& frame); | |
71 | |
72 // Called by the session when the endpoint receives or sends a connection | |
73 // close, and should immediately close the stream. | |
74 virtual void OnConnectionClosed(QuicErrorCode error, | |
75 ConnectionCloseSource source); | |
76 | |
77 // Called by the stream subclass after it has consumed the final incoming | |
78 // data. | |
79 void OnFinRead(); | |
80 | |
81 // Called when new data is available from the sequencer. Subclasses must | |
82 // actively retrieve the data using the sequencer's Readv() or | |
83 // GetReadableRegions() method. | |
84 virtual void OnDataAvailable() = 0; | |
85 | |
86 // Called by the subclass or the sequencer to reset the stream from this | |
87 // end. | |
88 virtual void Reset(QuicRstStreamErrorCode error); | |
89 | |
90 // Called by the subclass or the sequencer to close the entire connection from | |
91 // this end. | |
92 virtual void CloseConnectionWithDetails(QuicErrorCode error, | |
93 const std::string& details); | |
94 | |
95 QuicStreamId id() const { return id_; } | |
96 | |
97 QuicRstStreamErrorCode stream_error() const { return stream_error_; } | |
98 QuicErrorCode connection_error() const { return connection_error_; } | |
99 | |
100 bool reading_stopped() const { | |
101 return sequencer_.ignore_read_data() || read_side_closed_; | |
102 } | |
103 bool write_side_closed() const { return write_side_closed_; } | |
104 | |
105 bool rst_received() { return rst_received_; } | |
106 bool rst_sent() { return rst_sent_; } | |
107 bool fin_received() { return fin_received_; } | |
108 bool fin_sent() { return fin_sent_; } | |
109 | |
110 uint64_t queued_data_bytes() const { return queued_data_bytes_; } | |
111 | |
112 uint64_t stream_bytes_read() const { return stream_bytes_read_; } | |
113 uint64_t stream_bytes_written() const { return stream_bytes_written_; } | |
114 | |
115 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; } | |
116 void set_fin_received(bool fin_received) { fin_received_ = fin_received; } | |
117 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; } | |
118 | |
119 void set_rst_received(bool rst_received) { rst_received_ = rst_received; } | |
120 void set_stream_error(QuicRstStreamErrorCode error) { stream_error_ = error; } | |
121 | |
122 // Adjust the flow control window according to new offset in |frame|. | |
123 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame); | |
124 | |
125 // Used in Chrome. | |
126 int num_frames_received() const; | |
127 int num_early_frames_received() const; | |
128 int num_duplicate_frames_received() const; | |
129 | |
130 QuicFlowController* flow_controller() { return &flow_controller_; } | |
131 | |
132 // Called when endpoint receives a frame which could increase the highest | |
133 // offset. | |
134 // Returns true if the highest offset did increase. | |
135 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset); | |
136 // Called when bytes are sent to the peer. | |
137 void AddBytesSent(QuicByteCount bytes); | |
138 // Called by the stream sequencer as bytes are consumed from the buffer. | |
139 // If the receive window has dropped below the threshold, then send a | |
140 // WINDOW_UPDATE frame. | |
141 void AddBytesConsumed(QuicByteCount bytes); | |
142 | |
143 // Updates the flow controller's send window offset and calls OnCanWrite if | |
144 // it was blocked before. | |
145 void UpdateSendWindowOffset(QuicStreamOffset new_offset); | |
146 | |
147 // Returns true if the stream has received either a RST_STREAM or a FIN - | |
148 // either of which gives a definitive number of bytes which the peer has | |
149 // sent. If this is not true on deletion of the stream object, the session | |
150 // must keep track of the stream's byte offset until a definitive final value | |
151 // arrives. | |
152 bool HasFinalReceivedByteOffset() const { | |
153 return fin_received_ || rst_received_; | |
154 } | |
155 | |
156 // Returns true if the stream has queued data waiting to write. | |
157 bool HasBufferedData() const; | |
158 | |
159 // Returns the version of QUIC being used for this stream. | |
160 QuicVersion version() const; | |
161 | |
162 bool fin_received() const { return fin_received_; } | |
163 | |
164 // Sets the sequencer to consume all incoming data itself and not call | |
165 // OnDataAvailable(). | |
166 // When the FIN is received, the stream will be notified automatically (via | |
167 // OnFinRead()) (which may happen during the call of StopReading()). | |
168 // TODO(dworley): There should be machinery to send a RST_STREAM/NO_ERROR and | |
169 // stop sending stream-level flow-control updates when this end sends FIN. | |
170 virtual void StopReading(); | |
171 | |
172 // Get peer IP of the lastest packet which connection is dealing/delt with. | |
173 virtual const IPEndPoint& PeerAddressOfLatestPacket() const; | |
174 | |
175 protected: | |
176 // Sends as much of 'data' to the connection as the connection will consume, | |
177 // and then buffers any remaining data in queued_data_. | |
178 // If fin is true: if it is immediately passed on to the session, | |
179 // write_side_closed() becomes true, otherwise fin_buffered_ becomes true. | |
180 void WriteOrBufferData(base::StringPiece data, | |
181 bool fin, | |
182 QuicAckListenerInterface* ack_listener); | |
183 | |
184 // Sends as many bytes in the first |count| buffers of |iov| to the connection | |
185 // as the connection will consume. | |
186 // If |ack_listener| is provided, then it will be notified once all | |
187 // the ACKs for this write have been received. | |
188 // Returns the number of bytes consumed by the connection. | |
189 QuicConsumedData WritevData(const struct iovec* iov, | |
190 int iov_count, | |
191 bool fin, | |
192 QuicAckListenerInterface* ack_listener); | |
193 | |
194 // Allows override of the session level writev, for the force HOL | |
195 // blocking experiment. | |
196 virtual QuicConsumedData WritevDataInner( | |
197 QuicIOVector iov, | |
198 QuicStreamOffset offset, | |
199 bool fin, | |
200 QuicAckListenerInterface* ack_notifier_delegate); | |
201 | |
202 // Close the write side of the socket. Further writes will fail. | |
203 // Can be called by the subclass or internally. | |
204 // Does not send a FIN. May cause the stream to be closed. | |
205 virtual void CloseWriteSide(); | |
206 | |
207 bool fin_buffered() const { return fin_buffered_; } | |
208 | |
209 const QuicSession* session() const { return session_; } | |
210 QuicSession* session() { return session_; } | |
211 | |
212 const QuicStreamSequencer* sequencer() const { return &sequencer_; } | |
213 QuicStreamSequencer* sequencer() { return &sequencer_; } | |
214 | |
215 void DisableConnectionFlowControlForThisStream() { | |
216 stream_contributes_to_connection_flow_control_ = false; | |
217 } | |
218 | |
219 private: | |
220 friend class test::ReliableQuicStreamPeer; | |
221 friend class QuicStreamUtils; | |
222 | |
223 // Close the read side of the socket. May cause the stream to be closed. | |
224 // Subclasses and consumers should use StopReading to terminate reading early. | |
225 void CloseReadSide(); | |
226 | |
227 // Subclasses and consumers should use reading_stopped. | |
228 bool read_side_closed() const { return read_side_closed_; } | |
229 | |
230 struct PendingData { | |
231 PendingData(std::string data_in, QuicAckListenerInterface* ack_listener_in); | |
232 ~PendingData(); | |
233 | |
234 // Pending data to be written. | |
235 std::string data; | |
236 // Index of the first byte in data still to be written. | |
237 size_t offset; | |
238 // AckListener that should be notified when the pending data is acked. | |
239 // Can be nullptr. | |
240 scoped_refptr<QuicAckListenerInterface> ack_listener; | |
241 }; | |
242 | |
243 // Calls MaybeSendBlocked on the stream's flow controller and the connection | |
244 // level flow controller. If the stream is flow control blocked by the | |
245 // connection-level flow controller but not by the stream-level flow | |
246 // controller, marks this stream as connection-level write blocked. | |
247 void MaybeSendBlocked(); | |
248 | |
249 std::list<PendingData> queued_data_; | |
250 // How many bytes are queued? | |
251 uint64_t queued_data_bytes_; | |
252 | |
253 QuicStreamSequencer sequencer_; | |
254 QuicStreamId id_; | |
255 // Pointer to the owning QuicSession object. | |
256 QuicSession* session_; | |
257 // Bytes read and written refer to payload bytes only: they do not include | |
258 // framing, encryption overhead etc. | |
259 uint64_t stream_bytes_read_; | |
260 uint64_t stream_bytes_written_; | |
261 | |
262 // Stream error code received from a RstStreamFrame or error code sent by the | |
263 // visitor or sequencer in the RstStreamFrame. | |
264 QuicRstStreamErrorCode stream_error_; | |
265 // Connection error code due to which the stream was closed. |stream_error_| | |
266 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers | |
267 // should check |connection_error_|. | |
268 QuicErrorCode connection_error_; | |
269 | |
270 // True if the read side is closed and further frames should be rejected. | |
271 bool read_side_closed_; | |
272 // True if the write side is closed, and further writes should fail. | |
273 bool write_side_closed_; | |
274 | |
275 // True if the subclass has written a FIN with WriteOrBufferData, but it was | |
276 // buffered in queued_data_ rather than being sent to the session. | |
277 bool fin_buffered_; | |
278 // True if a FIN has been sent to the session. | |
279 bool fin_sent_; | |
280 | |
281 // True if this stream has received (and the sequencer has accepted) a | |
282 // StreamFrame with the FIN set. | |
283 bool fin_received_; | |
284 | |
285 // True if an RST_STREAM has been sent to the session. | |
286 // In combination with fin_sent_, used to ensure that a FIN and/or a | |
287 // RST_STREAM is always sent to terminate the stream. | |
288 bool rst_sent_; | |
289 | |
290 // True if this stream has received a RST_STREAM frame. | |
291 bool rst_received_; | |
292 | |
293 // Tracks if the session this stream is running under was created by a | |
294 // server or a client. | |
295 Perspective perspective_; | |
296 | |
297 QuicFlowController flow_controller_; | |
298 | |
299 // The connection level flow controller. Not owned. | |
300 QuicFlowController* connection_flow_controller_; | |
301 | |
302 // Special streams, such as the crypto and headers streams, do not respect | |
303 // connection level flow control limits (but are stream level flow control | |
304 // limited). | |
305 bool stream_contributes_to_connection_flow_control_; | |
306 | |
307 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream); | |
308 }; | |
309 | |
310 } // namespace net | |
311 | |
312 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ | |
OLD | NEW |