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_protocol.h"
21 #include "net/quic/quic_stream_sequencer.h"
26 class ReliableQuicStreamPeer
;
33 class NET_EXPORT_PRIVATE ReliableQuicStream
{
35 ReliableQuicStream(QuicStreamId id
,
36 QuicSession
* session
);
38 virtual ~ReliableQuicStream();
40 bool WillAcceptStreamFrame(const QuicStreamFrame
& frame
) const;
42 // Called when a (potentially duplicate) stream frame has been received
43 // for this stream. Returns false if this frame can not be accepted
44 // because there is too much data already buffered.
45 virtual bool OnStreamFrame(const QuicStreamFrame
& frame
);
47 // Called when the connection becomes writeable to allow the stream
48 // to write any pending data.
49 virtual void OnCanWrite();
51 // Called by the session just before the stream is deleted.
52 virtual void OnClose();
54 // Called when we get a stream reset from the peer.
55 virtual void OnStreamReset(const QuicRstStreamFrame
& frame
);
57 // Called when we get or send a connection close, and should immediately
58 // close the stream. This is not passed through the sequencer,
59 // but is handled immediately.
60 virtual void OnConnectionClosed(QuicErrorCode error
, bool from_peer
);
62 // Called when the final data has been read.
63 virtual void OnFinRead();
65 virtual uint32
ProcessRawData(const char* data
, uint32 data_len
) = 0;
67 // Called to reset the stream from this end.
68 virtual void Reset(QuicRstStreamErrorCode error
);
70 // Called to close the entire connection from this end.
71 virtual void CloseConnection(QuicErrorCode error
);
72 virtual void CloseConnectionWithDetails(QuicErrorCode error
,
73 const string
& details
);
75 // Returns the effective priority for the stream. This value may change
76 // during the life of the stream.
77 virtual QuicPriority
EffectivePriority() const = 0;
79 QuicStreamId
id() const { return id_
; }
81 QuicRstStreamErrorCode
stream_error() const { return stream_error_
; }
82 QuicErrorCode
connection_error() const { return connection_error_
; }
84 bool read_side_closed() const { return read_side_closed_
; }
85 bool write_side_closed() const { return write_side_closed_
; }
87 uint64
stream_bytes_read() const { return stream_bytes_read_
; }
88 uint64
stream_bytes_written() const { return stream_bytes_written_
; }
90 QuicVersion
version() const;
92 void set_fin_sent(bool fin_sent
) { fin_sent_
= fin_sent
; }
93 void set_rst_sent(bool rst_sent
) { rst_sent_
= rst_sent
; }
95 // Adjust our flow control windows according to new offset in |frame|.
96 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame
& frame
);
98 // True if this stream is blocked from writing due to flow control limits.
99 bool IsFlowControlBlocked() const;
101 // Updates our send window offset (if offset larger).
102 void UpdateFlowControlSendLimit(QuicStreamOffset offset
);
104 // If our receive window has dropped below the threshold, then send a
105 // WINDOW_UPDATE frame. This is called whenever bytes are consumed from the
106 // sequencer's buffer.
107 void MaybeSendWindowUpdate();
110 // Sends as much of 'data' to the connection as the connection will consume,
111 // and then buffers any remaining data in queued_data_.
112 void WriteOrBufferData(
113 base::StringPiece data
,
115 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
117 // Sends as many bytes in the first |count| buffers of |iov| to the connection
118 // as the connection will consume.
119 // If |ack_notifier_delegate| is provided, then it will be notified once all
120 // the ACKs for this write have been received.
121 // Returns the number of bytes consumed by the connection.
122 QuicConsumedData
WritevData(
123 const struct iovec
* iov
,
126 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
128 // Close the read side of the socket. Further frames will not be accepted.
129 virtual void CloseReadSide();
131 // Close the write side of the socket. Further writes will fail.
132 void CloseWriteSide();
134 bool HasBufferedData();
136 bool fin_buffered() { return fin_buffered_
; }
138 const QuicSession
* session() const { return session_
; }
139 QuicSession
* session() { return session_
; }
141 const QuicStreamSequencer
* sequencer() const { return &sequencer_
; }
142 QuicStreamSequencer
* sequencer() { return &sequencer_
; }
144 // Returns true if flow control is enabled for this stream.
145 virtual bool IsFlowControlEnabled() const = 0;
148 friend class test::ReliableQuicStreamPeer
;
149 friend class QuicStreamUtils
;
150 class ProxyAckNotifierDelegate
;
153 PendingData(string data_in
,
154 scoped_refptr
<ProxyAckNotifierDelegate
> delegate_in
);
158 // Delegate that should be notified when the pending data is acked.
160 scoped_refptr
<ProxyAckNotifierDelegate
> delegate
;
163 // Calculates and returns available flow control send window.
164 uint64
SendWindowSize() const;
166 // Calculates and returns total number of bytes this stream has received.
167 uint64
TotalReceivedBytes() const;
169 std::list
<PendingData
> queued_data_
;
171 QuicStreamSequencer sequencer_
;
173 QuicSession
* session_
;
174 // Bytes read and written refer to payload bytes only: they do not include
175 // framing, encryption overhead etc.
176 uint64 stream_bytes_read_
;
177 uint64 stream_bytes_written_
;
179 // Stream error code received from a RstStreamFrame or error code sent by the
180 // visitor or sequencer in the RstStreamFrame.
181 QuicRstStreamErrorCode stream_error_
;
182 // Connection error code due to which the stream was closed. |stream_error_|
183 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
184 // should check |connection_error_|.
185 QuicErrorCode connection_error_
;
187 // Stream level flow control.
188 // This stream is allowed to send up to flow_control_send_limit_ bytes. Once
189 // it has reached this limit it must not send more data until it receives a
190 // suitable WINDOW_UPDATE frame from the peer.
191 QuicStreamOffset flow_control_send_limit_
;
193 // Stream level flow control.
194 // The maximum size of the stream receive window. Used to determine by how
195 // much we should increase the window offset when sending a WINDOW_UPDATE.
196 uint64 max_flow_control_receive_window_bytes_
;
198 // Stream level flow control.
199 // This stream expects to receive up to receive_window_offset_bytes_.
200 // If the peer sends more than this (without sending us a WINDOW_UPDATE frame
201 // first), then this is a flow control error.
202 QuicStreamOffset flow_control_receive_window_offset_bytes_
;
204 // True if the read side is closed and further frames should be rejected.
205 bool read_side_closed_
;
206 // True if the write side is closed, and further writes should fail.
207 bool write_side_closed_
;
212 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
213 // always sent before stream termination.
216 // True if the session this stream is running under is a server session.
219 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream
);
224 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_