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