Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_stream_sequencer.h
blob8a2e4d87c67e9512d1fe39b36d84787fadfc8f7e
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_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "net/base/iovec.h"
13 #include "net/quic/quic_protocol.h"
15 namespace net {
17 namespace test {
18 class QuicStreamSequencerPeer;
19 } // namespace test
21 class QuicSession;
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 {
27 public:
28 // A contiguous segment received by a QUIC stream.
29 struct FrameData {
30 FrameData(QuicStreamOffset offset, std::string segment);
32 const QuicStreamOffset offset;
33 std::string segment;
36 // TODO(alyssar) use something better than strings.
37 // Maybe write new frames into a ring buffer, and keep track of consumed
38 // bytes, and gaps.
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
48 // buffered.
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 // TODO(rch): remove this method and instead implement it as a helper method
61 // based on GetReadableRegions and MarkConsumed.
62 int Readv(const struct iovec* iov, size_t iov_len);
64 // Consumes |num_bytes| data. Used in conjunction with |GetReadableRegions|
65 // to do zero-copy reads.
66 void MarkConsumed(size_t num_bytes);
68 // Returns true if the sequncer has bytes available for reading.
69 bool HasBytesToRead() const;
71 // Returns true if the sequencer has delivered the fin.
72 bool IsClosed() const;
74 // Calls |OnDataAvailable| on |stream_| if there is buffered data that can
75 // be processed, and causes |OnDataAvailable| to be called as new data
76 // arrives.
77 void SetUnblocked();
79 // Blocks processing of frames until |SetUnblocked| is called.
80 void SetBlockedUntilFlush();
82 size_t num_bytes_buffered() const { return num_bytes_buffered_; }
83 QuicStreamOffset num_bytes_consumed() const { return num_bytes_consumed_; }
85 int num_frames_received() const { return num_frames_received_; }
87 int num_duplicate_frames_received() const {
88 return num_duplicate_frames_received_;
91 int num_early_frames_received() const { return num_early_frames_received_; }
93 private:
94 friend class test::QuicStreamSequencerPeer;
96 // Finds the place the frame should be inserted. If an identical frame is
97 // present, stops on the identical frame.
98 FrameList::iterator FindInsertionPoint(const QuicStreamFrame& frame);
100 // Returns true if |frame| contains data which overlaps buffered data
101 // (indicating an invalid stream frame has been received).
102 bool FrameOverlapsBufferedData(
103 const QuicStreamFrame& frame,
104 FrameList::const_iterator insertion_point) const;
106 // Returns true if the sequencer has received this frame before.
107 bool IsDuplicate(const QuicStreamFrame& frame,
108 FrameList::const_iterator insertion_point) const;
110 // Wait until we've seen 'offset' bytes, and then terminate the stream.
111 void CloseStreamAtOffset(QuicStreamOffset offset);
113 // If we've received a FIN and have processed all remaining data, then inform
114 // the stream of FIN, and clear buffers.
115 bool MaybeCloseStream();
117 // Called whenever bytes are consumed by the stream. Updates
118 // num_bytes_consumed_ and num_bytes_buffered_.
119 void RecordBytesConsumed(size_t bytes_consumed);
121 // The stream which owns this sequencer.
122 ReliableQuicStream* stream_;
124 // The last data consumed by the stream.
125 QuicStreamOffset num_bytes_consumed_;
127 // Stores buffered frames in offset order.
128 FrameList buffered_frames_;
130 // The offset, if any, we got a stream termination for. When this many bytes
131 // have been processed, the sequencer will be closed.
132 QuicStreamOffset close_offset_;
134 // If true, the sequencer is blocked from passing data to the stream and will
135 // buffer all new incoming data until FlushBufferedFrames is called.
136 bool blocked_;
138 // Tracks how many bytes the sequencer has buffered.
139 size_t num_bytes_buffered_;
141 // Count of the number of frames received.
142 int num_frames_received_;
144 // Count of the number of duplicate frames received.
145 int num_duplicate_frames_received_;
147 // Count of the number of frames received before all previous frames were
148 // received.
149 int num_early_frames_received_;
151 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer);
154 } // namespace net
156 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_