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.
5 #ifndef NET_QUIC_QUIC_STREAM_SEQUENCER_H_
6 #define NET_QUIC_QUIC_STREAM_SEQUENCER_H_
11 #include "base/basictypes.h"
12 #include "net/base/iovec.h"
13 #include "net/quic/quic_protocol.h"
18 class QuicStreamSequencerPeer
;
22 class ReliableQuicStream
;
24 // Buffers frames until we have something which can be passed
25 // up to the next layer.
26 class NET_EXPORT_PRIVATE QuicStreamSequencer
{
28 // A contiguous segment received by a QUIC stream.
30 FrameData(QuicStreamOffset offset
, std::string segment
);
32 const QuicStreamOffset offset
;
36 // TODO(alyssar) use something better than strings.
37 // Maybe write new frames into a ring buffer, and keep track of consumed
39 typedef std::list
<FrameData
> FrameList
;
41 explicit QuicStreamSequencer(ReliableQuicStream
* quic_stream
);
42 virtual ~QuicStreamSequencer();
44 // If the frame is the next one we need in order to process in-order data,
45 // ProcessData will be immediately called on the stream until all buffered
46 // data is processed or the stream fails to consume data. Any unconsumed
47 // data will be buffered. If the frame is not the next in line, it will be
49 void OnStreamFrame(const QuicStreamFrame
& frame
);
51 // Once data is buffered, it's up to the stream to read it when the stream
52 // can handle more data. The following three functions make that possible.
54 // Fills in up to iov_len iovecs with the next readable regions. Returns the
55 // number of iovs used. Non-destructive of the underlying data.
56 int GetReadableRegions(iovec
* iov
, size_t iov_len
) const;
58 // Copies the data into the iov_len buffers provided. Returns the number of
59 // bytes read. Any buffered data no longer in use will be released.
60 int Readv(const struct iovec
* iov
, size_t iov_len
);
62 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions|
63 // to do zero-copy reads.
64 void MarkConsumed(size_t num_bytes
);
66 // Returns true if the sequncer has bytes available for reading.
67 bool HasBytesToRead() const;
69 // Returns true if the sequencer has delivered the fin.
70 bool IsClosed() const;
72 // Calls |ProcessRawData| on |stream_| for each buffered frame that may
74 void FlushBufferedFrames();
76 // Blocks processing of frames until |FlushBufferedFrames| is called.
77 void SetBlockedUntilFlush();
79 size_t num_bytes_buffered() const { return num_bytes_buffered_
; }
80 QuicStreamOffset
num_bytes_consumed() const { return num_bytes_consumed_
; }
82 int num_frames_received() const { return num_frames_received_
; }
84 int num_duplicate_frames_received() const {
85 return num_duplicate_frames_received_
;
88 int num_early_frames_received() const { return num_early_frames_received_
; }
91 friend class test::QuicStreamSequencerPeer
;
93 // Finds the place the frame should be inserted. If an identical frame is
94 // present, stops on the identical frame.
95 FrameList::iterator
FindInsertionPoint(const QuicStreamFrame
& frame
);
97 // Returns true if |frame| contains data which overlaps buffered data
98 // (indicating an invalid stream frame has been received).
99 bool FrameOverlapsBufferedData(
100 const QuicStreamFrame
& frame
,
101 FrameList::const_iterator insertion_point
) const;
103 // Returns true if the sequencer has received this frame before.
104 bool IsDuplicate(const QuicStreamFrame
& frame
,
105 FrameList::const_iterator insertion_point
) const;
107 // Wait until we've seen 'offset' bytes, and then terminate the stream.
108 void CloseStreamAtOffset(QuicStreamOffset offset
);
110 // If we've received a FIN and have processed all remaining data, then inform
111 // the stream of FIN, and clear buffers.
112 bool MaybeCloseStream();
114 // Called whenever bytes are consumed by the stream. Updates
115 // num_bytes_consumed_ and num_bytes_buffered_.
116 void RecordBytesConsumed(size_t bytes_consumed
);
118 // The stream which owns this sequencer.
119 ReliableQuicStream
* stream_
;
121 // The last data consumed by the stream.
122 QuicStreamOffset num_bytes_consumed_
;
124 // Stores buffered frames in offset order.
125 FrameList buffered_frames_
;
127 // The offset, if any, we got a stream termination for. When this many bytes
128 // have been processed, the sequencer will be closed.
129 QuicStreamOffset close_offset_
;
131 // If true, the sequencer is blocked from passing data to the stream and will
132 // buffer all new incoming data until FlushBufferedFrames is called.
135 // Tracks how many bytes the sequencer has buffered.
136 size_t num_bytes_buffered_
;
138 // Count of the number of frames received.
139 int num_frames_received_
;
141 // Count of the number of duplicate frames received.
142 int num_duplicate_frames_received_
;
144 // Count of the number of frames received before all previous frames were
146 int num_early_frames_received_
;
148 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer
);
153 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_