Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / reliable_quic_stream.h
blob7cc26c86d4987369ddacbbdf57db7ab1097360cd
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 stream subclass after it has consumed the final incoming
77 // data.
78 void OnFinRead();
80 // Called when new data is available from the sequencer. Subclasses must
81 // actively retrieve the data using the sequencer's Readv() or
82 // GetReadableRegions() method.
83 virtual void OnDataAvailable() = 0;
85 // Called by the subclass or the sequencer to reset the stream from this
86 // end.
87 virtual void Reset(QuicRstStreamErrorCode error);
89 // Called by the subclass or the sequencer to close the entire connection from
90 // this end.
91 virtual void CloseConnection(QuicErrorCode error);
92 virtual void CloseConnectionWithDetails(QuicErrorCode error,
93 const std::string& details);
95 // Returns the effective priority for the stream. This value may change
96 // during the life of the stream.
97 virtual QuicPriority EffectivePriority() const = 0;
99 QuicStreamId id() const { return id_; }
101 QuicRstStreamErrorCode stream_error() const { return stream_error_; }
102 QuicErrorCode connection_error() const { return connection_error_; }
104 bool read_side_closed() const { return read_side_closed_; }
105 bool write_side_closed() const { return write_side_closed_; }
107 uint64 stream_bytes_read() const { return stream_bytes_read_; }
108 uint64 stream_bytes_written() const { return stream_bytes_written_; }
110 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
111 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
113 void set_fec_policy(FecPolicy fec_policy) { fec_policy_ = fec_policy; }
114 FecPolicy fec_policy() const { return fec_policy_; }
116 // Adjust the flow control window according to new offset in |frame|.
117 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
119 // Used in Chrome.
120 int num_frames_received() const;
121 int num_early_frames_received() const;
122 int num_duplicate_frames_received() const;
124 QuicFlowController* flow_controller() { return &flow_controller_; }
126 // Called when endpoint receives a frame which could increase the highest
127 // offset.
128 // Returns true if the highest offset did increase.
129 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset);
130 // Called when bytes are sent to the peer.
131 void AddBytesSent(QuicByteCount bytes);
132 // Called by the stream sequencer as bytes are consumed from the buffer.
133 // If the receive window has dropped below the threshold, then send a
134 // WINDOW_UPDATE frame.
135 void AddBytesConsumed(QuicByteCount bytes);
137 // Updates the flow controller's send window offset and calls OnCanWrite if
138 // it was blocked before.
139 void UpdateSendWindowOffset(QuicStreamOffset new_offset);
141 // Returns true if the stream have received either a RST_STREAM or a FIN -
142 // either of which gives a definitive number of bytes which the peer has
143 // sent. If this is not true on deletion of the stream object, the session
144 // must keep track of the stream's byte offset until a definitive final value
145 // arrives.
146 bool HasFinalReceivedByteOffset() const {
147 return fin_received_ || rst_received_;
150 // Returns true if the stream has queued data waiting to write.
151 bool HasBufferedData() const;
153 // Returns the version of QUIC being used for this stream.
154 QuicVersion version() const;
156 bool fin_received() const { return fin_received_; }
158 protected:
159 // Sends as much of 'data' to the connection as the connection will consume,
160 // and then buffers any remaining data in queued_data_.
161 // If fin is true: if it is immediately passed on to the session,
162 // write_side_closed() becomes true, otherwise fin_buffered_ becomes true.
163 void WriteOrBufferData(
164 base::StringPiece data,
165 bool fin,
166 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
168 // Sends as many bytes in the first |count| buffers of |iov| to the connection
169 // as the connection will consume.
170 // If |ack_notifier_delegate| is provided, then it will be notified once all
171 // the ACKs for this write have been received.
172 // Returns the number of bytes consumed by the connection.
173 QuicConsumedData WritevData(
174 const struct iovec* iov,
175 int iov_count,
176 bool fin,
177 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
179 // Close the read side of the stream. Further incoming stream frames will be
180 // discarded. Can be called by the subclass or internally.
181 // May cause the stream to be closed.
182 virtual void CloseReadSide();
184 // Close the write side of the socket. Further writes will fail.
185 // Can be called by the subclass or internally.
186 // Does not send a FIN. May cause the stream to be closed.
187 void CloseWriteSide();
189 // Helper method that returns FecProtection to use when writing.
190 FecProtection GetFecProtection();
192 bool fin_buffered() const { return fin_buffered_; }
194 const QuicSession* session() const { return session_; }
195 QuicSession* session() { return session_; }
197 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
198 QuicStreamSequencer* sequencer() { return &sequencer_; }
200 void DisableConnectionFlowControlForThisStream() {
201 stream_contributes_to_connection_flow_control_ = false;
204 private:
205 friend class test::ReliableQuicStreamPeer;
206 friend class QuicStreamUtils;
207 class ProxyAckNotifierDelegate;
209 struct PendingData {
210 PendingData(std::string data_in,
211 scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
212 ~PendingData();
214 // Pending data to be written.
215 std::string data;
216 // Index of the first byte in data still to be written.
217 size_t offset;
218 // Delegate that should be notified when the pending data is acked.
219 // Can be nullptr.
220 scoped_refptr<ProxyAckNotifierDelegate> delegate;
223 // Calls MaybeSendBlocked on the stream's flow controller and the connection
224 // level flow controller. If the stream is flow control blocked by the
225 // connection-level flow controller but not by the stream-level flow
226 // controller, marks this stream as connection-level write blocked.
227 void MaybeSendBlocked();
229 std::list<PendingData> queued_data_;
231 QuicStreamSequencer sequencer_;
232 QuicStreamId id_;
233 // Pointer to the owning QuicSession object.
234 QuicSession* session_;
235 // Bytes read and written refer to payload bytes only: they do not include
236 // framing, encryption overhead etc.
237 uint64 stream_bytes_read_;
238 uint64 stream_bytes_written_;
240 // Stream error code received from a RstStreamFrame or error code sent by the
241 // visitor or sequencer in the RstStreamFrame.
242 QuicRstStreamErrorCode stream_error_;
243 // Connection error code due to which the stream was closed. |stream_error_|
244 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
245 // should check |connection_error_|.
246 QuicErrorCode connection_error_;
248 // True if the read side is closed and further frames should be rejected.
249 bool read_side_closed_;
250 // True if the write side is closed, and further writes should fail.
251 bool write_side_closed_;
253 // True if the subclass has written a FIN with WriteOrBufferData, but it was
254 // buffered in queued_data_ rather than being sent to the session.
255 bool fin_buffered_;
256 // True if a FIN has been sent to the session.
257 bool fin_sent_;
259 // True if this stream has received (and the sequencer has accepted) a
260 // StreamFrame with the FIN set.
261 bool fin_received_;
263 // True if an RST_STREAM has been sent to the session.
264 // In combination with fin_sent_, used to ensure that a FIN and/or a
265 // RST_STREAM is always sent to terminate the stream.
266 bool rst_sent_;
268 // True if this stream has received a RST_STREAM frame.
269 bool rst_received_;
271 // FEC policy to be used for this stream.
272 FecPolicy fec_policy_;
274 // Tracks if the session this stream is running under was created by a
275 // server or a client.
276 Perspective perspective_;
278 QuicFlowController flow_controller_;
280 // The connection level flow controller. Not owned.
281 QuicFlowController* connection_flow_controller_;
283 // Special streams, such as the crypto and headers streams, do not respect
284 // connection level flow control limits (but are stream level flow control
285 // limited).
286 bool stream_contributes_to_connection_flow_control_;
288 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream);
291 } // namespace net
293 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_