| 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 <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "net/base/iovec.h" | |
| 13 #include "net/quic/quic_protocol.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 namespace test { | |
| 18 class QuicStreamSequencerPeer; | |
| 19 } // namespace test | |
| 20 | |
| 21 class QuicSession; | |
| 22 class ReliableQuicStream; | |
| 23 | |
| 24 // Buffers frames until we have something which can be passed | |
| 25 // up to the next layer. | |
| 26 class NET_EXPORT_PRIVATE QuicStreamSequencer { | |
| 27 public: | |
| 28 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream); | |
| 29 virtual ~QuicStreamSequencer(); | |
| 30 | |
| 31 // If the frame is the next one we need in order to process in-order data, | |
| 32 // ProcessData will be immediately called on the stream until all buffered | |
| 33 // data is processed or the stream fails to consume data. Any unconsumed | |
| 34 // data will be buffered. If the frame is not the next in line, it will be | |
| 35 // buffered. | |
| 36 void OnStreamFrame(const QuicStreamFrame& frame); | |
| 37 | |
| 38 // Once data is buffered, it's up to the stream to read it when the stream | |
| 39 // can handle more data. The following three functions make that possible. | |
| 40 | |
| 41 // Fills in up to iov_len iovecs with the next readable regions. Returns the | |
| 42 // number of iovs used. Non-destructive of the underlying data. | |
| 43 int GetReadableRegions(iovec* iov, size_t iov_len); | |
| 44 | |
| 45 // Copies the data into the iov_len buffers provided. Returns the number of | |
| 46 // bytes read. Any buffered data no longer in use will be released. | |
| 47 int Readv(const struct iovec* iov, size_t iov_len); | |
| 48 | |
| 49 // Returns true if the sequncer has bytes available for reading. | |
| 50 bool HasBytesToRead() const; | |
| 51 | |
| 52 // Returns true if the sequencer has delivered the fin. | |
| 53 bool IsClosed() const; | |
| 54 | |
| 55 // Returns true if the sequencer has received this frame before. | |
| 56 bool IsDuplicate(const QuicStreamFrame& frame) const; | |
| 57 | |
| 58 // Returns true if |frame| contains data which overlaps buffered data | |
| 59 // (indicating an invalid stream frame has been received). | |
| 60 bool FrameOverlapsBufferedData(const QuicStreamFrame& frame) const; | |
| 61 | |
| 62 // Calls |ProcessRawData| on |stream_| for each buffered frame that may | |
| 63 // be processed. | |
| 64 void FlushBufferedFrames(); | |
| 65 | |
| 66 // Blocks processing of frames until |FlushBufferedFrames| is called. | |
| 67 void SetBlockedUntilFlush(); | |
| 68 | |
| 69 size_t num_bytes_buffered() const { return num_bytes_buffered_; } | |
| 70 QuicStreamOffset num_bytes_consumed() const { return num_bytes_consumed_; } | |
| 71 | |
| 72 int num_frames_received() const { return num_frames_received_; } | |
| 73 | |
| 74 int num_duplicate_frames_received() const { | |
| 75 return num_duplicate_frames_received_; | |
| 76 } | |
| 77 | |
| 78 int num_early_frames_received() const { return num_early_frames_received_; } | |
| 79 | |
| 80 private: | |
| 81 friend class test::QuicStreamSequencerPeer; | |
| 82 | |
| 83 // Wait until we've seen 'offset' bytes, and then terminate the stream. | |
| 84 void CloseStreamAtOffset(QuicStreamOffset offset); | |
| 85 | |
| 86 // If we've received a FIN and have processed all remaining data, then inform | |
| 87 // the stream of FIN, and clear buffers. | |
| 88 bool MaybeCloseStream(); | |
| 89 | |
| 90 // Called whenever bytes are consumed by the stream. Updates | |
| 91 // num_bytes_consumed_ and num_bytes_buffered_. | |
| 92 void RecordBytesConsumed(size_t bytes_consumed); | |
| 93 | |
| 94 // The stream which owns this sequencer. | |
| 95 ReliableQuicStream* stream_; | |
| 96 | |
| 97 // The last data consumed by the stream. | |
| 98 QuicStreamOffset num_bytes_consumed_; | |
| 99 | |
| 100 // TODO(alyssar) use something better than strings. | |
| 101 // TODO(rjshade): In future we may support retransmission of partial stream | |
| 102 // frames, in which case we will have to allow receipt of overlapping frames. | |
| 103 // Maybe write new frames into a ring buffer, and keep track of consumed | |
| 104 // bytes, and gaps. | |
| 105 typedef std::map<QuicStreamOffset, std::string> FrameMap; | |
| 106 | |
| 107 // Stores buffered frames (maps from sequence number -> frame data as string). | |
| 108 FrameMap buffered_frames_; | |
| 109 | |
| 110 // The offset, if any, we got a stream termination for. When this many bytes | |
| 111 // have been processed, the sequencer will be closed. | |
| 112 QuicStreamOffset close_offset_; | |
| 113 | |
| 114 // If true, the sequencer is blocked from passing data to the stream and will | |
| 115 // buffer all new incoming data until FlushBufferedFrames is called. | |
| 116 bool blocked_; | |
| 117 | |
| 118 // Tracks how many bytes the sequencer has buffered. | |
| 119 size_t num_bytes_buffered_; | |
| 120 | |
| 121 // Count of the number of frames received. | |
| 122 int num_frames_received_; | |
| 123 | |
| 124 // Count of the number of duplicate frames received. | |
| 125 int num_duplicate_frames_received_; | |
| 126 | |
| 127 // Count of the number of frames received before all previous frames were | |
| 128 // received. | |
| 129 int num_early_frames_received_; | |
| 130 | |
| 131 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer); | |
| 132 }; | |
| 133 | |
| 134 } // namespace net | |
| 135 | |
| 136 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | |
| OLD | NEW |