| OLD | NEW |
| (Empty) |
| 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 // It does not contain the entire interface needed by an application to interact | |
| 8 // with a QUIC stream. Some parts of the interface must be obtained by | |
| 9 // accessing the owning session object. A subclass of ReliableQuicStream | |
| 10 // connects the object and the application that generates and consumes the data | |
| 11 // of the stream. | |
| 12 | |
| 13 // The ReliableQuicStream object has a dependent QuicStreamSequencer object, | |
| 14 // which is given the stream frames as they arrive, and provides stream data in | |
| 15 // order by invoking ProcessRawData(). | |
| 16 | |
| 17 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_ | |
| 18 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_ | |
| 19 | |
| 20 #include <stddef.h> | |
| 21 #include <stdint.h> | |
| 22 #include <sys/types.h> | |
| 23 | |
| 24 #include <list> | |
| 25 #include <string> | |
| 26 | |
| 27 #include "base/macros.h" | |
| 28 #include "base/memory/ref_counted.h" | |
| 29 #include "base/strings/string_piece.h" | |
| 30 #include "net/base/iovec.h" | |
| 31 #include "net/base/net_export.h" | |
| 32 #include "net/quic/core/quic_flow_controller.h" | |
| 33 #include "net/quic/core/quic_protocol.h" | |
| 34 #include "net/quic/core/quic_stream_sequencer.h" | |
| 35 #include "net/quic/core/quic_types.h" | |
| 36 | |
| 37 namespace net { | |
| 38 | |
| 39 namespace test { | |
| 40 class ReliableQuicStreamPeer; | |
| 41 } // namespace test | |
| 42 | |
| 43 class QuicSession; | |
| 44 | |
| 45 class NET_EXPORT_PRIVATE ReliableQuicStream { | |
| 46 public: | |
| 47 ReliableQuicStream(QuicStreamId id, QuicSession* session); | |
| 48 | |
| 49 virtual ~ReliableQuicStream(); | |
| 50 | |
| 51 // Not in use currently. | |
| 52 void SetFromConfig(); | |
| 53 | |
| 54 // Called by the session when a (potentially duplicate) stream frame has been | |
| 55 // received for this stream. | |
| 56 virtual void OnStreamFrame(const QuicStreamFrame& frame); | |
| 57 | |
| 58 // Called by the session when the connection becomes writeable to allow the | |
| 59 // stream to write any pending data. | |
| 60 virtual void OnCanWrite(); | |
| 61 | |
| 62 // Called by the session just before the object is destroyed. | |
| 63 // The object should not be accessed after OnClose is called. | |
| 64 // Sends a RST_STREAM with code QUIC_RST_ACKNOWLEDGEMENT if neither a FIN nor | |
| 65 // a RST_STREAM has been sent. | |
| 66 virtual void OnClose(); | |
| 67 | |
| 68 // Called by the session when the endpoint receives a RST_STREAM from the | |
| 69 // peer. | |
| 70 virtual void OnStreamReset(const QuicRstStreamFrame& frame); | |
| 71 | |
| 72 // Called by the session when the endpoint receives or sends a connection | |
| 73 // close, and should immediately close the stream. | |
| 74 virtual void OnConnectionClosed(QuicErrorCode error, | |
| 75 ConnectionCloseSource source); | |
| 76 | |
| 77 // Called by the stream subclass after it has consumed the final incoming | |
| 78 // data. | |
| 79 virtual void OnFinRead(); | |
| 80 | |
| 81 // Called when new data is available from the sequencer. Subclasses must | |
| 82 // actively retrieve the data using the sequencer's Readv() or | |
| 83 // GetReadableRegions() method. | |
| 84 virtual void OnDataAvailable() = 0; | |
| 85 | |
| 86 // Called by the subclass or the sequencer to reset the stream from this | |
| 87 // end. | |
| 88 virtual void Reset(QuicRstStreamErrorCode error); | |
| 89 | |
| 90 // Called by the subclass or the sequencer to close the entire connection from | |
| 91 // this end. | |
| 92 virtual void CloseConnectionWithDetails(QuicErrorCode error, | |
| 93 const std::string& details); | |
| 94 | |
| 95 QuicStreamId id() const { return id_; } | |
| 96 | |
| 97 QuicRstStreamErrorCode stream_error() const { return stream_error_; } | |
| 98 QuicErrorCode connection_error() const { return connection_error_; } | |
| 99 | |
| 100 bool reading_stopped() const { | |
| 101 return sequencer_.ignore_read_data() || read_side_closed_; | |
| 102 } | |
| 103 bool write_side_closed() const { return write_side_closed_; } | |
| 104 | |
| 105 bool rst_received() { return rst_received_; } | |
| 106 bool rst_sent() { return rst_sent_; } | |
| 107 bool fin_received() { return fin_received_; } | |
| 108 bool fin_sent() { return fin_sent_; } | |
| 109 | |
| 110 uint64_t queued_data_bytes() const { return queued_data_bytes_; } | |
| 111 | |
| 112 uint64_t stream_bytes_read() const { return stream_bytes_read_; } | |
| 113 uint64_t stream_bytes_written() const { return stream_bytes_written_; } | |
| 114 // For tests that override WritevData. | |
| 115 void set_stream_bytes_written(uint64_t bytes_written) { | |
| 116 stream_bytes_written_ = bytes_written; | |
| 117 } | |
| 118 | |
| 119 size_t busy_counter() const { return busy_counter_; } | |
| 120 void set_busy_counter(size_t busy_counter) { busy_counter_ = busy_counter; } | |
| 121 | |
| 122 void set_fin_sent(bool fin_sent) { fin_sent_ = fin_sent; } | |
| 123 void set_fin_received(bool fin_received) { fin_received_ = fin_received; } | |
| 124 void set_rst_sent(bool rst_sent) { rst_sent_ = rst_sent; } | |
| 125 | |
| 126 void set_rst_received(bool rst_received) { rst_received_ = rst_received; } | |
| 127 void set_stream_error(QuicRstStreamErrorCode error) { stream_error_ = error; } | |
| 128 | |
| 129 // Adjust the flow control window according to new offset in |frame|. | |
| 130 virtual void OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame); | |
| 131 | |
| 132 // Used in Chrome. | |
| 133 int num_frames_received() const; | |
| 134 int num_duplicate_frames_received() const; | |
| 135 | |
| 136 QuicFlowController* flow_controller() { return &flow_controller_; } | |
| 137 | |
| 138 // Called when endpoint receives a frame which could increase the highest | |
| 139 // offset. | |
| 140 // Returns true if the highest offset did increase. | |
| 141 bool MaybeIncreaseHighestReceivedOffset(QuicStreamOffset new_offset); | |
| 142 // Called when bytes are sent to the peer. | |
| 143 void AddBytesSent(QuicByteCount bytes); | |
| 144 // Called by the stream sequencer as bytes are consumed from the buffer. | |
| 145 // If the receive window has dropped below the threshold, then send a | |
| 146 // WINDOW_UPDATE frame. | |
| 147 void AddBytesConsumed(QuicByteCount bytes); | |
| 148 | |
| 149 // Updates the flow controller's send window offset and calls OnCanWrite if | |
| 150 // it was blocked before. | |
| 151 void UpdateSendWindowOffset(QuicStreamOffset new_offset); | |
| 152 | |
| 153 // Returns true if the stream has received either a RST_STREAM or a FIN - | |
| 154 // either of which gives a definitive number of bytes which the peer has | |
| 155 // sent. If this is not true on deletion of the stream object, the session | |
| 156 // must keep track of the stream's byte offset until a definitive final value | |
| 157 // arrives. | |
| 158 bool HasFinalReceivedByteOffset() const { | |
| 159 return fin_received_ || rst_received_; | |
| 160 } | |
| 161 | |
| 162 // Returns true if the stream has queued data waiting to write. | |
| 163 bool HasBufferedData() const; | |
| 164 | |
| 165 // Returns the version of QUIC being used for this stream. | |
| 166 QuicVersion version() const; | |
| 167 | |
| 168 bool fin_received() const { return fin_received_; } | |
| 169 | |
| 170 // Sets the sequencer to consume all incoming data itself and not call | |
| 171 // OnDataAvailable(). | |
| 172 // When the FIN is received, the stream will be notified automatically (via | |
| 173 // OnFinRead()) (which may happen during the call of StopReading()). | |
| 174 // TODO(dworley): There should be machinery to send a RST_STREAM/NO_ERROR and | |
| 175 // stop sending stream-level flow-control updates when this end sends FIN. | |
| 176 virtual void StopReading(); | |
| 177 | |
| 178 // Get peer IP of the lastest packet which connection is dealing/delt with. | |
| 179 virtual const IPEndPoint& PeerAddressOfLatestPacket() const; | |
| 180 | |
| 181 protected: | |
| 182 // Sends as much of 'data' to the connection as the connection will consume, | |
| 183 // and then buffers any remaining data in queued_data_. | |
| 184 // If fin is true: if it is immediately passed on to the session, | |
| 185 // write_side_closed() becomes true, otherwise fin_buffered_ becomes true. | |
| 186 void WriteOrBufferData(base::StringPiece data, | |
| 187 bool fin, | |
| 188 QuicAckListenerInterface* ack_listener); | |
| 189 | |
| 190 // Sends as many bytes in the first |count| buffers of |iov| to the connection | |
| 191 // as the connection will consume. | |
| 192 // If |ack_listener| is provided, then it will be notified once all | |
| 193 // the ACKs for this write have been received. | |
| 194 // Returns the number of bytes consumed by the connection. | |
| 195 QuicConsumedData WritevData(const struct iovec* iov, | |
| 196 int iov_count, | |
| 197 bool fin, | |
| 198 QuicAckListenerInterface* ack_listener); | |
| 199 | |
| 200 // Allows override of the session level writev, for the force HOL | |
| 201 // blocking experiment. | |
| 202 virtual QuicConsumedData WritevDataInner( | |
| 203 QuicIOVector iov, | |
| 204 QuicStreamOffset offset, | |
| 205 bool fin, | |
| 206 QuicAckListenerInterface* ack_notifier_delegate); | |
| 207 | |
| 208 // Close the write side of the socket. Further writes will fail. | |
| 209 // Can be called by the subclass or internally. | |
| 210 // Does not send a FIN. May cause the stream to be closed. | |
| 211 virtual void CloseWriteSide(); | |
| 212 | |
| 213 bool fin_buffered() const { return fin_buffered_; } | |
| 214 | |
| 215 const QuicSession* session() const { return session_; } | |
| 216 QuicSession* session() { return session_; } | |
| 217 | |
| 218 const QuicStreamSequencer* sequencer() const { return &sequencer_; } | |
| 219 QuicStreamSequencer* sequencer() { return &sequencer_; } | |
| 220 | |
| 221 void DisableConnectionFlowControlForThisStream() { | |
| 222 stream_contributes_to_connection_flow_control_ = false; | |
| 223 } | |
| 224 | |
| 225 private: | |
| 226 friend class test::ReliableQuicStreamPeer; | |
| 227 friend class QuicStreamUtils; | |
| 228 | |
| 229 // Close the read side of the socket. May cause the stream to be closed. | |
| 230 // Subclasses and consumers should use StopReading to terminate reading early. | |
| 231 void CloseReadSide(); | |
| 232 | |
| 233 // Subclasses and consumers should use reading_stopped. | |
| 234 bool read_side_closed() const { return read_side_closed_; } | |
| 235 | |
| 236 struct PendingData { | |
| 237 PendingData(std::string data_in, QuicAckListenerInterface* ack_listener_in); | |
| 238 ~PendingData(); | |
| 239 | |
| 240 // Pending data to be written. | |
| 241 std::string data; | |
| 242 // Index of the first byte in data still to be written. | |
| 243 size_t offset; | |
| 244 // AckListener that should be notified when the pending data is acked. | |
| 245 // Can be nullptr. | |
| 246 scoped_refptr<QuicAckListenerInterface> ack_listener; | |
| 247 }; | |
| 248 | |
| 249 // Calls MaybeSendBlocked on the stream's flow controller and the connection | |
| 250 // level flow controller. If the stream is flow control blocked by the | |
| 251 // connection-level flow controller but not by the stream-level flow | |
| 252 // controller, marks this stream as connection-level write blocked. | |
| 253 void MaybeSendBlocked(); | |
| 254 | |
| 255 std::list<PendingData> queued_data_; | |
| 256 // How many bytes are queued? | |
| 257 uint64_t queued_data_bytes_; | |
| 258 | |
| 259 QuicStreamSequencer sequencer_; | |
| 260 QuicStreamId id_; | |
| 261 // Pointer to the owning QuicSession object. | |
| 262 QuicSession* session_; | |
| 263 // Bytes read and written refer to payload bytes only: they do not include | |
| 264 // framing, encryption overhead etc. | |
| 265 uint64_t stream_bytes_read_; | |
| 266 uint64_t stream_bytes_written_; | |
| 267 | |
| 268 // Stream error code received from a RstStreamFrame or error code sent by the | |
| 269 // visitor or sequencer in the RstStreamFrame. | |
| 270 QuicRstStreamErrorCode stream_error_; | |
| 271 // Connection error code due to which the stream was closed. |stream_error_| | |
| 272 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers | |
| 273 // should check |connection_error_|. | |
| 274 QuicErrorCode connection_error_; | |
| 275 | |
| 276 // True if the read side is closed and further frames should be rejected. | |
| 277 bool read_side_closed_; | |
| 278 // True if the write side is closed, and further writes should fail. | |
| 279 bool write_side_closed_; | |
| 280 | |
| 281 // True if the subclass has written a FIN with WriteOrBufferData, but it was | |
| 282 // buffered in queued_data_ rather than being sent to the session. | |
| 283 bool fin_buffered_; | |
| 284 // True if a FIN has been sent to the session. | |
| 285 bool fin_sent_; | |
| 286 | |
| 287 // True if this stream has received (and the sequencer has accepted) a | |
| 288 // StreamFrame with the FIN set. | |
| 289 bool fin_received_; | |
| 290 | |
| 291 // True if an RST_STREAM has been sent to the session. | |
| 292 // In combination with fin_sent_, used to ensure that a FIN and/or a | |
| 293 // RST_STREAM is always sent to terminate the stream. | |
| 294 bool rst_sent_; | |
| 295 | |
| 296 // True if this stream has received a RST_STREAM frame. | |
| 297 bool rst_received_; | |
| 298 | |
| 299 // Tracks if the session this stream is running under was created by a | |
| 300 // server or a client. | |
| 301 Perspective perspective_; | |
| 302 | |
| 303 QuicFlowController flow_controller_; | |
| 304 | |
| 305 // The connection level flow controller. Not owned. | |
| 306 QuicFlowController* connection_flow_controller_; | |
| 307 | |
| 308 // Special streams, such as the crypto and headers streams, do not respect | |
| 309 // connection level flow control limits (but are stream level flow control | |
| 310 // limited). | |
| 311 bool stream_contributes_to_connection_flow_control_; | |
| 312 | |
| 313 // For debugging only, used for busy loop check. | |
| 314 size_t busy_counter_; | |
| 315 | |
| 316 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream); | |
| 317 }; | |
| 318 | |
| 319 } // namespace net | |
| 320 | |
| 321 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ | |
| OLD | NEW |