• 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.  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);
44 
45   // Called when the connection becomes writeable to allow the stream
46   // to write any pending data.
47   virtual void OnCanWrite();
48 
49   // Called by the session just before the stream is deleted.
50   virtual void OnClose();
51 
52   // Called when we get a stream reset from the peer.
53   virtual void OnStreamReset(const QuicRstStreamFrame& frame);
54 
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);
59 
60   // Called when the final data has been read.
61   virtual void OnFinRead();
62 
63   virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0;
64 
65   // Called to reset the stream from this end.
66   virtual void Reset(QuicRstStreamErrorCode error);
67 
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);
72 
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;
76 
id()77   QuicStreamId id() const { return id_; }
78 
stream_error()79   QuicRstStreamErrorCode stream_error() const { return stream_error_; }
connection_error()80   QuicErrorCode connection_error() const { return connection_error_; }
81 
read_side_closed()82   bool read_side_closed() const { return read_side_closed_; }
write_side_closed()83   bool write_side_closed() const { return write_side_closed_; }
84 
stream_bytes_read()85   uint64 stream_bytes_read() const { return stream_bytes_read_; }
stream_bytes_written()86   uint64 stream_bytes_written() const { return stream_bytes_written_; }
87 
88   QuicVersion version() const;
89 
set_fin_sent(bool fin_sent)90   void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
set_rst_sent(bool rst_sent)91   void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
92 
93   // Adjust our flow control windows according to new offset in |frame|.
94   virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
95 
96   int num_frames_received() const;
97 
98   int num_duplicate_frames_received() const;
99 
flow_controller()100   QuicFlowController* flow_controller() { return &flow_controller_; }
101 
102   // Called when we see a frame which could increase the highest offset.
103   // Returns true if the highest offset did increase.
104   bool MaybeIncreaseHighestReceivedOffset(uint64 new_offset);
105   // Called when bytese are sent to the peer.
106   void AddBytesSent(uint64 bytes);
107   // Called by the stream sequencer as bytes are consumed from the buffer.
108   // If our receive window has dropped below the threshold, then send a
109   // WINDOW_UPDATE frame.
110   void AddBytesConsumed(uint64 bytes);
111 
112   // Returns true if the stream is flow control blocked, by the stream flow
113   // control window or the connection flow control window.
114   bool IsFlowControlBlocked();
115 
116   // Returns true if we have received either a RST or a FIN - either of which
117   // gives a definitive number of bytes which the peer has sent. If this is not
118   // true on stream termination the session must keep track of the stream's byte
119   // offset until a definitive final value arrives.
HasFinalReceivedByteOffset()120   bool HasFinalReceivedByteOffset() const {
121     return fin_received_ || rst_received_;
122   }
123 
124  protected:
125   // Sends as much of 'data' to the connection as the connection will consume,
126   // and then buffers any remaining data in queued_data_.
127   void WriteOrBufferData(
128       base::StringPiece data,
129       bool fin,
130       QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
131 
132   // Sends as many bytes in the first |count| buffers of |iov| to the connection
133   // as the connection will consume.
134   // If |ack_notifier_delegate| is provided, then it will be notified once all
135   // the ACKs for this write have been received.
136   // Returns the number of bytes consumed by the connection.
137   QuicConsumedData WritevData(
138       const struct iovec* iov,
139       int iov_count,
140       bool fin,
141       QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
142 
143   // Helper method that returns FecProtection to use for writes to the session.
144   FecProtection GetFecProtection();
145 
146   // Close the read side of the socket.  Further frames will not be accepted.
147   virtual void CloseReadSide();
148 
149   // Close the write side of the socket.  Further writes will fail.
150   void CloseWriteSide();
151 
152   bool HasBufferedData() const;
153 
fin_buffered()154   bool fin_buffered() const { return fin_buffered_; }
155 
set_fec_policy(FecPolicy fec_policy)156   void set_fec_policy(FecPolicy fec_policy) { fec_policy_ = fec_policy; }
157 
session()158   const QuicSession* session() const { return session_; }
session()159   QuicSession* session() { return session_; }
160 
sequencer()161   const QuicStreamSequencer* sequencer() const { return &sequencer_; }
sequencer()162   QuicStreamSequencer* sequencer() { return &sequencer_; }
163 
DisableFlowControl()164   void DisableFlowControl() {
165     flow_controller_.Disable();
166   }
167 
168  private:
169   friend class test::ReliableQuicStreamPeer;
170   friend class QuicStreamUtils;
171   class ProxyAckNotifierDelegate;
172 
173   struct PendingData {
174     PendingData(string data_in,
175                 scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
176     ~PendingData();
177 
178     string data;
179     // Delegate that should be notified when the pending data is acked.
180     // Can be nullptr.
181     scoped_refptr<ProxyAckNotifierDelegate> delegate;
182   };
183 
184   // Calls MaybeSendBlocked on our flow controller, and connection level flow
185   // controller. If we are flow control blocked, marks this stream as write
186   // blocked.
187   void MaybeSendBlocked();
188 
189   std::list<PendingData> queued_data_;
190 
191   QuicStreamSequencer sequencer_;
192   QuicStreamId id_;
193   QuicSession* session_;
194   // Bytes read and written refer to payload bytes only: they do not include
195   // framing, encryption overhead etc.
196   uint64 stream_bytes_read_;
197   uint64 stream_bytes_written_;
198 
199   // Stream error code received from a RstStreamFrame or error code sent by the
200   // visitor or sequencer in the RstStreamFrame.
201   QuicRstStreamErrorCode stream_error_;
202   // Connection error code due to which the stream was closed. |stream_error_|
203   // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
204   // should check |connection_error_|.
205   QuicErrorCode connection_error_;
206 
207   // True if the read side is closed and further frames should be rejected.
208   bool read_side_closed_;
209   // True if the write side is closed, and further writes should fail.
210   bool write_side_closed_;
211 
212   bool fin_buffered_;
213   bool fin_sent_;
214 
215   // True if this stream has received (and the sequencer has accepted) a
216   // StreamFrame with the FIN set.
217   bool fin_received_;
218 
219   // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
220   // always sent before stream termination.
221   bool rst_sent_;
222 
223   // True if this stream has received a RST stream frame.
224   bool rst_received_;
225 
226   // FEC policy to be used for this stream.
227   FecPolicy fec_policy_;
228 
229   // True if the session this stream is running under is a server session.
230   bool is_server_;
231 
232   QuicFlowController flow_controller_;
233 
234   // The connection level flow controller. Not owned.
235   QuicFlowController* connection_flow_controller_;
236 
237   DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream);
238 };
239 
240 }  // namespace net
241 
242 #endif  // NET_QUIC_RELIABLE_QUIC_STREAM_H_
243