blob: 120b997c8f1af62aabdf87296ff6b63dffe667e0 [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>
10
11#include "base/macros.h"
12#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"
14#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_string.h"
16
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.
28 class StreamInterface {
29 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;
38 // TODO(rch): Clean up this interface via OnUnrecoverableError and
39 // remove PeerAddressOfLatestPacket().
40 // Called when an error has occurred which should result in the stream
41 // being reset.
42 virtual void Reset(QuicRstStreamErrorCode error) = 0;
43 // Called when an error has occurred which should result in the connection
44 // being closed.
45 virtual void CloseConnectionWithDetails(QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -070046 const std::string& details) = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050047
48 // Returns the stream id of this stream.
49 virtual QuicStreamId id() const = 0;
50 // Returns the peer address of the last packet received for this stream.
51 virtual const QuicSocketAddress& PeerAddressOfLatestPacket() const = 0;
52 };
53
54 explicit QuicStreamSequencer(StreamInterface* quic_stream);
55 QuicStreamSequencer(const QuicStreamSequencer&) = delete;
56 QuicStreamSequencer(QuicStreamSequencer&&) = default;
57 QuicStreamSequencer& operator=(const QuicStreamSequencer&) = delete;
58 virtual ~QuicStreamSequencer();
59
60 // If the frame is the next one we need in order to process in-order data,
61 // ProcessData will be immediately called on the stream until all buffered
62 // data is processed or the stream fails to consume data. Any unconsumed
63 // data will be buffered. If the frame is not the next in line, it will be
64 // buffered.
65 void OnStreamFrame(const QuicStreamFrame& frame);
66
67 // If the frame is the next one we need in order to process in-order data,
68 // ProcessData will be immediately called on the crypto stream until all
69 // buffered data is processed or the crypto stream fails to consume data. Any
70 // unconsumed data will be buffered. If the frame is not the next in line, it
71 // will be buffered.
72 void OnCryptoFrame(const QuicCryptoFrame& frame);
73
74 // Once data is buffered, it's up to the stream to read it when the stream
75 // can handle more data. The following three functions make that possible.
76
77 // Fills in up to iov_len iovecs with the next readable regions. Returns the
78 // number of iovs used. Non-destructive of the underlying data.
79 int GetReadableRegions(iovec* iov, size_t iov_len) const;
80
81 // Fills in one iovec with the next readable region. Returns false if there
82 // is no readable region available.
83 bool GetReadableRegion(iovec* iov) const;
84
85 // Fill in one iovec with the next unread region for the quic spdy stream.
86 // Returns false if no readable region is available.
87 bool PrefetchNextRegion(iovec* iov);
88
89 // Copies the data into the iov_len buffers provided. Returns the number of
90 // bytes read. Any buffered data no longer in use will be released.
91 // TODO(rch): remove this method and instead implement it as a helper method
92 // based on GetReadableRegions and MarkConsumed.
93 int Readv(const struct iovec* iov, size_t iov_len);
94
95 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions|
96 // to do zero-copy reads.
97 void MarkConsumed(size_t num_bytes);
98
99 // Appends all of the readable data to |buffer| and marks all of the appended
100 // data as consumed.
vasilvvc48c8712019-03-11 13:38:16 -0700101 void Read(std::string* buffer);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500102
103 // Returns true if the sequncer has bytes available for reading.
104 bool HasBytesToRead() const;
105
106 // Number of bytes available to read.
107 size_t ReadableBytes() const;
108
109 // Returns true if the sequencer has delivered the fin.
110 bool IsClosed() const;
111
112 // Calls |OnDataAvailable| on |stream_| if there is buffered data that can
113 // be processed, and causes |OnDataAvailable| to be called as new data
114 // arrives.
115 void SetUnblocked();
116
117 // Blocks processing of frames until |SetUnblocked| is called.
118 void SetBlockedUntilFlush();
119
120 // Sets the sequencer to discard all incoming data itself and not call
121 // |stream_->OnDataAvailable()|. |stream_->OnFinRead()| will be called
122 // automatically when the FIN is consumed (which may be immediately).
123 void StopReading();
124
125 // Free the memory of underlying buffer.
126 void ReleaseBuffer();
127
128 // Free the memory of underlying buffer when no bytes remain in it.
129 void ReleaseBufferIfEmpty();
130
131 // Number of bytes in the buffer right now.
132 size_t NumBytesBuffered() const;
133
134 // Number of bytes has been consumed.
135 QuicStreamOffset NumBytesConsumed() const;
136
137 QuicStreamOffset close_offset() const { return close_offset_; }
138
139 int num_frames_received() const { return num_frames_received_; }
140
141 int num_duplicate_frames_received() const {
142 return num_duplicate_frames_received_;
143 }
144
145 bool ignore_read_data() const { return ignore_read_data_; }
146
147 void set_level_triggered(bool level_triggered) {
148 level_triggered_ = level_triggered;
149 }
150
151 bool level_triggered() const { return level_triggered_; }
152
153 void set_stream(StreamInterface* stream) { stream_ = stream; }
154
155 // Returns string describing internal state.
vasilvvc48c8712019-03-11 13:38:16 -0700156 const std::string DebugString() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500157
158 private:
159 friend class test::QuicStreamSequencerPeer;
160
161 // Deletes and records as consumed any buffered data that is now in-sequence.
162 // (To be called only after StopReading has been called.)
163 void FlushBufferedFrames();
164
165 // Wait until we've seen 'offset' bytes, and then terminate the stream.
166 void CloseStreamAtOffset(QuicStreamOffset offset);
167
168 // If we've received a FIN and have processed all remaining data, then inform
169 // the stream of FIN, and clear buffers.
170 bool MaybeCloseStream();
171
172 // Shared implementation between OnStreamFrame and OnCryptoFrame.
173 void OnFrameData(QuicStreamOffset byte_offset,
174 size_t data_len,
175 const char* data_buffer);
176
177 // The stream which owns this sequencer.
178 StreamInterface* stream_;
179
180 // Stores received data in offset order.
181 QuicStreamSequencerBuffer buffered_frames_;
182
183 // The offset, if any, we got a stream termination for. When this many bytes
184 // have been processed, the sequencer will be closed.
185 QuicStreamOffset close_offset_;
186
187 // If true, the sequencer is blocked from passing data to the stream and will
188 // buffer all new incoming data until FlushBufferedFrames is called.
189 bool blocked_;
190
191 // Count of the number of frames received.
192 int num_frames_received_;
193
194 // Count of the number of duplicate frames received.
195 int num_duplicate_frames_received_;
196
197 // If true, all incoming data will be discarded.
198 bool ignore_read_data_;
199
200 // If false, only call OnDataAvailable() when it becomes newly unblocked.
201 // Otherwise, call OnDataAvailable() when number of readable bytes changes.
202 bool level_triggered_;
203
204 // Latched value of quic_stop_reading_when_level_triggered flag. When true,
205 // the sequencer will discard incoming data (but not FIN bits) after
206 // StopReading is called, even in level_triggered_ mode.
207 const bool stop_reading_when_level_triggered_;
208};
209
210} // namespace quic
211
212#endif // QUICHE_QUIC_CORE_QUIC_STREAM_SEQUENCER_H_