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 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "net/base/iovec.h" 13 #include "net/quic/quic_protocol.h" 14 15 using std::map; 16 using std::string; 17 18 namespace net { 19 20 namespace test { 21 class QuicStreamSequencerPeer; 22 } // namespace test 23 24 class QuicSession; 25 class ReliableQuicStream; 26 27 // Buffers frames until we have something which can be passed 28 // up to the next layer. 29 // TOOD(alyssar) add some checks for overflow attempts [1, 256,] [2, 256] 30 class NET_EXPORT_PRIVATE QuicStreamSequencer { 31 public: 32 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream); 33 QuicStreamSequencer(size_t max_frame_memory, 34 ReliableQuicStream* quic_stream); 35 36 virtual ~QuicStreamSequencer(); 37 38 // Returns the expected value of OnStreamFrame for this frame. 39 bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const; 40 41 // If the frame is the next one we need in order to process in-order data, 42 // ProcessData will be immediately called on the stream until all buffered 43 // data is processed or the stream fails to consume data. Any unconsumed 44 // data will be buffered. 45 // 46 // If the frame is not the next in line, it will either be buffered, and 47 // this will return true, or it will be rejected and this will return false. 48 bool OnStreamFrame(const QuicStreamFrame& frame); 49 50 // Once data is buffered, it's up to the stream to read it when the stream 51 // can handle more data. The following three functions make that possible. 52 53 // Fills in up to iov_len iovecs with the next readable regions. Returns the 54 // number of iovs used. Non-destructive of the underlying data. 55 int GetReadableRegions(iovec* iov, size_t iov_len); 56 57 // Copies the data into the iov_len buffers provided. Returns the number of 58 // bytes read. Any buffered data no longer in use will be released. 59 int Readv(const struct iovec* iov, size_t iov_len); 60 61 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions| 62 // to do zero-copy reads. 63 void MarkConsumed(size_t num_bytes); 64 65 // Returns true if the sequncer has bytes available for reading. 66 bool HasBytesToRead() const; 67 68 // Returns true if the sequencer has delivered the fin. 69 bool IsClosed() const; 70 71 // Returns true if the sequencer has received this frame before. 72 bool IsDuplicate(const QuicStreamFrame& frame) const; 73 74 // Calls |ProcessRawData| on |stream_| for each buffered frame that may 75 // be processed. 76 void FlushBufferedFrames(); 77 78 private: 79 friend class test::QuicStreamSequencerPeer; 80 81 // TODO(alyssar) use something better than strings. 82 typedef map<QuicStreamOffset, string> FrameMap; 83 84 // Wait until we've seen 'offset' bytes, and then terminate the stream. 85 void CloseStreamAtOffset(QuicStreamOffset offset); 86 87 bool MaybeCloseStream(); 88 89 ReliableQuicStream* stream_; // The stream which owns this sequencer. 90 QuicStreamOffset num_bytes_consumed_; // The last data consumed by the stream 91 FrameMap frames_; // sequence number -> frame 92 size_t max_frame_memory_; // the maximum memory the sequencer can buffer. 93 // The offset, if any, we got a stream termination for. When this many bytes 94 // have been processed, the sequencer will be closed. 95 QuicStreamOffset close_offset_; 96 }; 97 98 } // namespace net 99 100 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ 101