Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / reliable_quic_stream.h
blob881bd70d2f864459c70996d25f7f4ec22a57142e
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>
13 #include <string>
15 #include "base/basictypes.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/strings/string_piece.h"
18 #include "net/base/iovec.h"
19 #include "net/base/net_export.h"
20 #include "net/quic/quic_ack_notifier.h"
21 #include "net/quic/quic_flow_controller.h"
22 #include "net/quic/quic_protocol.h"
23 #include "net/quic/quic_stream_sequencer.h"
24 #include "net/quic/quic_types.h"
26 namespace net {
28 namespace test {
29 class ReliableQuicStreamPeer;
30 } // namespace test
32 class QuicSession;
34 class NET_EXPORT_PRIVATE ReliableQuicStream {
35 public:
36 ReliableQuicStream(QuicStreamId id,
37 QuicSession* session);
39 virtual ~ReliableQuicStream();
41 // Called when a (potentially duplicate) stream frame has been received
42 // for this stream.
43 virtual void OnStreamFrame(const QuicStreamFrame& frame);
45 // Called when the connection becomes writeable to allow the stream
46 // to write any pending data.
47 virtual void OnCanWrite();
49 // Called by the session just before the stream is deleted.
50 virtual void OnClose();
52 // Called when we get a stream reset from the peer.
53 virtual void OnStreamReset(const QuicRstStreamFrame& frame);
55 // Called when we get or send a connection close, and should immediately
56 // close the stream. This is not passed through the sequencer,
57 // but is handled immediately.
58 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer);
60 // Called when the final data has been read.
61 virtual void OnFinRead();
63 virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0;
65 // Called to reset the stream from this end.
66 virtual void Reset(QuicRstStreamErrorCode error);
68 // Called to close the entire connection from this end.
69 virtual void CloseConnection(QuicErrorCode error);
70 virtual void CloseConnectionWithDetails(QuicErrorCode error,
71 const std::string& details);
73 // Returns the effective priority for the stream. This value may change
74 // during the life of the stream.
75 virtual QuicPriority EffectivePriority() const = 0;
77 QuicStreamId id() const { return id_; }
79 QuicRstStreamErrorCode stream_error() const { return stream_error_; }
80 QuicErrorCode connection_error() const { return connection_error_; }
82 bool read_side_closed() const { return read_side_closed_; }
83 bool write_side_closed() const { return write_side_closed_; }
85 uint64 stream_bytes_read() const { return stream_bytes_read_; }
86 uint64 stream_bytes_written() const { return stream_bytes_written_; }
88 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
89 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
91 void set_fec_policy(FecPolicy fec_policy) { fec_policy_ = fec_policy; }
92 FecPolicy fec_policy() const { return fec_policy_; }
94 // Adjust our flow control windows according to new offset in |frame|.
95 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
97 // Used in Chrome.
98 int num_frames_received() const;
99 int num_early_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(QuicStreamOffset new_offset);
107 // Called when bytese are sent to the peer.
108 void AddBytesSent(QuicByteCount 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(QuicByteCount bytes);
114 // Updates the flow controller's send window offset and calls OnCanWrite if
115 // it was blocked before.
116 void UpdateSendWindowOffset(QuicStreamOffset new_offset);
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 // Returns true if the stream has queued data waiting to write.
127 bool HasBufferedData() const;
129 // Returns the version of QUIC being used for this stream.
130 QuicVersion version() const;
132 protected:
133 // Sends as much of 'data' to the connection as the connection will consume,
134 // and then buffers any remaining data in queued_data_.
135 void WriteOrBufferData(
136 base::StringPiece data,
137 bool fin,
138 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
140 // Sends as many bytes in the first |count| buffers of |iov| to the connection
141 // as the connection will consume.
142 // If |ack_notifier_delegate| is provided, then it will be notified once all
143 // the ACKs for this write have been received.
144 // Returns the number of bytes consumed by the connection.
145 QuicConsumedData WritevData(
146 const struct iovec* iov,
147 int iov_count,
148 bool fin,
149 QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
151 // Helper method that returns FecProtection to use for writes to the session.
152 FecProtection GetFecProtection();
154 // Close the read side of the socket. Further frames will not be accepted.
155 virtual void CloseReadSide();
157 // Close the write side of the socket. Further writes will fail.
158 void CloseWriteSide();
160 bool fin_buffered() const { return fin_buffered_; }
162 const QuicSession* session() const { return session_; }
163 QuicSession* session() { return session_; }
165 const QuicStreamSequencer* sequencer() const { return &sequencer_; }
166 QuicStreamSequencer* sequencer() { return &sequencer_; }
168 void DisableConnectionFlowControlForThisStream() {
169 stream_contributes_to_connection_flow_control_ = false;
172 private:
173 friend class test::ReliableQuicStreamPeer;
174 friend class QuicStreamUtils;
175 class ProxyAckNotifierDelegate;
177 struct PendingData {
178 PendingData(std::string data_in,
179 scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
180 ~PendingData();
182 std::string data;
183 // Delegate that should be notified when the pending data is acked.
184 // Can be nullptr.
185 scoped_refptr<ProxyAckNotifierDelegate> delegate;
188 // Calls MaybeSendBlocked on our flow controller, and connection level flow
189 // controller. If we are flow control blocked, marks this stream as write
190 // blocked.
191 void MaybeSendBlocked();
193 std::list<PendingData> queued_data_;
195 QuicStreamSequencer sequencer_;
196 QuicStreamId id_;
197 QuicSession* session_;
198 // Bytes read and written refer to payload bytes only: they do not include
199 // framing, encryption overhead etc.
200 uint64 stream_bytes_read_;
201 uint64 stream_bytes_written_;
203 // Stream error code received from a RstStreamFrame or error code sent by the
204 // visitor or sequencer in the RstStreamFrame.
205 QuicRstStreamErrorCode stream_error_;
206 // Connection error code due to which the stream was closed. |stream_error_|
207 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
208 // should check |connection_error_|.
209 QuicErrorCode connection_error_;
211 // True if the read side is closed and further frames should be rejected.
212 bool read_side_closed_;
213 // True if the write side is closed, and further writes should fail.
214 bool write_side_closed_;
216 bool fin_buffered_;
217 bool fin_sent_;
219 // True if this stream has received (and the sequencer has accepted) a
220 // StreamFrame with the FIN set.
221 bool fin_received_;
223 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
224 // always sent before stream termination.
225 bool rst_sent_;
227 // True if this stream has received a RST stream frame.
228 bool rst_received_;
230 // FEC policy to be used for this stream.
231 FecPolicy fec_policy_;
233 // Tracks if the session this stream is running under was created by a
234 // server or a client.
235 Perspective perspective_;
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_