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>
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"
29 class ReliableQuicStreamPeer
;
34 class NET_EXPORT_PRIVATE ReliableQuicStream
{
36 ReliableQuicStream(QuicStreamId id
,
37 QuicSession
* session
);
39 virtual ~ReliableQuicStream();
41 // Called when a (potentially duplicate) stream frame has been received
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 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;
100 int num_early_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(QuicStreamOffset new_offset
);
108 // Called when bytese are sent to the peer.
109 void AddBytesSent(QuicByteCount 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(QuicByteCount bytes
);
115 // Updates the flow controller's send window offset and calls OnCanWrite if
116 // it was blocked before.
117 void UpdateSendWindowOffset(QuicStreamOffset new_offset
);
119 // Returns true if the stream is flow control blocked, by the stream flow
120 // control window or the connection flow control window.
121 bool IsFlowControlBlocked();
123 // Returns true if we have received either a RST or a FIN - either of which
124 // gives a definitive number of bytes which the peer has sent. If this is not
125 // true on stream termination the session must keep track of the stream's byte
126 // offset until a definitive final value arrives.
127 bool HasFinalReceivedByteOffset() const {
128 return fin_received_
|| rst_received_
;
131 // Returns true if the stream has queued data waiting to write.
132 bool HasBufferedData() const;
135 // Sends as much of 'data' to the connection as the connection will consume,
136 // and then buffers any remaining data in queued_data_.
137 void WriteOrBufferData(
138 base::StringPiece data
,
140 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
142 // Sends as many bytes in the first |count| buffers of |iov| to the connection
143 // as the connection will consume.
144 // If |ack_notifier_delegate| is provided, then it will be notified once all
145 // the ACKs for this write have been received.
146 // Returns the number of bytes consumed by the connection.
147 QuicConsumedData
WritevData(
148 const struct iovec
* iov
,
151 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
153 // Helper method that returns FecProtection to use for writes to the session.
154 FecProtection
GetFecProtection();
156 // Close the read side of the socket. Further frames will not be accepted.
157 virtual void CloseReadSide();
159 // Close the write side of the socket. Further writes will fail.
160 void CloseWriteSide();
162 bool fin_buffered() const { return fin_buffered_
; }
164 const QuicSession
* session() const { return session_
; }
165 QuicSession
* session() { return session_
; }
167 const QuicStreamSequencer
* sequencer() const { return &sequencer_
; }
168 QuicStreamSequencer
* sequencer() { return &sequencer_
; }
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(std::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_