| OLD | NEW |
| (Empty) |
| 1 // 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 NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | |
| 6 #define NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <map> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "net/quic/quic_protocol.h" | |
| 14 #include "net/quic/quic_stream_sequencer_buffer.h" | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 namespace test { | |
| 19 class QuicStreamSequencerPeer; | |
| 20 } // namespace test | |
| 21 | |
| 22 class QuicClock; | |
| 23 class QuicSession; | |
| 24 class ReliableQuicStream; | |
| 25 | |
| 26 // Buffers frames until we have something which can be passed | |
| 27 // up to the next layer. | |
| 28 class NET_EXPORT_PRIVATE QuicStreamSequencer { | |
| 29 public: | |
| 30 QuicStreamSequencer(ReliableQuicStream* quic_stream, const QuicClock* clock); | |
| 31 virtual ~QuicStreamSequencer(); | |
| 32 | |
| 33 // If the frame is the next one we need in order to process in-order data, | |
| 34 // ProcessData will be immediately called on the stream until all buffered | |
| 35 // data is processed or the stream fails to consume data. Any unconsumed | |
| 36 // data will be buffered. If the frame is not the next in line, it will be | |
| 37 // buffered. | |
| 38 void OnStreamFrame(const QuicStreamFrame& frame); | |
| 39 | |
| 40 // Once data is buffered, it's up to the stream to read it when the stream | |
| 41 // can handle more data. The following three functions make that possible. | |
| 42 | |
| 43 // Fills in up to iov_len iovecs with the next readable regions. Returns the | |
| 44 // number of iovs used. Non-destructive of the underlying data. | |
| 45 int GetReadableRegions(iovec* iov, size_t iov_len) const; | |
| 46 | |
| 47 // Fills in one iovec with the next readable region. |timestamp| is | |
| 48 // data arrived at the sequencer, and is used for measuring head of | |
| 49 // line blocking (HOL). Returns false if there is no readable | |
| 50 // region available. | |
| 51 bool GetReadableRegion(iovec* iov, QuicTime* timestamp) const; | |
| 52 | |
| 53 // Copies the data into the iov_len buffers provided. Returns the number of | |
| 54 // bytes read. Any buffered data no longer in use will be released. | |
| 55 // TODO(rch): remove this method and instead implement it as a helper method | |
| 56 // based on GetReadableRegions and MarkConsumed. | |
| 57 int Readv(const struct iovec* iov, size_t iov_len); | |
| 58 | |
| 59 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions| | |
| 60 // to do zero-copy reads. | |
| 61 void MarkConsumed(size_t num_bytes); | |
| 62 | |
| 63 // Returns true if the sequncer has bytes available for reading. | |
| 64 bool HasBytesToRead() const; | |
| 65 | |
| 66 // Returns true if the sequencer has delivered the fin. | |
| 67 bool IsClosed() const; | |
| 68 | |
| 69 // Calls |OnDataAvailable| on |stream_| if there is buffered data that can | |
| 70 // be processed, and causes |OnDataAvailable| to be called as new data | |
| 71 // arrives. | |
| 72 void SetUnblocked(); | |
| 73 | |
| 74 // Blocks processing of frames until |SetUnblocked| is called. | |
| 75 void SetBlockedUntilFlush(); | |
| 76 | |
| 77 // Sets the sequencer to discard all incoming data itself and not call | |
| 78 // |stream_->OnDataAvailable()|. |stream_->OnFinRead()| will be called | |
| 79 // automatically when the FIN is consumed (which may be immediately). | |
| 80 void StopReading(); | |
| 81 | |
| 82 // Number of bytes in the buffer right now. | |
| 83 size_t NumBytesBuffered() const; | |
| 84 | |
| 85 // Number of bytes has been consumed. | |
| 86 QuicStreamOffset NumBytesConsumed() const; | |
| 87 | |
| 88 int num_frames_received() const { return num_frames_received_; } | |
| 89 | |
| 90 int num_duplicate_frames_received() const { | |
| 91 return num_duplicate_frames_received_; | |
| 92 } | |
| 93 | |
| 94 int num_early_frames_received() const { return num_early_frames_received_; } | |
| 95 | |
| 96 bool ignore_read_data() const { return ignore_read_data_; } | |
| 97 | |
| 98 // Returns std::string describing internal state. | |
| 99 const std::string DebugString() const; | |
| 100 | |
| 101 private: | |
| 102 friend class test::QuicStreamSequencerPeer; | |
| 103 | |
| 104 // Deletes and records as consumed any buffered data that is now in-sequence. | |
| 105 // (To be called only after StopReading has been called.) | |
| 106 void FlushBufferedFrames(); | |
| 107 | |
| 108 // Wait until we've seen 'offset' bytes, and then terminate the stream. | |
| 109 void CloseStreamAtOffset(QuicStreamOffset offset); | |
| 110 | |
| 111 // If we've received a FIN and have processed all remaining data, then inform | |
| 112 // the stream of FIN, and clear buffers. | |
| 113 bool MaybeCloseStream(); | |
| 114 | |
| 115 // The stream which owns this sequencer. | |
| 116 ReliableQuicStream* stream_; | |
| 117 | |
| 118 // Stores received data in offset order. | |
| 119 QuicStreamSequencerBuffer buffered_frames_; | |
| 120 | |
| 121 // The offset, if any, we got a stream termination for. When this many bytes | |
| 122 // have been processed, the sequencer will be closed. | |
| 123 QuicStreamOffset close_offset_; | |
| 124 | |
| 125 // If true, the sequencer is blocked from passing data to the stream and will | |
| 126 // buffer all new incoming data until FlushBufferedFrames is called. | |
| 127 bool blocked_; | |
| 128 | |
| 129 // Count of the number of frames received. | |
| 130 int num_frames_received_; | |
| 131 | |
| 132 // Count of the number of duplicate frames received. | |
| 133 int num_duplicate_frames_received_; | |
| 134 | |
| 135 // Count of the number of frames received before all previous frames were | |
| 136 // received. | |
| 137 int num_early_frames_received_; | |
| 138 | |
| 139 // Not owned. | |
| 140 const QuicClock* clock_; | |
| 141 | |
| 142 // If true, all incoming data will be discarded. | |
| 143 bool ignore_read_data_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer); | |
| 146 }; | |
| 147 | |
| 148 } // namespace net | |
| 149 | |
| 150 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | |
| OLD | NEW |