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/base/iovec.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, 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 |
28 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream); | 41 explicit QuicStreamSequencer(ReliableQuicStream* quic_stream); |
29 virtual ~QuicStreamSequencer(); | 42 virtual ~QuicStreamSequencer(); |
30 | 43 |
31 // If the frame is the next one we need in order to process in-order data, | 44 // 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 | 45 // ProcessData will be immediately called on the stream until all buffered |
33 // data is processed or the stream fails to consume data. Any unconsumed | 46 // 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 | 47 // data will be buffered. If the frame is not the next in line, it will be |
35 // buffered. | 48 // buffered. |
36 void OnStreamFrame(const QuicStreamFrame& frame); | 49 void OnStreamFrame(const QuicStreamFrame& frame); |
37 | 50 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 83 |
71 int num_duplicate_frames_received() const { | 84 int num_duplicate_frames_received() const { |
72 return num_duplicate_frames_received_; | 85 return num_duplicate_frames_received_; |
73 } | 86 } |
74 | 87 |
75 int num_early_frames_received() const { return num_early_frames_received_; } | 88 int num_early_frames_received() const { return num_early_frames_received_; } |
76 | 89 |
77 private: | 90 private: |
78 friend class test::QuicStreamSequencerPeer; | 91 friend class test::QuicStreamSequencerPeer; |
79 | 92 |
| 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); |
| 96 |
80 // Returns true if |frame| contains data which overlaps buffered data | 97 // Returns true if |frame| contains data which overlaps buffered data |
81 // (indicating an invalid stream frame has been received). | 98 // (indicating an invalid stream frame has been received). |
82 bool FrameOverlapsBufferedData(const QuicStreamFrame& frame) const; | 99 bool FrameOverlapsBufferedData( |
| 100 const QuicStreamFrame& frame, |
| 101 FrameList::const_iterator insertion_point) const; |
83 | 102 |
84 // Returns true if the sequencer has received this frame before. | 103 // Returns true if the sequencer has received this frame before. |
85 bool IsDuplicate(const QuicStreamFrame& frame) const; | 104 bool IsDuplicate(const QuicStreamFrame& frame, |
| 105 FrameList::const_iterator insertion_point) const; |
86 | 106 |
87 // Wait until we've seen 'offset' bytes, and then terminate the stream. | 107 // Wait until we've seen 'offset' bytes, and then terminate the stream. |
88 void CloseStreamAtOffset(QuicStreamOffset offset); | 108 void CloseStreamAtOffset(QuicStreamOffset offset); |
89 | 109 |
90 // If we've received a FIN and have processed all remaining data, then inform | 110 // If we've received a FIN and have processed all remaining data, then inform |
91 // the stream of FIN, and clear buffers. | 111 // the stream of FIN, and clear buffers. |
92 bool MaybeCloseStream(); | 112 bool MaybeCloseStream(); |
93 | 113 |
94 // Called whenever bytes are consumed by the stream. Updates | 114 // Called whenever bytes are consumed by the stream. Updates |
95 // num_bytes_consumed_ and num_bytes_buffered_. | 115 // num_bytes_consumed_ and num_bytes_buffered_. |
96 void RecordBytesConsumed(size_t bytes_consumed); | 116 void RecordBytesConsumed(size_t bytes_consumed); |
97 | 117 |
98 // The stream which owns this sequencer. | 118 // The stream which owns this sequencer. |
99 ReliableQuicStream* stream_; | 119 ReliableQuicStream* stream_; |
100 | 120 |
101 // The last data consumed by the stream. | 121 // The last data consumed by the stream. |
102 QuicStreamOffset num_bytes_consumed_; | 122 QuicStreamOffset num_bytes_consumed_; |
103 | 123 |
104 // TODO(alyssar) use something better than strings. | 124 // Stores buffered frames in offset order. |
105 // TODO(rjshade): In future we may support retransmission of partial stream | 125 FrameList buffered_frames_; |
106 // frames, in which case we will have to allow receipt of overlapping frames. | |
107 // Maybe write new frames into a ring buffer, and keep track of consumed | |
108 // bytes, and gaps. | |
109 typedef std::map<QuicStreamOffset, std::string> FrameMap; | |
110 | |
111 // Stores buffered frames (maps from byte offset -> frame data as string). | |
112 FrameMap buffered_frames_; | |
113 | 126 |
114 // The offset, if any, we got a stream termination for. When this many bytes | 127 // The offset, if any, we got a stream termination for. When this many bytes |
115 // have been processed, the sequencer will be closed. | 128 // have been processed, the sequencer will be closed. |
116 QuicStreamOffset close_offset_; | 129 QuicStreamOffset close_offset_; |
117 | 130 |
118 // If true, the sequencer is blocked from passing data to the stream and will | 131 // If true, the sequencer is blocked from passing data to the stream and will |
119 // buffer all new incoming data until FlushBufferedFrames is called. | 132 // buffer all new incoming data until FlushBufferedFrames is called. |
120 bool blocked_; | 133 bool blocked_; |
121 | 134 |
122 // Tracks how many bytes the sequencer has buffered. | 135 // Tracks how many bytes the sequencer has buffered. |
123 size_t num_bytes_buffered_; | 136 size_t num_bytes_buffered_; |
124 | 137 |
125 // Count of the number of frames received. | 138 // Count of the number of frames received. |
126 int num_frames_received_; | 139 int num_frames_received_; |
127 | 140 |
128 // Count of the number of duplicate frames received. | 141 // Count of the number of duplicate frames received. |
129 int num_duplicate_frames_received_; | 142 int num_duplicate_frames_received_; |
130 | 143 |
131 // Count of the number of frames received before all previous frames were | 144 // Count of the number of frames received before all previous frames were |
132 // received. | 145 // received. |
133 int num_early_frames_received_; | 146 int num_early_frames_received_; |
134 | 147 |
135 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer); | 148 DISALLOW_COPY_AND_ASSIGN(QuicStreamSequencer); |
136 }; | 149 }; |
137 | 150 |
138 } // namespace net | 151 } // namespace net |
139 | 152 |
140 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ | 153 #endif // NET_QUIC_QUIC_STREAM_SEQUENCER_H_ |
OLD | NEW |