blob: 74e9d0d5ee2f55119ce8608b5f03c98d87c51f80 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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_CORE_QUIC_STREAM_SEND_BUFFER_H_
6#define QUICHE_QUIC_CORE_QUIC_STREAM_SEND_BUFFER_H_
7
8#include "net/third_party/quiche/src/quic/core/frames/quic_stream_frame.h"
9#include "net/third_party/quiche/src/quic/core/quic_interval_set.h"
wub553a9662019-03-28 20:13:23 -070010#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/platform/api/quic_containers.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_iovec.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice.h"
wub553a9662019-03-28 20:13:23 -070014#include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice_span.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015
16namespace quic {
17
18namespace test {
19class QuicStreamSendBufferPeer;
20class QuicStreamPeer;
21} // namespace test
22
23class QuicDataWriter;
24
25// BufferedSlice comprises information of a piece of stream data stored in
26// contiguous memory space. Please note, BufferedSlice is constructed when
27// stream data is saved in send buffer and is removed when stream data is fully
28// acked. It is move-only.
29struct BufferedSlice {
30 BufferedSlice(QuicMemSlice mem_slice, QuicStreamOffset offset);
31 BufferedSlice(BufferedSlice&& other);
32 BufferedSlice& operator=(BufferedSlice&& other);
33
34 BufferedSlice(const BufferedSlice& other) = delete;
35 BufferedSlice& operator=(const BufferedSlice& other) = delete;
36 ~BufferedSlice();
37
38 // Stream data of this data slice.
39 QuicMemSlice slice;
40 // Location of this data slice in the stream.
41 QuicStreamOffset offset;
42};
43
44struct StreamPendingRetransmission {
45 StreamPendingRetransmission(QuicStreamOffset offset, QuicByteCount length)
46 : offset(offset), length(length) {}
47
48 // Starting offset of this pending retransmission.
49 QuicStreamOffset offset;
50 // Length of this pending retransmission.
51 QuicByteCount length;
52
53 QUIC_EXPORT_PRIVATE bool operator==(
54 const StreamPendingRetransmission& other) const;
55};
56
57// QuicStreamSendBuffer contains a list of QuicStreamDataSlices. New data slices
58// are added to the tail of the list. Data slices are removed from the head of
59// the list when they get fully acked. Stream data can be retrieved and acked
60// across slice boundaries.
61class QUIC_EXPORT_PRIVATE QuicStreamSendBuffer {
62 public:
63 explicit QuicStreamSendBuffer(QuicBufferAllocator* allocator);
64 QuicStreamSendBuffer(const QuicStreamSendBuffer& other) = delete;
vasilvv00f47df2019-04-03 13:58:53 -070065 QuicStreamSendBuffer(QuicStreamSendBuffer&& other) = default;
QUICHE teama6ef0a62019-03-07 20:34:33 -050066 ~QuicStreamSendBuffer();
67
68 // Save |data_length| of data starts at |iov_offset| in |iov| to send buffer.
69 void SaveStreamData(const struct iovec* iov,
70 int iov_count,
71 size_t iov_offset,
72 QuicByteCount data_length);
73
74 // Save |slice| to send buffer.
75 void SaveMemSlice(QuicMemSlice slice);
76
wub553a9662019-03-28 20:13:23 -070077 // Save all slices in |span| to send buffer. Return total bytes saved.
78 QuicByteCount SaveMemSliceSpan(QuicMemSliceSpan span);
79
QUICHE teama6ef0a62019-03-07 20:34:33 -050080 // Called when |bytes_consumed| bytes has been consumed by the stream.
81 void OnStreamDataConsumed(size_t bytes_consumed);
82
83 // Write |data_length| of data starts at |offset|.
84 bool WriteStreamData(QuicStreamOffset offset,
85 QuicByteCount data_length,
86 QuicDataWriter* writer);
87
88 // Called when data [offset, offset + data_length) is acked or removed as
89 // stream is canceled. Removes fully acked data slice from send buffer. Set
90 // |newly_acked_length|. Returns false if trying to ack unsent data.
91 bool OnStreamDataAcked(QuicStreamOffset offset,
92 QuicByteCount data_length,
93 QuicByteCount* newly_acked_length);
94
95 // Called when data [offset, offset + data_length) is considered as lost.
96 void OnStreamDataLost(QuicStreamOffset offset, QuicByteCount data_length);
97
98 // Called when data [offset, offset + length) was retransmitted.
99 void OnStreamDataRetransmitted(QuicStreamOffset offset,
100 QuicByteCount data_length);
101
102 // Returns true if there is pending retransmissions.
103 bool HasPendingRetransmission() const;
104
105 // Returns next pending retransmissions.
106 StreamPendingRetransmission NextPendingRetransmission() const;
107
108 // Returns true if data [offset, offset + data_length) is outstanding and
109 // waiting to be acked. Returns false otherwise.
110 bool IsStreamDataOutstanding(QuicStreamOffset offset,
111 QuicByteCount data_length) const;
112
113 // Number of data slices in send buffer.
114 size_t size() const;
115
116 QuicStreamOffset stream_offset() const { return stream_offset_; }
117
118 uint64_t stream_bytes_written() const { return stream_bytes_written_; }
119
120 uint64_t stream_bytes_outstanding() const {
121 return stream_bytes_outstanding_;
122 }
123
124 const QuicIntervalSet<QuicStreamOffset>& bytes_acked() const {
125 return bytes_acked_;
126 }
127
128 const QuicIntervalSet<QuicStreamOffset>& pending_retransmissions() const {
129 return pending_retransmissions_;
130 }
131
132 private:
133 friend class test::QuicStreamSendBufferPeer;
134 friend class test::QuicStreamPeer;
135
136 // Called when data within offset [start, end) gets acked. Frees fully
137 // acked buffered slices if any. Returns false if the corresponding data does
138 // not exist or has been acked.
139 bool FreeMemSlices(QuicStreamOffset start, QuicStreamOffset end);
140
141 // Cleanup empty slices in order from buffered_slices_.
142 void CleanUpBufferedSlices();
143
144 QuicDeque<BufferedSlice> buffered_slices_;
145
146 // Offset of next inserted byte.
147 QuicStreamOffset stream_offset_;
148
149 QuicBufferAllocator* allocator_;
150
151 // Bytes that have been consumed by the stream.
152 uint64_t stream_bytes_written_;
153
154 // Bytes that have been consumed and are waiting to be acked.
155 uint64_t stream_bytes_outstanding_;
156
157 // Offsets of data that has been acked.
158 QuicIntervalSet<QuicStreamOffset> bytes_acked_;
159
160 // Data considered as lost and needs to be retransmitted.
161 QuicIntervalSet<QuicStreamOffset> pending_retransmissions_;
162
163 // Index of slice which contains data waiting to be written for the first
164 // time. -1 if send buffer is empty or all data has been written.
165 int32_t write_index_;
166};
167
168} // namespace quic
169
170#endif // QUICHE_QUIC_CORE_QUIC_STREAM_SEND_BUFFER_H_