OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | 5 #ifndef NET_QUIC_QUIC_STREAM_SEQUENCER_H_ |
6 #define NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | 6 #define NET_QUIC_QUIC_STREAM_SEQUENCER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "net/base/iovec.h" | 12 #include "net/quic/quic_frame_list.h" |
13 #include "net/quic/quic_protocol.h" | 13 #include "net/quic/quic_protocol.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 namespace test { | 17 namespace test { |
18 class QuicStreamSequencerPeer; | 18 class QuicStreamSequencerPeer; |
19 } // namespace test | 19 } // namespace test |
20 | 20 |
21 class QuicSession; | 21 class QuicSession; |
22 class ReliableQuicStream; | 22 class ReliableQuicStream; |
23 | 23 |
24 // Buffers frames until we have something which can be passed | 24 // Buffers frames until we have something which can be passed |
25 // up to the next layer. | 25 // up to the next layer. |
26 class NET_EXPORT_PRIVATE QuicStreamSequencer { | 26 class NET_EXPORT_PRIVATE QuicStreamSequencer { |
27 public: | 27 public: |
28 // A contiguous segment received by a QUIC stream. | |
29 struct FrameData { | |
30 FrameData(QuicStreamOffset offset, const std::string& segment); | |
31 | |
32 const QuicStreamOffset offset; | |
33 std::string segment; | |
34 }; | |
35 | |
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; | |
40 | |
41 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream); | 28 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream); |
42 virtual ~QuicStreamSequencer(); | 29 virtual ~QuicStreamSequencer(); |
43 | 30 |
44 // If the frame is the next one we need in order to process in-order data, | 31 // 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 | 32 // ProcessData will be immediately called on the stream until all buffered |
46 // data is processed or the stream fails to consume data. Any unconsumed | 33 // 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 | 34 // data will be buffered. If the frame is not the next in line, it will be |
48 // buffered. | 35 // buffered. |
49 void OnStreamFrame(const QuicStreamFrame& frame); | 36 void OnStreamFrame(const QuicStreamFrame& frame); |
50 | 37 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 73 |
87 int num_duplicate_frames_received() const { | 74 int num_duplicate_frames_received() const { |
88 return num_duplicate_frames_received_; | 75 return num_duplicate_frames_received_; |
89 } | 76 } |
90 | 77 |
91 int num_early_frames_received() const { return num_early_frames_received_; } | 78 int num_early_frames_received() const { return num_early_frames_received_; } |
92 | 79 |
93 private: | 80 private: |
94 friend class test::QuicStreamSequencerPeer; | 81 friend class test::QuicStreamSequencerPeer; |
95 | 82 |
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); | |
99 | |
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; | |
105 | |
106 // Returns true if the sequencer has received this frame before. | |
107 bool IsDuplicate(const QuicStreamFrame& frame, | |
108 FrameList::const_iterator insertion_point) const; | |
109 | |
110 // Wait until we've seen 'offset' bytes, and then terminate the stream. | 83 // Wait until we've seen 'offset' bytes, and then terminate the stream. |
111 void CloseStreamAtOffset(QuicStreamOffset offset); | 84 void CloseStreamAtOffset(QuicStreamOffset offset); |
112 | 85 |
113 // If we've received a FIN and have processed all remaining data, then inform | 86 // If we've received a FIN and have processed all remaining data, then inform |
114 // the stream of FIN, and clear buffers. | 87 // the stream of FIN, and clear buffers. |
115 bool MaybeCloseStream(); | 88 bool MaybeCloseStream(); |
116 | 89 |
117 // Called whenever bytes are consumed by the stream. Updates | 90 // Called whenever bytes are consumed by the stream. Updates |
118 // num_bytes_consumed_ and num_bytes_buffered_. | 91 // num_bytes_consumed_ and num_bytes_buffered_. |
119 void RecordBytesConsumed(size_t bytes_consumed); | 92 void RecordBytesConsumed(size_t bytes_consumed); |
120 | 93 |
121 // The stream which owns this sequencer. | 94 // The stream which owns this sequencer. |
122 ReliableQuicStream* stream_; | 95 ReliableQuicStream* stream_; |
123 | 96 |
124 // The last data consumed by the stream. | 97 // The last data consumed by the stream. |
125 QuicStreamOffset num_bytes_consumed_; | 98 QuicStreamOffset num_bytes_consumed_; |
126 | 99 |
127 // Stores buffered frames in offset order. | 100 // Stores buffered frames in offset order. |
128 FrameList buffered_frames_; | 101 QuicFrameList buffered_frames_; |
129 | 102 |
130 // The offset, if any, we got a stream termination for. When this many bytes | 103 // The offset, if any, we got a stream termination for. When this many bytes |
131 // have been processed, the sequencer will be closed. | 104 // have been processed, the sequencer will be closed. |
132 QuicStreamOffset close_offset_; | 105 QuicStreamOffset close_offset_; |
133 | 106 |
134 // If true, the sequencer is blocked from passing data to the stream and will | 107 // If true, the sequencer is blocked from passing data to the stream and will |
135 // buffer all new incoming data until FlushBufferedFrames is called. | 108 // buffer all new incoming data until FlushBufferedFrames is called. |
136 bool blocked_; | 109 bool blocked_; |
137 | 110 |
138 // Tracks how many bytes the sequencer has buffered. | 111 // Tracks how many bytes the sequencer has buffered. |
139 size_t num_bytes_buffered_; | 112 size_t num_bytes_buffered_; |
140 | 113 |
141 // Count of the number of frames received. | 114 // Count of the number of frames received. |
142 int num_frames_received_; | 115 int num_frames_received_; |
143 | 116 |
144 // Count of the number of duplicate frames received. | 117 // Count of the number of duplicate frames received. |
145 int num_duplicate_frames_received_; | 118 int num_duplicate_frames_received_; |
146 | 119 |
147 // Count of the number of frames received before all previous frames were | 120 // Count of the number of frames received before all previous frames were |
148 // received. | 121 // received. |
149 int num_early_frames_received_; | 122 int num_early_frames_received_; |
150 | 123 |
151 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer); | 124 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer); |
152 }; | 125 }; |
153 | 126 |
154 } // namespace net | 127 } // namespace net |
155 | 128 |
156 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | 129 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ |
OLD | NEW |