Add UMA reporting to CdmPromise.
[chromium-blink-merge.git] / net / quic / reliable_quic_stream.h
blobb9aa8ee732c9dc955210ed1fb9936c5ea24dc81c
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 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_
8 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_
10 #include <sys/types.h>
12 #include <list>
14 #include "base/basictypes.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/strings/string_piece.h"
17 #include "net/base/iovec.h"
18 #include "net/base/net_export.h"
19 #include "net/quic/quic_ack_notifier.h"
20 #include "net/quic/quic_flow_controller.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_stream_sequencer.h"
23 #include "net/quic/quic_types.h"
25 namespace net {
27 namespace test {
28 class ReliableQuicStreamPeer;
29 } // namespace test
31 class QuicSession;
33 class NET_EXPORT_PRIVATE ReliableQuicStream {
34 public:
35 ReliableQuicStream(QuicStreamId id,
36 QuicSession* session);
38 virtual ~ReliableQuicStream();
40 // Called when a (potentially duplicate) stream frame has been received
41 // for this stream.
42 virtual void OnStreamFrame(const QuicStreamFrame& frame);
44 // Called when the connection becomes writeable to allow the stream
45 // to write any pending data.
46 virtual void OnCanWrite();
48 // Called by the session just before the stream is deleted.
49 virtual void OnClose();
51 // Called when we get a stream reset from the peer.
52 virtual void OnStreamReset(const QuicRstStreamFrame& frame);
54 // Called when we get or send a connection close, and should immediately
55 // close the stream. This is not passed through the sequencer,
56 // but is handled immediately.
57 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer);
59 // Called when the final data has been read.
60 virtual void OnFinRead();
62 virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0;
64 // Called to reset the stream from this end.
65 virtual void Reset(QuicRstStreamErrorCode error);
67 // Called to close the entire connection from this end.
68 virtual void CloseConnection(QuicErrorCode error);
69 virtual void CloseConnectionWithDetails(QuicErrorCode error,
70 const string& details);
72 // Returns the effective priority for the stream. This value may change
73 // during the life of the stream.
74 virtual QuicPriority EffectivePriority() const = 0;
76 QuicStreamId id() const { return id_; }
78 QuicRstStreamErrorCode stream_error() const { return stream_error_; }
79 QuicErrorCode connection_error() const { return connection_error_; }
81 bool read_side_closed() const { return read_side_closed_; }
82 bool write_side_closed() const { return write_side_closed_; }
84 uint64 stream_bytes_read() const { return stream_bytes_read_; }
85 uint64 stream_bytes_written() const { return stream_bytes_written_; }
87 QuicVersion version() const;
89 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
90 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
92 void set_fec_policy(FecPolicy fec_policy) { fec_policy_ = fec_policy; }
93 FecPolicy fec_policy() const { return fec_policy_; }
95 // Adjust our flow control windows according to new offset in |frame|.
96 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
98 int num_frames_received() const;
100 int num_duplicate_frames_received() const;
102 QuicFlowController* flow_controller() { return &flow_controller_; }
104 // Called when we see a frame which could increase the highest offset.
105 // Returns true if the highest offset did increase.
106 bool MaybeIncreaseHighestReceivedOffset(uint64 new_offset);
107 // Called when bytese are sent to the peer.
108 void AddBytesSent(uint64 bytes);
109 // Called by the stream sequencer as bytes are consumed from the buffer.
110 // If our receive window has dropped below the threshold, then send a
111 // WINDOW_UPDATE frame.
112 void AddBytesConsumed(uint64 bytes);
114 // Returns true if the stream is flow control blocked, by the stream flow
115 // control window or the connection flow control window.
116 bool IsFlowControlBlocked();
118 // Returns true if we have received either a RST or a FIN - either of which
119 // gives a definitive number of bytes which the peer has sent. If this is not
120 // true on stream termination the session must keep track of the stream's byte
121 // offset until a definitive final value arrives.
122 bool HasFinalReceivedByteOffset() const {
123 return fin_received_ || rst_received_;
126 protected:
127 // Sends as much of 'data' to the connection as the connection will consume,
128 // and then buffers any remaining data in queued_data_.
129 void WriteOrBufferData(
130 base::StringPiece data,
131 bool fin,
132 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
134 // Sends as many bytes in the first |count| buffers of |iov| to the connection
135 // as the connection will consume.
136 // If |ack_notifier_delegate| is provided, then it will be notified once all
137 // the ACKs for this write have been received.
138 // Returns the number of bytes consumed by the connection.
139 QuicConsumedData WritevData(
140 const struct iovec* iov,
141 int iov_count,
142 bool fin,
143 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
145 // Helper method that returns FecProtection to use for writes to the session.
146 FecProtection GetFecProtection();
148 // Close the read side of the socket. Further frames will not be accepted.
149 virtual void CloseReadSide();
151 // Close the write side of the socket. Further writes will fail.
152 void CloseWriteSide();
154 bool HasBufferedData() const;
156 bool fin_buffered() const { return fin_buffered_; }
158 const QuicSession* session() const { return session_; }
159 QuicSession* session() { return session_; }
161 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
162 QuicStreamSequencer* sequencer() { return &sequencer_; }
164 // TODO(rjshade): Remove this method when removing QUIC_VERSION_20.
165 void DisableFlowControl() {
166 flow_controller_.Disable();
169 void DisableConnectionFlowControlForThisStream() {
170 stream_contributes_to_connection_flow_control_ = false;
173 private:
174 friend class test::ReliableQuicStreamPeer;
175 friend class QuicStreamUtils;
176 class ProxyAckNotifierDelegate;
178 struct PendingData {
179 PendingData(string data_in,
180 scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
181 ~PendingData();
183 string data;
184 // Delegate that should be notified when the pending data is acked.
185 // Can be nullptr.
186 scoped_refptr<ProxyAckNotifierDelegate> delegate;
189 // Calls MaybeSendBlocked on our flow controller, and connection level flow
190 // controller. If we are flow control blocked, marks this stream as write
191 // blocked.
192 void MaybeSendBlocked();
194 std::list<PendingData> queued_data_;
196 QuicStreamSequencer sequencer_;
197 QuicStreamId id_;
198 QuicSession* session_;
199 // Bytes read and written refer to payload bytes only: they do not include
200 // framing, encryption overhead etc.
201 uint64 stream_bytes_read_;
202 uint64 stream_bytes_written_;
204 // Stream error code received from a RstStreamFrame or error code sent by the
205 // visitor or sequencer in the RstStreamFrame.
206 QuicRstStreamErrorCode stream_error_;
207 // Connection error code due to which the stream was closed. |stream_error_|
208 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
209 // should check |connection_error_|.
210 QuicErrorCode connection_error_;
212 // True if the read side is closed and further frames should be rejected.
213 bool read_side_closed_;
214 // True if the write side is closed, and further writes should fail.
215 bool write_side_closed_;
217 bool fin_buffered_;
218 bool fin_sent_;
220 // True if this stream has received (and the sequencer has accepted) a
221 // StreamFrame with the FIN set.
222 bool fin_received_;
224 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
225 // always sent before stream termination.
226 bool rst_sent_;
228 // True if this stream has received a RST stream frame.
229 bool rst_received_;
231 // FEC policy to be used for this stream.
232 FecPolicy fec_policy_;
234 // True if the session this stream is running under is a server session.
235 bool is_server_;
237 QuicFlowController flow_controller_;
239 // The connection level flow controller. Not owned.
240 QuicFlowController* connection_flow_controller_;
242 // Special streams, such as the crypto and headers streams, do not respect
243 // connection level flow control limits (but are stream level flow control
244 // limited).
245 bool stream_contributes_to_connection_flow_control_;
247 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream);
250 } // namespace net
252 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_