reliable_quic_stream.h revision 010d83a9304c5a91596085d917d248abff47903a
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 // Called when a (potentially duplicate) stream frame has been received 40 // for this stream. Returns false if this frame can not be accepted 41 // because there is too much data already buffered. 42 virtual bool 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 76 QuicStreamId id() const { return id_; } 77 78 QuicRstStreamErrorCode stream_error() const { return stream_error_; } 79 QuicErrorCode connection_error() const { return connection_error_; } 80 81 bool read_side_closed() const { return read_side_closed_; } 82 bool write_side_closed() const { return write_side_closed_; } 83 84 uint64 stream_bytes_read() const { return stream_bytes_read_; } 85 uint64 stream_bytes_written() const { return stream_bytes_written_; } 86 87 QuicVersion version() const; 88 89 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; } 90 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; } 91 92 // Adjust our flow control windows according to new offset in |frame|. 93 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame); 94 95 // If our receive window has dropped below the threshold, then send a 96 // WINDOW_UPDATE frame. This is called whenever bytes are consumed from the 97 // sequencer's buffer. 98 void MaybeSendWindowUpdate(); 99 100 int num_frames_received() const; 101 102 int num_duplicate_frames_received() const; 103 104 QuicFlowController* flow_controller() { return &flow_controller_; } 105 106 // Called by the stream sequeuncer as bytes are added to the buffer. 107 void AddBytesBuffered(uint64 bytes); 108 // Called by the stream sequeuncer as bytes are removed from the buffer. 109 void RemoveBytesBuffered(uint64 bytes); 110 // Called when bytese are sent to the peer. 111 void AddBytesSent(uint64 bytes); 112 // Called by the stream sequeuncer as bytes are consumed from the buffer. 113 void AddBytesConsumed(uint64 bytes); 114 115 // Returns true if the stream is flow control blocked, by the stream flow 116 // control window or the connection flow control window. 117 bool IsFlowControlBlocked(); 118 119 protected: 120 // Sends as much of 'data' to the connection as the connection will consume, 121 // and then buffers any remaining data in queued_data_. 122 void WriteOrBufferData( 123 base::StringPiece data, 124 bool fin, 125 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); 126 127 // Sends as many bytes in the first |count| buffers of |iov| to the connection 128 // as the connection will consume. 129 // If |ack_notifier_delegate| is provided, then it will be notified once all 130 // the ACKs for this write have been received. 131 // Returns the number of bytes consumed by the connection. 132 QuicConsumedData WritevData( 133 const struct iovec* iov, 134 int iov_count, 135 bool fin, 136 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); 137 138 // Close the read side of the socket. Further frames will not be accepted. 139 virtual void CloseReadSide(); 140 141 // Close the write side of the socket. Further writes will fail. 142 void CloseWriteSide(); 143 144 bool HasBufferedData() const; 145 146 bool fin_buffered() const { return fin_buffered_; } 147 148 const QuicSession* session() const { return session_; } 149 QuicSession* session() { return session_; } 150 151 const QuicStreamSequencer* sequencer() const { return &sequencer_; } 152 QuicStreamSequencer* sequencer() { return &sequencer_; } 153 154 void DisableFlowControl() { 155 flow_controller_.Disable(); 156 } 157 158 private: 159 friend class test::ReliableQuicStreamPeer; 160 friend class QuicStreamUtils; 161 class ProxyAckNotifierDelegate; 162 163 struct PendingData { 164 PendingData(string data_in, 165 scoped_refptr<ProxyAckNotifierDelegate> delegate_in); 166 ~PendingData(); 167 168 string data; 169 // Delegate that should be notified when the pending data is acked. 170 // Can be nullptr. 171 scoped_refptr<ProxyAckNotifierDelegate> delegate; 172 }; 173 174 // Calculates and returns available flow control send window. 175 uint64 SendWindowSize() const; 176 177 // Calculates and returns total number of bytes this stream has received. 178 uint64 TotalReceivedBytes() const; 179 180 // Calls MaybeSendBlocked on our flow controller, and connection level flow 181 // controller. If we are flow control blocked, marks this stream as write 182 // blocked. 183 void MaybeSendBlocked(); 184 185 std::list<PendingData> queued_data_; 186 187 QuicStreamSequencer sequencer_; 188 QuicStreamId id_; 189 QuicSession* session_; 190 // Bytes read and written refer to payload bytes only: they do not include 191 // framing, encryption overhead etc. 192 uint64 stream_bytes_read_; 193 uint64 stream_bytes_written_; 194 195 // Stream error code received from a RstStreamFrame or error code sent by the 196 // visitor or sequencer in the RstStreamFrame. 197 QuicRstStreamErrorCode stream_error_; 198 // Connection error code due to which the stream was closed. |stream_error_| 199 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers 200 // should check |connection_error_|. 201 QuicErrorCode connection_error_; 202 203 // True if the read side is closed and further frames should be rejected. 204 bool read_side_closed_; 205 // True if the write side is closed, and further writes should fail. 206 bool write_side_closed_; 207 208 bool fin_buffered_; 209 bool fin_sent_; 210 211 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is 212 // always sent before stream termination. 213 bool rst_sent_; 214 215 // True if the session this stream is running under is a server session. 216 bool is_server_; 217 218 QuicFlowController flow_controller_; 219 220 // The connection level flow controller. Not owned. 221 QuicFlowController* connection_flow_controller_; 222 223 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream); 224}; 225 226} // namespace net 227 228#endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ 229