blob: b7ea49dac05ed5125502ee66fae8df8d2ba45f9c [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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#ifndef QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_
6#define QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_
7
8#include <cstddef>
9#include <map>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
QUICHE team5be974e2020-12-29 18:35:24 -050012#include "quic/core/quic_packets.h"
13#include "quic/core/quic_stream_sequencer_buffer.h"
14#include "quic/core/quic_types.h"
15#include "quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016
17namespace quic {
18
19namespace test {
20class QuicStreamSequencerPeer;
21} // namespace test
22
23// Buffers frames until we have something which can be passed
24// up to the next layer.
25class QUIC_EXPORT_PRIVATE QuicStreamSequencer {
26 public:
27 // Interface that thie Sequencer uses to communicate with the Stream.
dschinazif25169a2019-10-23 08:12:18 -070028 class QUIC_EXPORT_PRIVATE StreamInterface {
QUICHE teama6ef0a62019-03-07 20:34:33 -050029 public:
30 virtual ~StreamInterface() = default;
31
32 // Called when new data is available to be read from the sequencer.
33 virtual void OnDataAvailable() = 0;
34 // Called when the end of the stream has been read.
35 virtual void OnFinRead() = 0;
36 // Called when bytes have been consumed from the sequencer.
37 virtual void AddBytesConsumed(QuicByteCount bytes) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 // Called when an error has occurred which should result in the stream
39 // being reset.
40 virtual void Reset(QuicRstStreamErrorCode error) = 0;
41 // Called when an error has occurred which should result in the connection
42 // being closed.
renjietang87df0d02020-02-13 11:53:52 -080043 virtual void OnUnrecoverableError(QuicErrorCode error,
44 const std::string& details) = 0;
mattm55006b02021-01-14 15:09:54 -080045 // Called when an error has occurred which should result in the connection
46 // being closed, specifying the wire error code |ietf_error| explicitly.
47 virtual void OnUnrecoverableError(QuicErrorCode error,
48 QuicIetfTransportErrorCodes ietf_error,
49 const std::string& details) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 // Returns the stream id of this stream.
51 virtual QuicStreamId id() const = 0;
fayang528c36b2020-12-23 07:02:09 -080052
53 // Returns the QUIC version being used by this stream.
54 virtual ParsedQuicVersion version() const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050055 };
56
57 explicit QuicStreamSequencer(StreamInterface* quic_stream);
58 QuicStreamSequencer(const QuicStreamSequencer&) = delete;
59 QuicStreamSequencer(QuicStreamSequencer&&) = default;
60 QuicStreamSequencer& operator=(const QuicStreamSequencer&) = delete;
dschinazi969837b2020-04-20 12:59:40 -070061 QuicStreamSequencer& operator=(QuicStreamSequencer&&) = default;
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 virtual ~QuicStreamSequencer();
63
64 // If the frame is the next one we need in order to process in-order data,
65 // ProcessData will be immediately called on the stream until all buffered
66 // data is processed or the stream fails to consume data. Any unconsumed
67 // data will be buffered. If the frame is not the next in line, it will be
68 // buffered.
69 void OnStreamFrame(const QuicStreamFrame& frame);
70
71 // If the frame is the next one we need in order to process in-order data,
72 // ProcessData will be immediately called on the crypto stream until all
73 // buffered data is processed or the crypto stream fails to consume data. Any
74 // unconsumed data will be buffered. If the frame is not the next in line, it
75 // will be buffered.
76 void OnCryptoFrame(const QuicCryptoFrame& frame);
77
78 // Once data is buffered, it's up to the stream to read it when the stream
79 // can handle more data. The following three functions make that possible.
80
81 // Fills in up to iov_len iovecs with the next readable regions. Returns the
82 // number of iovs used. Non-destructive of the underlying data.
83 int GetReadableRegions(iovec* iov, size_t iov_len) const;
84
85 // Fills in one iovec with the next readable region. Returns false if there
86 // is no readable region available.
87 bool GetReadableRegion(iovec* iov) const;
88
bnc7b3e0a92019-06-24 16:06:45 -070089 // Fills in one iovec with the region starting at |offset| and returns true.
90 // Returns false if no readable region is available, either because data has
91 // not been received yet or has already been consumed.
92 bool PeekRegion(QuicStreamOffset offset, iovec* iov) const;
93
QUICHE teama6ef0a62019-03-07 20:34:33 -050094 // Copies the data into the iov_len buffers provided. Returns the number of
95 // bytes read. Any buffered data no longer in use will be released.
96 // TODO(rch): remove this method and instead implement it as a helper method
97 // based on GetReadableRegions and MarkConsumed.
renjietang8be8b742020-01-14 12:41:09 -080098 size_t Readv(const struct iovec* iov, size_t iov_len);
QUICHE teama6ef0a62019-03-07 20:34:33 -050099
100 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions|
101 // to do zero-copy reads.
102 void MarkConsumed(size_t num_bytes);
103
104 // Appends all of the readable data to |buffer| and marks all of the appended
105 // data as consumed.
vasilvvc48c8712019-03-11 13:38:16 -0700106 void Read(std::string* buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107
108 // Returns true if the sequncer has bytes available for reading.
109 bool HasBytesToRead() const;
110
111 // Number of bytes available to read.
112 size_t ReadableBytes() const;
113
114 // Returns true if the sequencer has delivered the fin.
115 bool IsClosed() const;
116
117 // Calls |OnDataAvailable| on |stream_| if there is buffered data that can
118 // be processed, and causes |OnDataAvailable| to be called as new data
119 // arrives.
120 void SetUnblocked();
121
122 // Blocks processing of frames until |SetUnblocked| is called.
123 void SetBlockedUntilFlush();
124
125 // Sets the sequencer to discard all incoming data itself and not call
126 // |stream_->OnDataAvailable()|. |stream_->OnFinRead()| will be called
127 // automatically when the FIN is consumed (which may be immediately).
128 void StopReading();
129
130 // Free the memory of underlying buffer.
131 void ReleaseBuffer();
132
133 // Free the memory of underlying buffer when no bytes remain in it.
134 void ReleaseBufferIfEmpty();
135
136 // Number of bytes in the buffer right now.
137 size_t NumBytesBuffered() const;
138
139 // Number of bytes has been consumed.
140 QuicStreamOffset NumBytesConsumed() const;
141
142 QuicStreamOffset close_offset() const { return close_offset_; }
143
144 int num_frames_received() const { return num_frames_received_; }
145
146 int num_duplicate_frames_received() const {
147 return num_duplicate_frames_received_;
148 }
149
150 bool ignore_read_data() const { return ignore_read_data_; }
151
152 void set_level_triggered(bool level_triggered) {
153 level_triggered_ = level_triggered;
154 }
155
156 bool level_triggered() const { return level_triggered_; }
157
158 void set_stream(StreamInterface* stream) { stream_ = stream; }
159
160 // Returns string describing internal state.
vasilvvc48c8712019-03-11 13:38:16 -0700161 const std::string DebugString() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500162
163 private:
164 friend class test::QuicStreamSequencerPeer;
165
166 // Deletes and records as consumed any buffered data that is now in-sequence.
167 // (To be called only after StopReading has been called.)
168 void FlushBufferedFrames();
169
170 // Wait until we've seen 'offset' bytes, and then terminate the stream.
renjietangc152cc52019-08-13 11:54:57 -0700171 // Returns true if |stream_| is still available to receive data, and false if
172 // |stream_| is reset.
173 bool CloseStreamAtOffset(QuicStreamOffset offset);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500174
175 // If we've received a FIN and have processed all remaining data, then inform
176 // the stream of FIN, and clear buffers.
bncf152d8c2019-08-06 06:03:08 -0700177 void MaybeCloseStream();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500178
179 // Shared implementation between OnStreamFrame and OnCryptoFrame.
180 void OnFrameData(QuicStreamOffset byte_offset,
181 size_t data_len,
182 const char* data_buffer);
183
184 // The stream which owns this sequencer.
185 StreamInterface* stream_;
186
187 // Stores received data in offset order.
188 QuicStreamSequencerBuffer buffered_frames_;
189
renjietang03987852019-10-22 12:06:37 -0700190 // The highest offset that is received so far.
191 QuicStreamOffset highest_offset_;
192
QUICHE teama6ef0a62019-03-07 20:34:33 -0500193 // The offset, if any, we got a stream termination for. When this many bytes
194 // have been processed, the sequencer will be closed.
195 QuicStreamOffset close_offset_;
196
197 // If true, the sequencer is blocked from passing data to the stream and will
198 // buffer all new incoming data until FlushBufferedFrames is called.
199 bool blocked_;
200
201 // Count of the number of frames received.
202 int num_frames_received_;
203
204 // Count of the number of duplicate frames received.
205 int num_duplicate_frames_received_;
206
207 // If true, all incoming data will be discarded.
208 bool ignore_read_data_;
209
210 // If false, only call OnDataAvailable() when it becomes newly unblocked.
211 // Otherwise, call OnDataAvailable() when number of readable bytes changes.
212 bool level_triggered_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500213};
214
215} // namespace quic
216
217#endif // QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_