reliable_quic_stream.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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(); 101 102 int num_duplicate_frames_received(); 103 104 QuicFlowController* flow_controller() { return &flow_controller_; } 105 106 protected: 107 // Sends as much of 'data' to the connection as the connection will consume, 108 // and then buffers any remaining data in queued_data_. 109 void WriteOrBufferData( 110 base::StringPiece data, 111 bool fin, 112 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); 113 114 // Sends as many bytes in the first |count| buffers of |iov| to the connection 115 // as the connection will consume. 116 // If |ack_notifier_delegate| is provided, then it will be notified once all 117 // the ACKs for this write have been received. 118 // Returns the number of bytes consumed by the connection. 119 QuicConsumedData WritevData( 120 const struct iovec* iov, 121 int iov_count, 122 bool fin, 123 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); 124 125 // Close the read side of the socket. Further frames will not be accepted. 126 virtual void CloseReadSide(); 127 128 // Close the write side of the socket. Further writes will fail. 129 void CloseWriteSide(); 130 131 bool HasBufferedData(); 132 133 bool fin_buffered() { return fin_buffered_; } 134 135 const QuicSession* session() const { return session_; } 136 QuicSession* session() { return session_; } 137 138 const QuicStreamSequencer* sequencer() const { return &sequencer_; } 139 QuicStreamSequencer* sequencer() { return &sequencer_; } 140 141 void DisableFlowControl() { 142 flow_controller_.Disable(); 143 } 144 145 private: 146 friend class test::ReliableQuicStreamPeer; 147 friend class QuicStreamUtils; 148 class ProxyAckNotifierDelegate; 149 150 struct PendingData { 151 PendingData(string data_in, 152 scoped_refptr<ProxyAckNotifierDelegate> delegate_in); 153 ~PendingData(); 154 155 string data; 156 // Delegate that should be notified when the pending data is acked. 157 // Can be nullptr. 158 scoped_refptr<ProxyAckNotifierDelegate> delegate; 159 }; 160 161 // Calculates and returns available flow control send window. 162 uint64 SendWindowSize() const; 163 164 // Calculates and returns total number of bytes this stream has received. 165 uint64 TotalReceivedBytes() const; 166 167 std::list<PendingData> queued_data_; 168 169 QuicStreamSequencer sequencer_; 170 QuicStreamId id_; 171 QuicSession* session_; 172 // Bytes read and written refer to payload bytes only: they do not include 173 // framing, encryption overhead etc. 174 uint64 stream_bytes_read_; 175 uint64 stream_bytes_written_; 176 177 // Stream error code received from a RstStreamFrame or error code sent by the 178 // visitor or sequencer in the RstStreamFrame. 179 QuicRstStreamErrorCode stream_error_; 180 // Connection error code due to which the stream was closed. |stream_error_| 181 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers 182 // should check |connection_error_|. 183 QuicErrorCode connection_error_; 184 185 // True if the read side is closed and further frames should be rejected. 186 bool read_side_closed_; 187 // True if the write side is closed, and further writes should fail. 188 bool write_side_closed_; 189 190 bool fin_buffered_; 191 bool fin_sent_; 192 193 // In combination with fin_sent_, used to ensure that a FIN and/or a RST is 194 // always sent before stream termination. 195 bool rst_sent_; 196 197 // True if the session this stream is running under is a server session. 198 bool is_server_; 199 200 QuicFlowController flow_controller_; 201 202 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream); 203}; 204 205} // namespace net 206 207#endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ 208