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.
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>
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"
28 class ReliableQuicStreamPeer
;
33 class NET_EXPORT_PRIVATE ReliableQuicStream
{
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. Returns false if this frame can not be accepted
42 // because there is too much data already buffered.
43 virtual bool 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 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 QuicVersion
version() const;
90 void set_fin_sent(bool fin_sent
) { fin_sent_
= fin_sent
; }
91 void set_rst_sent(bool rst_sent
) { rst_sent_
= rst_sent
; }
93 void set_fec_policy(FecPolicy fec_policy
) { fec_policy_
= fec_policy
; }
94 FecPolicy
fec_policy() const { return fec_policy_
; }
96 // Adjust our flow control windows according to new offset in |frame|.
97 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
);
99 int num_frames_received() const;
101 int num_duplicate_frames_received() const;
103 QuicFlowController
* flow_controller() { return &flow_controller_
; }
105 // Called when we see a frame which could increase the highest offset.
106 // Returns true if the highest offset did increase.
107 bool MaybeIncreaseHighestReceivedOffset(uint64 new_offset
);
108 // Called when bytese are sent to the peer.
109 void AddBytesSent(uint64 bytes
);
110 // Called by the stream sequencer as bytes are consumed from the buffer.
111 // If our receive window has dropped below the threshold, then send a
112 // WINDOW_UPDATE frame.
113 void AddBytesConsumed(uint64 bytes
);
115 // Returns true if the stream is flow control blocked, by the stream flow
116 // control window or the connection flow control window.
117 bool IsFlowControlBlocked();
119 // Returns true if we have received either a RST or a FIN - either of which
120 // gives a definitive number of bytes which the peer has sent. If this is not
121 // true on stream termination the session must keep track of the stream's byte
122 // offset until a definitive final value arrives.
123 bool HasFinalReceivedByteOffset() const {
124 return fin_received_
|| rst_received_
;
128 // Sends as much of 'data' to the connection as the connection will consume,
129 // and then buffers any remaining data in queued_data_.
130 void WriteOrBufferData(
131 base::StringPiece data
,
133 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
135 // Sends as many bytes in the first |count| buffers of |iov| to the connection
136 // as the connection will consume.
137 // If |ack_notifier_delegate| is provided, then it will be notified once all
138 // the ACKs for this write have been received.
139 // Returns the number of bytes consumed by the connection.
140 QuicConsumedData
WritevData(
141 const struct iovec
* iov
,
144 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
146 // Helper method that returns FecProtection to use for writes to the session.
147 FecProtection
GetFecProtection();
149 // Close the read side of the socket. Further frames will not be accepted.
150 virtual void CloseReadSide();
152 // Close the write side of the socket. Further writes will fail.
153 void CloseWriteSide();
155 bool HasBufferedData() const;
157 bool fin_buffered() const { return fin_buffered_
; }
159 const QuicSession
* session() const { return session_
; }
160 QuicSession
* session() { return session_
; }
162 const QuicStreamSequencer
* sequencer() const { return &sequencer_
; }
163 QuicStreamSequencer
* sequencer() { return &sequencer_
; }
165 // TODO(rjshade): Remove this method when removing QUIC_VERSION_20.
166 void DisableFlowControl() {
167 flow_controller_
.Disable();
170 void DisableConnectionFlowControlForThisStream() {
171 stream_contributes_to_connection_flow_control_
= false;
175 friend class test::ReliableQuicStreamPeer
;
176 friend class QuicStreamUtils
;
177 class ProxyAckNotifierDelegate
;
180 PendingData(string data_in
,
181 scoped_refptr
<ProxyAckNotifierDelegate
> delegate_in
);
185 // Delegate that should be notified when the pending data is acked.
187 scoped_refptr
<ProxyAckNotifierDelegate
> delegate
;
190 // Calls MaybeSendBlocked on our flow controller, and connection level flow
191 // controller. If we are flow control blocked, marks this stream as write
193 void MaybeSendBlocked();
195 std::list
<PendingData
> queued_data_
;
197 QuicStreamSequencer sequencer_
;
199 QuicSession
* session_
;
200 // Bytes read and written refer to payload bytes only: they do not include
201 // framing, encryption overhead etc.
202 uint64 stream_bytes_read_
;
203 uint64 stream_bytes_written_
;
205 // Stream error code received from a RstStreamFrame or error code sent by the
206 // visitor or sequencer in the RstStreamFrame.
207 QuicRstStreamErrorCode stream_error_
;
208 // Connection error code due to which the stream was closed. |stream_error_|
209 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
210 // should check |connection_error_|.
211 QuicErrorCode connection_error_
;
213 // True if the read side is closed and further frames should be rejected.
214 bool read_side_closed_
;
215 // True if the write side is closed, and further writes should fail.
216 bool write_side_closed_
;
221 // True if this stream has received (and the sequencer has accepted) a
222 // StreamFrame with the FIN set.
225 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
226 // always sent before stream termination.
229 // True if this stream has received a RST stream frame.
232 // FEC policy to be used for this stream.
233 FecPolicy fec_policy_
;
235 // True if the session this stream is running under is a server session.
238 QuicFlowController flow_controller_
;
240 // The connection level flow controller. Not owned.
241 QuicFlowController
* connection_flow_controller_
;
243 // Special streams, such as the crypto and headers streams, do not respect
244 // connection level flow control limits (but are stream level flow control
246 bool stream_contributes_to_connection_flow_control_
;
248 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream
);
253 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_