QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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 | #ifndef QUICHE_QUIC_QUARTC_QUARTC_STREAM_H_ |
| 6 | #define QUICHE_QUIC_QUARTC_QUARTC_STREAM_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <limits> |
| 10 | |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_ack_listener_interface.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_session.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_stream.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_span.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_reference_counted.h" |
| 17 | #include "net/quic/platform/impl/quic_export_impl.h" |
| 18 | #include "net/third_party/quiche/src/quic/quartc/quartc_interval_counter.h" |
| 19 | |
| 20 | namespace quic { |
| 21 | |
| 22 | // Sends and receives data with a particular QUIC stream ID, reliably and |
| 23 | // in-order. To send/receive data out of order, use separate streams. To |
| 24 | // send/receive unreliably, close a stream after reliability is no longer |
| 25 | // needed. |
| 26 | class QuartcStream : public QuicStream { |
| 27 | public: |
| 28 | QuartcStream(QuicStreamId id, QuicSession* session); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | |
| 30 | ~QuartcStream() override; |
| 31 | |
| 32 | // QuicStream overrides. |
| 33 | void OnDataAvailable() override; |
| 34 | |
| 35 | void OnClose() override; |
| 36 | |
| 37 | void OnStreamDataConsumed(size_t bytes_consumed) override; |
| 38 | |
| 39 | void OnDataBuffered( |
| 40 | QuicStreamOffset offset, |
| 41 | QuicByteCount data_length, |
| 42 | const QuicReferenceCountedPointer<QuicAckListenerInterface>& ack_listener) |
| 43 | override; |
| 44 | |
| 45 | bool OnStreamFrameAcked(QuicStreamOffset offset, |
| 46 | QuicByteCount data_length, |
| 47 | bool fin_acked, |
| 48 | QuicTime::Delta ack_delay_time, |
| 49 | QuicByteCount* newly_acked_length) override; |
| 50 | |
| 51 | void OnStreamFrameRetransmitted(QuicStreamOffset offset, |
| 52 | QuicByteCount data_length, |
| 53 | bool fin_retransmitted) override; |
| 54 | |
| 55 | void OnStreamFrameLost(QuicStreamOffset offset, |
| 56 | QuicByteCount data_length, |
| 57 | bool fin_lost) override; |
| 58 | |
| 59 | void OnCanWrite() override; |
| 60 | |
| 61 | // QuartcStream interface methods. |
| 62 | |
| 63 | // Whether the stream should be cancelled instead of retransmitted on loss. |
| 64 | // If set to true, the stream will reset itself instead of retransmitting lost |
| 65 | // stream frames. Defaults to false. Setting it to true is equivalent to |
| 66 | // setting |max_retransmission_count| to zero. |
| 67 | bool cancel_on_loss(); |
| 68 | void set_cancel_on_loss(bool cancel_on_loss); |
| 69 | |
| 70 | // Maximum number of times this stream's data may be retransmitted. Each byte |
| 71 | // of stream data may be retransmitted this many times. If any byte (or range |
| 72 | // of bytes) is lost and would be retransmitted more than this number of |
| 73 | // times, the stream resets itself instead of retransmitting the data again. |
| 74 | // Setting this value to zero disables retransmissions. |
| 75 | // |
| 76 | // Note that this limit applies only to stream data, not to the FIN bit. If |
| 77 | // only the FIN bit needs to be retransmitted, there is no benefit to |
| 78 | // cancelling the stream and sending a reset frame instead. |
| 79 | int max_retransmission_count() const; |
| 80 | void set_max_retransmission_count(int max_retransmission_count); |
| 81 | |
| 82 | QuicByteCount BytesPendingRetransmission(); |
| 83 | |
| 84 | // Returns the current read offset for this stream. During a call to |
| 85 | // Delegate::OnReceived, this value is the offset of the first byte read. |
| 86 | QuicStreamOffset ReadOffset(); |
| 87 | |
| 88 | // Marks this stream as finished writing. Asynchronously sends a FIN and |
| 89 | // closes the write-side. It is not necessary to call FinishWriting() if the |
| 90 | // last call to Write() sends a FIN. |
| 91 | void FinishWriting(); |
| 92 | |
| 93 | // Implemented by the user of the QuartcStream to receive incoming |
| 94 | // data and be notified of state changes. |
| 95 | class Delegate { |
| 96 | public: |
| 97 | virtual ~Delegate() {} |
| 98 | |
| 99 | // Called when the stream receives data. |iov| is a pointer to the first of |
| 100 | // |iov_length| readable regions. |iov| points to readable data within |
| 101 | // |stream|'s sequencer buffer. QUIC may modify or delete this data after |
| 102 | // the application consumes it. |fin| indicates the end of stream data. |
| 103 | // Returns the number of bytes consumed. May return 0 if the delegate is |
| 104 | // unable to consume any bytes at this time. |
| 105 | virtual size_t OnReceived(QuartcStream* stream, |
| 106 | iovec* iov, |
| 107 | size_t iov_length, |
| 108 | bool fin) = 0; |
| 109 | |
| 110 | // Called when the stream is closed, either locally or by the remote |
| 111 | // endpoint. Streams close when (a) fin bits are both sent and received, |
| 112 | // (b) Close() is called, or (c) the stream is reset. |
| 113 | // TODO(zhihuang) Creates a map from the integer error_code to WebRTC native |
| 114 | // error code. |
| 115 | virtual void OnClose(QuartcStream* stream) = 0; |
| 116 | |
| 117 | // Called when the contents of the stream's buffer changes. |
| 118 | virtual void OnBufferChanged(QuartcStream* stream) = 0; |
| 119 | }; |
| 120 | |
| 121 | // The |delegate| is not owned by QuartcStream. |
| 122 | void SetDelegate(Delegate* delegate); |
| 123 | |
| 124 | private: |
| 125 | Delegate* delegate_ = nullptr; |
| 126 | |
| 127 | // Maximum number of times this stream's data may be retransmitted. |
| 128 | int max_retransmission_count_ = std::numeric_limits<int>::max(); |
| 129 | |
| 130 | // Counter which tracks the number of times each frame has been lost |
| 131 | // (accounting for the possibility of overlapping frames). |
| 132 | // |
| 133 | // If the maximum count of any lost frame exceeds |max_retransmission_count_|, |
| 134 | // the stream will cancel itself on the next attempt to retransmit data (the |
| 135 | // next call to |OnCanWrite|). |
| 136 | QuartcIntervalCounter<QuicStreamOffset> lost_frame_counter_; |
| 137 | }; |
| 138 | |
| 139 | } // namespace quic |
| 140 | |
| 141 | #endif // QUICHE_QUIC_QUARTC_QUARTC_STREAM_H_ |