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 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
);
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;
130 // Sends as much of 'data' to the connection as the connection will consume,
131 // and then buffers any remaining data in queued_data_.
132 void WriteOrBufferData(
133 base::StringPiece data
,
135 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
137 // Sends as many bytes in the first |count| buffers of |iov| to the connection
138 // as the connection will consume.
139 // If |ack_notifier_delegate| is provided, then it will be notified once all
140 // the ACKs for this write have been received.
141 // Returns the number of bytes consumed by the connection.
142 QuicConsumedData
WritevData(
143 const struct iovec
* iov
,
146 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
148 // Helper method that returns FecProtection to use for writes to the session.
149 FecProtection
GetFecProtection();
151 // Close the read side of the socket. Further frames will not be accepted.
152 virtual void CloseReadSide();
154 // Close the write side of the socket. Further writes will fail.
155 void CloseWriteSide();
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 void DisableConnectionFlowControlForThisStream() {
166 stream_contributes_to_connection_flow_control_
= false;
170 friend class test::ReliableQuicStreamPeer
;
171 friend class QuicStreamUtils
;
172 class ProxyAckNotifierDelegate
;
175 PendingData(std::string data_in
,
176 scoped_refptr
<ProxyAckNotifierDelegate
> delegate_in
);
180 // Delegate that should be notified when the pending data is acked.
182 scoped_refptr
<ProxyAckNotifierDelegate
> delegate
;
185 // Calls MaybeSendBlocked on our flow controller, and connection level flow
186 // controller. If we are flow control blocked, marks this stream as write
188 void MaybeSendBlocked();
190 std::list
<PendingData
> queued_data_
;
192 QuicStreamSequencer sequencer_
;
194 QuicSession
* session_
;
195 // Bytes read and written refer to payload bytes only: they do not include
196 // framing, encryption overhead etc.
197 uint64 stream_bytes_read_
;
198 uint64 stream_bytes_written_
;
200 // Stream error code received from a RstStreamFrame or error code sent by the
201 // visitor or sequencer in the RstStreamFrame.
202 QuicRstStreamErrorCode stream_error_
;
203 // Connection error code due to which the stream was closed. |stream_error_|
204 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
205 // should check |connection_error_|.
206 QuicErrorCode connection_error_
;
208 // True if the read side is closed and further frames should be rejected.
209 bool read_side_closed_
;
210 // True if the write side is closed, and further writes should fail.
211 bool write_side_closed_
;
216 // True if this stream has received (and the sequencer has accepted) a
217 // StreamFrame with the FIN set.
220 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
221 // always sent before stream termination.
224 // True if this stream has received a RST stream frame.
227 // FEC policy to be used for this stream.
228 FecPolicy fec_policy_
;
230 // Tracks if the session this stream is running under was created by a
231 // server or a client.
232 Perspective perspective_
;
234 QuicFlowController flow_controller_
;
236 // The connection level flow controller. Not owned.
237 QuicFlowController
* connection_flow_controller_
;
239 // Special streams, such as the crypto and headers streams, do not respect
240 // connection level flow control limits (but are stream level flow control
242 bool stream_contributes_to_connection_flow_control_
;
244 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream
);
249 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_