• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
6 
7 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_
8 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_
9 
10 #include <sys/types.h>
11 
12 #include <list>
13 
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"
24 
25 namespace net {
26 
27 namespace test {
28 class ReliableQuicStreamPeer;
29 }  // namespace test
30 
31 class QuicSession;
32 
33 class NET_EXPORT_PRIVATE ReliableQuicStream {
34  public:
35   ReliableQuicStream(QuicStreamId id,
36                      QuicSession* session);
37 
38   virtual ~ReliableQuicStream();
39 
40   // Called when a (potentially duplicate) stream frame has been received
41   // for this stream.
42   virtual void OnStreamFrame(const QuicStreamFrame& frame);
43 
44   // Called when the connection becomes writeable to allow the stream
45   // to write any pending data.
46   virtual void OnCanWrite();
47 
48   // Called by the session just before the stream is deleted.
49   virtual void OnClose();
50 
51   // Called when we get a stream reset from the peer.
52   virtual void OnStreamReset(const QuicRstStreamFrame& frame);
53 
54   // Called when we get or send a connection close, and should immediately
55   // close the stream.  This is not passed through the sequencer,
56   // but is handled immediately.
57   virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer);
58 
59   // Called when the final data has been read.
60   virtual void OnFinRead();
61 
62   virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0;
63 
64   // Called to reset the stream from this end.
65   virtual void Reset(QuicRstStreamErrorCode error);
66 
67   // Called to close the entire connection from this end.
68   virtual void CloseConnection(QuicErrorCode error);
69   virtual void CloseConnectionWithDetails(QuicErrorCode error,
70                                           const string& details);
71 
72   // Returns the effective priority for the stream.  This value may change
73   // during the life of the stream.
74   virtual QuicPriority EffectivePriority() const = 0;
75 
id()76   QuicStreamId id() const { return id_; }
77 
stream_error()78   QuicRstStreamErrorCode stream_error() const { return stream_error_; }
connection_error()79   QuicErrorCode connection_error() const { return connection_error_; }
80 
read_side_closed()81   bool read_side_closed() const { return read_side_closed_; }
write_side_closed()82   bool write_side_closed() const { return write_side_closed_; }
83 
stream_bytes_read()84   uint64 stream_bytes_read() const { return stream_bytes_read_; }
stream_bytes_written()85   uint64 stream_bytes_written() const { return stream_bytes_written_; }
86 
87   QuicVersion version() const;
88 
set_fin_sent(bool fin_sent)89   void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
set_rst_sent(bool rst_sent)90   void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
91 
set_fec_policy(FecPolicy fec_policy)92   void set_fec_policy(FecPolicy fec_policy) { fec_policy_ = fec_policy; }
fec_policy()93   FecPolicy fec_policy() const { return fec_policy_; }
94 
95   // Adjust our flow control windows according to new offset in |frame|.
96   virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
97 
98   int num_frames_received() const;
99 
100   int num_duplicate_frames_received() const;
101 
flow_controller()102   QuicFlowController* flow_controller() { return &flow_controller_; }
103 
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(uint64 new_offset);
107   // Called when bytese are sent to the peer.
108   void AddBytesSent(uint64 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(uint64 bytes);
113 
114   // Updates the flow controller's send window offset and calls OnCanWrite if
115   // it was blocked before.
116   void UpdateSendWindowOffset(uint64 new_offset);
117 
118   // Returns true if the stream is flow control blocked, by the stream flow
119   // control window or the connection flow control window.
120   bool IsFlowControlBlocked();
121 
122   // Returns true if we have received either a RST or a FIN - either of which
123   // gives a definitive number of bytes which the peer has sent. If this is not
124   // true on stream termination the session must keep track of the stream's byte
125   // offset until a definitive final value arrives.
HasFinalReceivedByteOffset()126   bool HasFinalReceivedByteOffset() const {
127     return fin_received_ || rst_received_;
128   }
129 
130   // Returns true if the stream has queued data waiting to write.
131   bool HasBufferedData() const;
132 
133  protected:
134   // Sends as much of 'data' to the connection as the connection will consume,
135   // and then buffers any remaining data in queued_data_.
136   void WriteOrBufferData(
137       base::StringPiece data,
138       bool fin,
139       QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
140 
141   // Sends as many bytes in the first |count| buffers of |iov| to the connection
142   // as the connection will consume.
143   // If |ack_notifier_delegate| is provided, then it will be notified once all
144   // the ACKs for this write have been received.
145   // Returns the number of bytes consumed by the connection.
146   QuicConsumedData WritevData(
147       const struct iovec* iov,
148       int iov_count,
149       bool fin,
150       QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
151 
152   // Helper method that returns FecProtection to use for writes to the session.
153   FecProtection GetFecProtection();
154 
155   // Close the read side of the socket.  Further frames will not be accepted.
156   virtual void CloseReadSide();
157 
158   // Close the write side of the socket.  Further writes will fail.
159   void CloseWriteSide();
160 
fin_buffered()161   bool fin_buffered() const { return fin_buffered_; }
162 
session()163   const QuicSession* session() const { return session_; }
session()164   QuicSession* session() { return session_; }
165 
sequencer()166   const QuicStreamSequencer* sequencer() const { return &sequencer_; }
sequencer()167   QuicStreamSequencer* sequencer() { return &sequencer_; }
168 
169   // TODO(rjshade): Remove this method when removing QUIC_VERSION_19.
DisableFlowControl()170   void DisableFlowControl() {
171     flow_controller_.Disable();
172   }
173 
DisableConnectionFlowControlForThisStream()174   void DisableConnectionFlowControlForThisStream() {
175     stream_contributes_to_connection_flow_control_ = false;
176   }
177 
178  private:
179   friend class test::ReliableQuicStreamPeer;
180   friend class QuicStreamUtils;
181   class ProxyAckNotifierDelegate;
182 
183   struct PendingData {
184     PendingData(string data_in,
185                 scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
186     ~PendingData();
187 
188     string data;
189     // Delegate that should be notified when the pending data is acked.
190     // Can be nullptr.
191     scoped_refptr<ProxyAckNotifierDelegate> delegate;
192   };
193 
194   // Calls MaybeSendBlocked on our flow controller, and connection level flow
195   // controller. If we are flow control blocked, marks this stream as write
196   // blocked.
197   void MaybeSendBlocked();
198 
199   std::list<PendingData> queued_data_;
200 
201   QuicStreamSequencer sequencer_;
202   QuicStreamId id_;
203   QuicSession* session_;
204   // Bytes read and written refer to payload bytes only: they do not include
205   // framing, encryption overhead etc.
206   uint64 stream_bytes_read_;
207   uint64 stream_bytes_written_;
208 
209   // Stream error code received from a RstStreamFrame or error code sent by the
210   // visitor or sequencer in the RstStreamFrame.
211   QuicRstStreamErrorCode stream_error_;
212   // Connection error code due to which the stream was closed. |stream_error_|
213   // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
214   // should check |connection_error_|.
215   QuicErrorCode connection_error_;
216 
217   // True if the read side is closed and further frames should be rejected.
218   bool read_side_closed_;
219   // True if the write side is closed, and further writes should fail.
220   bool write_side_closed_;
221 
222   bool fin_buffered_;
223   bool fin_sent_;
224 
225   // True if this stream has received (and the sequencer has accepted) a
226   // StreamFrame with the FIN set.
227   bool fin_received_;
228 
229   // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
230   // always sent before stream termination.
231   bool rst_sent_;
232 
233   // True if this stream has received a RST stream frame.
234   bool rst_received_;
235 
236   // FEC policy to be used for this stream.
237   FecPolicy fec_policy_;
238 
239   // True if the session this stream is running under is a server session.
240   bool is_server_;
241 
242   QuicFlowController flow_controller_;
243 
244   // The connection level flow controller. Not owned.
245   QuicFlowController* connection_flow_controller_;
246 
247   // Special streams, such as the crypto and headers streams, do not respect
248   // connection level flow control limits (but are stream level flow control
249   // limited).
250   bool stream_contributes_to_connection_flow_control_;
251 
252   DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream);
253 };
254 
255 }  // namespace net
256 
257 #endif  // NET_QUIC_RELIABLE_QUIC_STREAM_H_
258