Re-land: C++ readability review
[chromium-blink-merge.git] / net / quic / quic_stream_sequencer.h
blob3570d3b86cc1a1fcc3aad61e10bd185c7a8f1163
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 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream);
29 virtual ~QuicStreamSequencer();
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);
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.
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);
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);
49 // Returns true if the sequncer has bytes available for reading.
50 bool HasBytesToRead() const;
52 // Returns true if the sequencer has delivered the fin.
53 bool IsClosed() const;
55 // Returns true if the sequencer has received this frame before.
56 bool IsDuplicate(const QuicStreamFrame& frame) const;
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;
62 // Calls |ProcessRawData| on |stream_| for each buffered frame that may
63 // be processed.
64 void FlushBufferedFrames();
66 // Blocks processing of frames until |FlushBufferedFrames| is called.
67 void SetBlockedUntilFlush();
69 size_t num_bytes_buffered() const { return num_bytes_buffered_; }
70 QuicStreamOffset num_bytes_consumed() const { return num_bytes_consumed_; }
72 int num_frames_received() const { return num_frames_received_; }
74 int num_duplicate_frames_received() const {
75 return num_duplicate_frames_received_;
78 int num_early_frames_received() const { return num_early_frames_received_; }
80 private:
81 friend class test::QuicStreamSequencerPeer;
83 // Wait until we've seen 'offset' bytes, and then terminate the stream.
84 void CloseStreamAtOffset(QuicStreamOffset offset);
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();
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);
94 // The stream which owns this sequencer.
95 ReliableQuicStream* stream_;
97 // The last data consumed by the stream.
98 QuicStreamOffset num_bytes_consumed_;
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;
107 // Stores buffered frames (maps from sequence number -> frame data as string).
108 FrameMap buffered_frames_;
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_;
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_;
118 // Tracks how many bytes the sequencer has buffered.
119 size_t num_bytes_buffered_;
121 // Count of the number of frames received.
122 int num_frames_received_;
124 // Count of the number of duplicate frames received.
125 int num_duplicate_frames_received_;
127 // Count of the number of frames received before all previous frames were
128 // received.
129 int num_early_frames_received_;
131 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer);
134 } // namespace net
136 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_