Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / quic / reliable_quic_stream.h
blob265e4795a78b785d60691842ce4042492084e5a2
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.
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.
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().
17 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_
18 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_
20 #include <sys/types.h>
22 #include <list>
23 #include <string>
25 #include "base/basictypes.h"
26 #include "base/memory/ref_counted.h"
27 #include "base/strings/string_piece.h"
28 #include "net/base/iovec.h"
29 #include "net/base/net_export.h"
30 #include "net/quic/quic_ack_notifier.h"
31 #include "net/quic/quic_flow_controller.h"
32 #include "net/quic/quic_protocol.h"
33 #include "net/quic/quic_stream_sequencer.h"
34 #include "net/quic/quic_types.h"
36 namespace net {
38 namespace test {
39 class ReliableQuicStreamPeer;
40 } // namespace test
42 class QuicSession;
44 class NET_EXPORT_PRIVATE ReliableQuicStream {
45 public:
46 ReliableQuicStream(QuicStreamId id,
47 QuicSession* session);
49 virtual ~ReliableQuicStream();
51 // Sets |fec_policy_| parameter from |session_|'s config.
52 void SetFromConfig();
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);
58 // Called by the session when the connection becomes writeable to allow the
59 // stream to write any pending data.
60 virtual void OnCanWrite();
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();
68 // Called by the session when the endpoint receives a RST_STREAM from the
69 // peer.
70 virtual void OnStreamReset(const QuicRstStreamFrame& frame);
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, bool from_peer);
76 // Called by the sequencer after ProcessRawData has accepted the final
77 // incoming data.
78 virtual void OnFinRead();
80 // Called by the sequencer to deliver to the subclass blocks of the stream's
81 // data in sequential order.
82 // ProcessRawData is called when the next sequential block of data becomes
83 // available, unless the subclass has left the next block of data in the
84 // sequencer. In that case, the subclass must actively retrieve the data
85 // using the sequencer's Readv() or FlushBufferedFrames().
86 // Return value is the number of data bytes accepted by the callee, i.e.,
87 // the first (return value) bytes of data are removed from the sequencer,
88 // and the final data_len-(return value) bytes of data are retained by the
89 // sequencer.
90 virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0;
92 // Called by the subclass or the sequencer to reset the stream from this
93 // end.
94 virtual void Reset(QuicRstStreamErrorCode error);
96 // Called by the subclass or the sequencer to close the entire connection from
97 // this end.
98 virtual void CloseConnection(QuicErrorCode error);
99 virtual void CloseConnectionWithDetails(QuicErrorCode error,
100 const std::string& details);
102 // Returns the effective priority for the stream. This value may change
103 // during the life of the stream.
104 virtual QuicPriority EffectivePriority() const = 0;
106 QuicStreamId id() const { return id_; }
108 QuicRstStreamErrorCode stream_error() const { return stream_error_; }
109 QuicErrorCode connection_error() const { return connection_error_; }
111 bool read_side_closed() const { return read_side_closed_; }
112 bool write_side_closed() const { return write_side_closed_; }
114 uint64 stream_bytes_read() const { return stream_bytes_read_; }
115 uint64 stream_bytes_written() const { return stream_bytes_written_; }
117 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
118 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
120 void set_fec_policy(FecPolicy fec_policy) { fec_policy_ = fec_policy; }
121 FecPolicy fec_policy() const { return fec_policy_; }
123 // Adjust the flow control window according to new offset in |frame|.
124 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
126 // Used in Chrome.
127 int num_frames_received() const;
128 int num_early_frames_received() const;
129 int num_duplicate_frames_received() const;
131 QuicFlowController* flow_controller() { return &flow_controller_; }
133 // Called when endpoint receives a frame which could increase the highest
134 // offset.
135 // Returns true if the highest offset did increase.
136 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset);
137 // Called when bytes are sent to the peer.
138 void AddBytesSent(QuicByteCount bytes);
139 // Called by the stream sequencer as bytes are consumed from the buffer.
140 // If the receive window has dropped below the threshold, then send a
141 // WINDOW_UPDATE frame.
142 void AddBytesConsumed(QuicByteCount bytes);
144 // Updates the flow controller's send window offset and calls OnCanWrite if
145 // it was blocked before.
146 void UpdateSendWindowOffset(QuicStreamOffset new_offset);
148 // Returns true if the stream have received either a RST_STREAM or a FIN -
149 // either of which gives a definitive number of bytes which the peer has
150 // sent. If this is not true on deletion of the stream object, the session
151 // must keep track of the stream's byte offset until a definitive final value
152 // arrives.
153 bool HasFinalReceivedByteOffset() const {
154 return fin_received_ || rst_received_;
157 // Returns true if the stream has queued data waiting to write.
158 bool HasBufferedData() const;
160 // Returns the version of QUIC being used for this stream.
161 QuicVersion version() const;
163 bool fin_received() const { return fin_received_; }
165 protected:
166 // Sends as much of 'data' to the connection as the connection will consume,
167 // and then buffers any remaining data in queued_data_.
168 // If fin is true: if it is immediately passed on to the session,
169 // write_side_closed() becomes true, otherwise fin_buffered_ becomes true.
170 void WriteOrBufferData(
171 base::StringPiece data,
172 bool fin,
173 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
175 // Sends as many bytes in the first |count| buffers of |iov| to the connection
176 // as the connection will consume.
177 // If |ack_notifier_delegate| is provided, then it will be notified once all
178 // the ACKs for this write have been received.
179 // Returns the number of bytes consumed by the connection.
180 QuicConsumedData WritevData(
181 const struct iovec* iov,
182 int iov_count,
183 bool fin,
184 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
186 // Close the read side of the stream. Further incoming stream frames will be
187 // discarded. Can be called by the subclass or internally.
188 // May cause the stream to be closed.
189 virtual void CloseReadSide();
191 // Close the write side of the socket. Further writes will fail.
192 // Can be called by the subclass or internally.
193 // Does not send a FIN. May cause the stream to be closed.
194 void CloseWriteSide();
196 // Helper method that returns FecProtection to use when writing.
197 FecProtection GetFecProtection();
199 bool fin_buffered() const { return fin_buffered_; }
201 const QuicSession* session() const { return session_; }
202 QuicSession* session() { return session_; }
204 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
205 QuicStreamSequencer* sequencer() { return &sequencer_; }
207 void DisableConnectionFlowControlForThisStream() {
208 stream_contributes_to_connection_flow_control_ = false;
211 private:
212 friend class test::ReliableQuicStreamPeer;
213 friend class QuicStreamUtils;
214 class ProxyAckNotifierDelegate;
216 struct PendingData {
217 PendingData(std::string data_in,
218 scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
219 ~PendingData();
221 // Pending data to be written.
222 std::string data;
223 // Index of the first byte in data still to be written.
224 size_t offset;
225 // Delegate that should be notified when the pending data is acked.
226 // Can be nullptr.
227 scoped_refptr<ProxyAckNotifierDelegate> delegate;
230 // Calls MaybeSendBlocked on the stream's flow controller and the connection
231 // level flow controller. If the stream is flow control blocked by the
232 // connection-level flow controller but not by the stream-level flow
233 // controller, marks this stream as connection-level write blocked.
234 void MaybeSendBlocked();
236 std::list<PendingData> queued_data_;
238 QuicStreamSequencer sequencer_;
239 QuicStreamId id_;
240 // Pointer to the owning QuicSession object.
241 QuicSession* session_;
242 // Bytes read and written refer to payload bytes only: they do not include
243 // framing, encryption overhead etc.
244 uint64 stream_bytes_read_;
245 uint64 stream_bytes_written_;
247 // Stream error code received from a RstStreamFrame or error code sent by the
248 // visitor or sequencer in the RstStreamFrame.
249 QuicRstStreamErrorCode stream_error_;
250 // Connection error code due to which the stream was closed. |stream_error_|
251 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
252 // should check |connection_error_|.
253 QuicErrorCode connection_error_;
255 // True if the read side is closed and further frames should be rejected.
256 bool read_side_closed_;
257 // True if the write side is closed, and further writes should fail.
258 bool write_side_closed_;
260 // True if the subclass has written a FIN with WriteOrBufferData, but it was
261 // buffered in queued_data_ rather than being sent to the session.
262 bool fin_buffered_;
263 // True if a FIN has been sent to the session.
264 bool fin_sent_;
266 // True if this stream has received (and the sequencer has accepted) a
267 // StreamFrame with the FIN set.
268 bool fin_received_;
270 // True if an RST_STREAM has been sent to the session.
271 // In combination with fin_sent_, used to ensure that a FIN and/or a
272 // RST_STREAM is always sent to terminate the stream.
273 bool rst_sent_;
275 // True if this stream has received a RST_STREAM frame.
276 bool rst_received_;
278 // FEC policy to be used for this stream.
279 FecPolicy fec_policy_;
281 // Tracks if the session this stream is running under was created by a
282 // server or a client.
283 Perspective perspective_;
285 QuicFlowController flow_controller_;
287 // The connection level flow controller. Not owned.
288 QuicFlowController* connection_flow_controller_;
290 // Special streams, such as the crypto and headers streams, do not respect
291 // connection level flow control limits (but are stream level flow control
292 // limited).
293 bool stream_contributes_to_connection_flow_control_;
295 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream);
298 } // namespace net
300 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_