blob: a74526b949e209df1b4f253d69409b220a696d7d [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 teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/quic_packets.h"
13#include "net/third_party/quiche/src/quic/core/quic_stream_sequencer_buffer.h"
renjietang03987852019-10-22 12:06:37 -070014#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015#include "net/third_party/quiche/src/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;
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 // Returns the stream id of this stream.
46 virtual QuicStreamId id() const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050047 };
48
49 explicit QuicStreamSequencer(StreamInterface* quic_stream);
50 QuicStreamSequencer(const QuicStreamSequencer&) = delete;
51 QuicStreamSequencer(QuicStreamSequencer&&) = default;
52 QuicStreamSequencer& operator=(const QuicStreamSequencer&) = delete;
dschinazi969837b2020-04-20 12:59:40 -070053 QuicStreamSequencer& operator=(QuicStreamSequencer&&) = default;
QUICHE teama6ef0a62019-03-07 20:34:33 -050054 virtual ~QuicStreamSequencer();
55
56 // If the frame is the next one we need in order to process in-order data,
57 // ProcessData will be immediately called on the stream until all buffered
58 // data is processed or the stream fails to consume data. Any unconsumed
59 // data will be buffered. If the frame is not the next in line, it will be
60 // buffered.
61 void OnStreamFrame(const QuicStreamFrame& frame);
62
63 // If the frame is the next one we need in order to process in-order data,
64 // ProcessData will be immediately called on the crypto stream until all
65 // buffered data is processed or the crypto stream fails to consume data. Any
66 // unconsumed data will be buffered. If the frame is not the next in line, it
67 // will be buffered.
68 void OnCryptoFrame(const QuicCryptoFrame& frame);
69
70 // Once data is buffered, it's up to the stream to read it when the stream
71 // can handle more data. The following three functions make that possible.
72
73 // Fills in up to iov_len iovecs with the next readable regions. Returns the
74 // number of iovs used. Non-destructive of the underlying data.
75 int GetReadableRegions(iovec* iov, size_t iov_len) const;
76
77 // Fills in one iovec with the next readable region. Returns false if there
78 // is no readable region available.
79 bool GetReadableRegion(iovec* iov) const;
80
bnc7b3e0a92019-06-24 16:06:45 -070081 // Fills in one iovec with the region starting at |offset| and returns true.
82 // Returns false if no readable region is available, either because data has
83 // not been received yet or has already been consumed.
84 bool PeekRegion(QuicStreamOffset offset, iovec* iov) const;
85
QUICHE teama6ef0a62019-03-07 20:34:33 -050086 // Copies the data into the iov_len buffers provided. Returns the number of
87 // bytes read. Any buffered data no longer in use will be released.
88 // TODO(rch): remove this method and instead implement it as a helper method
89 // based on GetReadableRegions and MarkConsumed.
renjietang8be8b742020-01-14 12:41:09 -080090 size_t Readv(const struct iovec* iov, size_t iov_len);
QUICHE teama6ef0a62019-03-07 20:34:33 -050091
92 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions|
93 // to do zero-copy reads.
94 void MarkConsumed(size_t num_bytes);
95
96 // Appends all of the readable data to |buffer| and marks all of the appended
97 // data as consumed.
vasilvvc48c8712019-03-11 13:38:16 -070098 void Read(std::string* buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -050099
100 // Returns true if the sequncer has bytes available for reading.
101 bool HasBytesToRead() const;
102
103 // Number of bytes available to read.
104 size_t ReadableBytes() const;
105
106 // Returns true if the sequencer has delivered the fin.
107 bool IsClosed() const;
108
109 // Calls |OnDataAvailable| on |stream_| if there is buffered data that can
110 // be processed, and causes |OnDataAvailable| to be called as new data
111 // arrives.
112 void SetUnblocked();
113
114 // Blocks processing of frames until |SetUnblocked| is called.
115 void SetBlockedUntilFlush();
116
117 // Sets the sequencer to discard all incoming data itself and not call
118 // |stream_->OnDataAvailable()|. |stream_->OnFinRead()| will be called
119 // automatically when the FIN is consumed (which may be immediately).
120 void StopReading();
121
122 // Free the memory of underlying buffer.
123 void ReleaseBuffer();
124
125 // Free the memory of underlying buffer when no bytes remain in it.
126 void ReleaseBufferIfEmpty();
127
128 // Number of bytes in the buffer right now.
129 size_t NumBytesBuffered() const;
130
131 // Number of bytes has been consumed.
132 QuicStreamOffset NumBytesConsumed() const;
133
134 QuicStreamOffset close_offset() const { return close_offset_; }
135
136 int num_frames_received() const { return num_frames_received_; }
137
138 int num_duplicate_frames_received() const {
139 return num_duplicate_frames_received_;
140 }
141
142 bool ignore_read_data() const { return ignore_read_data_; }
143
144 void set_level_triggered(bool level_triggered) {
145 level_triggered_ = level_triggered;
146 }
147
148 bool level_triggered() const { return level_triggered_; }
149
150 void set_stream(StreamInterface* stream) { stream_ = stream; }
151
152 // Returns string describing internal state.
vasilvvc48c8712019-03-11 13:38:16 -0700153 const std::string DebugString() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154
155 private:
156 friend class test::QuicStreamSequencerPeer;
157
158 // Deletes and records as consumed any buffered data that is now in-sequence.
159 // (To be called only after StopReading has been called.)
160 void FlushBufferedFrames();
161
162 // Wait until we've seen 'offset' bytes, and then terminate the stream.
renjietangc152cc52019-08-13 11:54:57 -0700163 // Returns true if |stream_| is still available to receive data, and false if
164 // |stream_| is reset.
165 bool CloseStreamAtOffset(QuicStreamOffset offset);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166
167 // If we've received a FIN and have processed all remaining data, then inform
168 // the stream of FIN, and clear buffers.
bncf152d8c2019-08-06 06:03:08 -0700169 void MaybeCloseStream();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500170
171 // Shared implementation between OnStreamFrame and OnCryptoFrame.
172 void OnFrameData(QuicStreamOffset byte_offset,
173 size_t data_len,
174 const char* data_buffer);
175
176 // The stream which owns this sequencer.
177 StreamInterface* stream_;
178
179 // Stores received data in offset order.
180 QuicStreamSequencerBuffer buffered_frames_;
181
renjietang03987852019-10-22 12:06:37 -0700182 // The highest offset that is received so far.
183 QuicStreamOffset highest_offset_;
184
QUICHE teama6ef0a62019-03-07 20:34:33 -0500185 // The offset, if any, we got a stream termination for. When this many bytes
186 // have been processed, the sequencer will be closed.
187 QuicStreamOffset close_offset_;
188
189 // If true, the sequencer is blocked from passing data to the stream and will
190 // buffer all new incoming data until FlushBufferedFrames is called.
191 bool blocked_;
192
193 // Count of the number of frames received.
194 int num_frames_received_;
195
196 // Count of the number of duplicate frames received.
197 int num_duplicate_frames_received_;
198
199 // If true, all incoming data will be discarded.
200 bool ignore_read_data_;
201
202 // If false, only call OnDataAvailable() when it becomes newly unblocked.
203 // Otherwise, call OnDataAvailable() when number of readable bytes changes.
204 bool level_triggered_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500205};
206
207} // namespace quic
208
209#endif // QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_