reliable_quic_stream.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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
24namespace net {
25
26namespace test {
27class ReliableQuicStreamPeer;
28}  // namespace test
29
30class QuicSession;
31
32class NET_EXPORT_PRIVATE ReliableQuicStream {
33 public:
34  ReliableQuicStream(QuicStreamId id,
35                     QuicSession* session);
36
37  virtual ~ReliableQuicStream();
38
39  bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const;
40
41  // Called when a (potentially duplicate) stream frame has been received
42  // for this stream.  Returns false if this frame can not be accepted
43  // because there is too much data already buffered.
44  virtual bool OnStreamFrame(const QuicStreamFrame& frame);
45
46  // Called when the connection becomes writeable to allow the stream
47  // to write any pending data.
48  virtual void OnCanWrite();
49
50  // Called by the session just before the stream is deleted.
51  virtual void OnClose();
52
53  // Called when we get a stream reset from the peer.
54  virtual void OnStreamReset(const QuicRstStreamFrame& frame);
55
56  // Called when we get or send a connection close, and should immediately
57  // close the stream.  This is not passed through the sequencer,
58  // but is handled immediately.
59  virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer);
60
61  // Called when the final data has been read.
62  virtual void OnFinRead();
63
64  virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0;
65
66  // Called to reset the stream from this end.
67  virtual void Reset(QuicRstStreamErrorCode error);
68
69  // Called to close the entire connection from this end.
70  virtual void CloseConnection(QuicErrorCode error);
71  virtual void CloseConnectionWithDetails(QuicErrorCode error,
72                                          const string& details);
73
74  // Returns the effective priority for the stream.  This value may change
75  // during the life of the stream.
76  virtual QuicPriority EffectivePriority() const = 0;
77
78  QuicStreamId id() const { return id_; }
79
80  QuicRstStreamErrorCode stream_error() const { return stream_error_; }
81  QuicErrorCode connection_error() const { return connection_error_; }
82
83  bool read_side_closed() const { return read_side_closed_; }
84  bool write_side_closed() const { return write_side_closed_; }
85
86  uint64 stream_bytes_read() const { return stream_bytes_read_; }
87  uint64 stream_bytes_written() const { return stream_bytes_written_; }
88
89  QuicVersion version() const;
90
91  void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; }
92  void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; }
93
94  // Adjust our flow control windows according to new offset in |frame|.
95  virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame);
96
97  // Updates our send window offset (if offset larger).
98  void UpdateFlowControlSendLimit(QuicStreamOffset offset);
99
100  // If our receive window has dropped below the threshold, then send a
101  // WINDOW_UPDATE frame. This is called whenever bytes are consumed from the
102  // sequencer's buffer.
103  void MaybeSendWindowUpdate();
104
105  int num_frames_received();
106
107  int num_duplicate_frames_received();
108
109  QuicFlowController* flow_controller() { return &flow_controller_; }
110
111 protected:
112  // Sends as much of 'data' to the connection as the connection will consume,
113  // and then buffers any remaining data in queued_data_.
114  void WriteOrBufferData(
115      base::StringPiece data,
116      bool fin,
117      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
118
119  // Sends as many bytes in the first |count| buffers of |iov| to the connection
120  // as the connection will consume.
121  // If |ack_notifier_delegate| is provided, then it will be notified once all
122  // the ACKs for this write have been received.
123  // Returns the number of bytes consumed by the connection.
124  QuicConsumedData WritevData(
125      const struct iovec* iov,
126      int iov_count,
127      bool fin,
128      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
129
130  // Close the read side of the socket.  Further frames will not be accepted.
131  virtual void CloseReadSide();
132
133  // Close the write side of the socket.  Further writes will fail.
134  void CloseWriteSide();
135
136  bool HasBufferedData();
137
138  bool fin_buffered() { return fin_buffered_; }
139
140  const QuicSession* session() const { return session_; }
141  QuicSession* session() { return session_; }
142
143  const QuicStreamSequencer* sequencer() const { return &sequencer_; }
144  QuicStreamSequencer* sequencer() { return &sequencer_; }
145
146  void DisableFlowControl() {
147    flow_controller_.Disable();
148  }
149
150 private:
151  friend class test::ReliableQuicStreamPeer;
152  friend class QuicStreamUtils;
153  class ProxyAckNotifierDelegate;
154
155  struct PendingData {
156    PendingData(string data_in,
157                scoped_refptr<ProxyAckNotifierDelegate> delegate_in);
158    ~PendingData();
159
160    string data;
161    // Delegate that should be notified when the pending data is acked.
162    // Can be nullptr.
163    scoped_refptr<ProxyAckNotifierDelegate> delegate;
164  };
165
166  // Calculates and returns available flow control send window.
167  uint64 SendWindowSize() const;
168
169  // Calculates and returns total number of bytes this stream has received.
170  uint64 TotalReceivedBytes() const;
171
172  std::list<PendingData> queued_data_;
173
174  QuicStreamSequencer sequencer_;
175  QuicStreamId id_;
176  QuicSession* session_;
177  // Bytes read and written refer to payload bytes only: they do not include
178  // framing, encryption overhead etc.
179  uint64 stream_bytes_read_;
180  uint64 stream_bytes_written_;
181
182  // Stream error code received from a RstStreamFrame or error code sent by the
183  // visitor or sequencer in the RstStreamFrame.
184  QuicRstStreamErrorCode stream_error_;
185  // Connection error code due to which the stream was closed. |stream_error_|
186  // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
187  // should check |connection_error_|.
188  QuicErrorCode connection_error_;
189
190  // True if the read side is closed and further frames should be rejected.
191  bool read_side_closed_;
192  // True if the write side is closed, and further writes should fail.
193  bool write_side_closed_;
194
195  bool fin_buffered_;
196  bool fin_sent_;
197
198  // In combination with fin_sent_, used to ensure that a FIN and/or a RST is
199  // always sent before stream termination.
200  bool rst_sent_;
201
202  // True if the session this stream is running under is a server session.
203  bool is_server_;
204
205  QuicFlowController flow_controller_;
206
207  DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream);
208};
209
210}  // namespace net
211
212#endif  // NET_QUIC_RELIABLE_QUIC_STREAM_H_
213