OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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 NET_QUIC_STREAM_SEQUENCER_BUFFER_H_ |
| 6 #define NET_QUIC_STREAM_SEQUENCER_BUFFER_H_ |
| 7 |
| 8 // StreamSequencerBuffer implements QuicStreamSequencerBufferInterface. |
| 9 // It is a circular stream buffer with random write and |
| 10 // in-sequence read. It consists of an std::vector of pointers pointing |
| 11 // to memory blocks created as needed and a std::list of Gaps to indicate |
| 12 // the missing data between the data already written into the buffer. |
| 13 // - Data are written in with offset indicating where it should be in the |
| 14 // stream, and the buffer grown as needed (up to the maximum buffer capacity), |
| 15 // without expensive copying (extra blocks are allocated). |
| 16 // - Data can be read from the buffer if there is no gap before it, |
| 17 // and the buffer shrinks as the data are consumed. |
| 18 // - An upper limit on the number of blocks in the buffer provides an upper |
| 19 // bound on memory use. |
| 20 // |
| 21 // This class is thread-unsafe. |
| 22 // |
| 23 // StreamSequencerBuffer maintains a concept of the readable region, which |
| 24 // contains all written data that has not been read. |
| 25 // It promises stability of the underlying memory addresses in the readable |
| 26 // region, so pointers into it can be maintained, and the offset of a pointer |
| 27 // from the start of the read region can be calculated. |
| 28 // |
| 29 // Expected Use: |
| 30 // StreamSequencerBuffer buffer(2.5 * 8 * 1024); |
| 31 // std::string source(1024, 'a'); |
| 32 // base::StringPiece std::string_piece(source.data(), source.size()); |
| 33 // size_t written = 0; |
| 34 // buffer.OnStreamData(800, std::string_piece, GetEpollClockNow(), &written); |
| 35 // source = std::string{800, 'b'}; |
| 36 // base::StringPiece std::string_piece1(source.data(), 800); |
| 37 // // Try to write to [1, 801), but should fail due to overlapping, |
| 38 // // res should be QUIC_INVALID_STREAM_DATA |
| 39 // auto res = buffer.OnStreamData(1, std::string_piece1, &written)); |
| 40 // // write to [0, 800), res should be QUIC_NO_ERROR |
| 41 // auto res = buffer.OnStreamData(0, std::string_piece1, GetEpollClockNow(), |
| 42 // &written); |
| 43 // |
| 44 // // Read into a iovec array with total capacity of 120 bytes. |
| 45 // char dest[120]; |
| 46 // iovec iovecs[3]{iovec{dest, 40}, iovec{dest + 40, 40}, |
| 47 // iovec{dest + 80, 40}}; |
| 48 // size_t read = buffer.Readv(iovecs, 3); |
| 49 // |
| 50 // // Get single readable region with timestamp. |
| 51 // QuicTime t; |
| 52 // iovec iov; |
| 53 // buffer.GetReadableRegion(iov, &t); |
| 54 // |
| 55 // // Get readable regions from [256, 1024) and consume some of it. |
| 56 // iovec iovs[2]; |
| 57 // int iov_count = buffer.GetReadableRegions(iovs, 2); |
| 58 // // Consume some bytes in iovs, returning number of bytes having been |
| 59 // consumed. |
| 60 // size_t consumed = consume_iovs(iovs, iov_count); |
| 61 // buffer.MarkConsumed(consumed); |
| 62 |
| 63 #include <functional> |
| 64 #include <list> |
| 65 #include <memory> |
| 66 |
| 67 #include "base/macros.h" |
| 68 #include "net/quic/quic_protocol.h" |
| 69 #include "net/quic/quic_stream_sequencer_buffer_interface.h" |
| 70 |
| 71 namespace net { |
| 72 |
| 73 namespace test { |
| 74 class StreamSequencerBufferPeer; |
| 75 } // namespace test |
| 76 |
| 77 class NET_EXPORT_PRIVATE StreamSequencerBuffer : public QuicStreamSequencerBuffe
rInterface { |
| 78 public: |
| 79 // A Gap indicates a missing chunk of bytes between |
| 80 // [begin_offset, end_offset) in the stream |
| 81 struct Gap { |
| 82 Gap(QuicStreamOffset begin_offset, QuicStreamOffset end_offset); |
| 83 QuicStreamOffset begin_offset; |
| 84 QuicStreamOffset end_offset; |
| 85 }; |
| 86 |
| 87 // A FrameInfo stores the length of a frame and the time it arrived. |
| 88 struct FrameInfo { |
| 89 FrameInfo(); |
| 90 FrameInfo(size_t length, QuicTime timestamp); |
| 91 |
| 92 size_t length; |
| 93 QuicTime timestamp; |
| 94 }; |
| 95 |
| 96 // Size of blocks used by this buffer. |
| 97 // Choose 8K to make block large enough to hold multiple frames, each of |
| 98 // which could be up to 1.5 KB. |
| 99 static const size_t kBlockSizeBytes = 8 * 1024; // 8KB |
| 100 |
| 101 // The basic storage block used by this buffer. |
| 102 struct BufferBlock { |
| 103 char buffer[kBlockSizeBytes]; |
| 104 }; |
| 105 |
| 106 explicit StreamSequencerBuffer(size_t max_capacity_bytes); |
| 107 |
| 108 ~StreamSequencerBuffer() override; |
| 109 |
| 110 // QuicStreamSequencerBufferInterface implementation. |
| 111 void Clear() override; |
| 112 bool Empty() const override; |
| 113 QuicErrorCode OnStreamData(QuicStreamOffset offset, |
| 114 base::StringPiece data, |
| 115 QuicTime timestamp, |
| 116 size_t* bytes_buffered) override; |
| 117 size_t Readv(const struct iovec* dest_iov, size_t dest_count) override; |
| 118 int GetReadableRegions(struct iovec* iov, int iov_len) const override; |
| 119 bool GetReadableRegion(iovec* iov, QuicTime* timestamp) const override; |
| 120 bool MarkConsumed(size_t bytes_buffered) override; |
| 121 size_t FlushBufferedFrames() override; |
| 122 bool HasBytesToRead() const override; |
| 123 QuicStreamOffset BytesConsumed() const override; |
| 124 size_t BytesBuffered() const override; |
| 125 |
| 126 private: |
| 127 friend class test::StreamSequencerBufferPeer; |
| 128 |
| 129 // Dispose the given buffer block. |
| 130 // After calling this method, blocks_[index] is set to nullptr |
| 131 // in order to indicate that no memory set is allocated for that block. |
| 132 void RetireBlock(size_t index); |
| 133 |
| 134 // Should only be called after the indexed block is read till the end of the |
| 135 // block or a gap has been reached. |
| 136 // If the block at |block_index| contains no buffered data, then the block is |
| 137 // retired. |
| 138 void RetireBlockIfEmpty(size_t block_index); |
| 139 |
| 140 // Called within OnStreamData() to update the gap OnStreamData() writes into |
| 141 // (remove, split or change begin/end offset). |
| 142 void UpdateGapList(std::list<Gap>::iterator gap_with_new_data_written, |
| 143 QuicStreamOffset start_offset, |
| 144 size_t bytes_written); |
| 145 |
| 146 // Calculate the capacity of block at specified index. |
| 147 // Return value should be either kBlockSizeBytes for non-trailing blocks and |
| 148 // max_buffer_capacity % kBlockSizeBytes for trailing block. |
| 149 size_t GetBlockCapacity(size_t index) const; |
| 150 |
| 151 // Does not check if offset is within reasonable range. |
| 152 size_t GetBlockIndex(QuicStreamOffset offset) const; |
| 153 |
| 154 // Given an offset in the stream, return the offset from the beginning of the |
| 155 // block which contains this data. |
| 156 size_t GetInBlockOffset(QuicStreamOffset offset) const; |
| 157 |
| 158 // Get offset relative to index 0 in logical 1st block to start next read. |
| 159 size_t ReadOffset() const; |
| 160 |
| 161 // Get the index of the logical 1st block to start next read. |
| 162 size_t NextBlockToRead() const; |
| 163 |
| 164 // Returns number of bytes available to be read out. |
| 165 size_t ReadableBytes() const; |
| 166 |
| 167 // Called after Readv() and MarkConsumed() to keep frame_arrival_time_map_ |
| 168 // up to date. |
| 169 // |offset| is the byte next read should start from. All frames before it |
| 170 // should be removed from the map. |
| 171 void UpdateFrameArrivalMap(QuicStreamOffset offset); |
| 172 |
| 173 // The maximum total capacity of this buffer in byte, as constructed. |
| 174 const size_t max_buffer_capacity_bytes_; |
| 175 |
| 176 // How many blocks this buffer would need when it reaches full capacity. |
| 177 const size_t blocks_count_; |
| 178 |
| 179 // Number of bytes read out of buffer. |
| 180 QuicStreamOffset total_bytes_read_; |
| 181 |
| 182 // Contains Gaps which represents currently missing data. |
| 183 std::list<Gap> gaps_; |
| 184 |
| 185 // An ordered, variable-length std::list of blocks, with the length limited |
| 186 // such that the number of blocks never exceeds blocks_count_. |
| 187 // Each std::list entry can hold up to kBlockSizeBytes bytes. |
| 188 std::vector<BufferBlock*> blocks_; |
| 189 |
| 190 // Number of bytes in buffer. |
| 191 size_t num_bytes_buffered_; |
| 192 |
| 193 // Stores all the buffered frames' start offset, length and arrival time. |
| 194 std::map<QuicStreamOffset, FrameInfo> frame_arrival_time_map_; |
| 195 |
| 196 DISALLOW_COPY_AND_ASSIGN(StreamSequencerBuffer); |
| 197 }; |
| 198 } // namespace net |
| 199 |
| 200 #endif // NET_QUIC_STREAM_SEQUENCER_BUFFER_H_ |
OLD | NEW |