Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_stream_sequencer.h
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.
4
5 #ifndef NET_QUIC_QUIC_STREAM_SEQUENCER_H_
6 #define NET_QUIC_QUIC_STREAM_SEQUENCER_H_
7
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "net/base/iovec.h"
12 #include "net/quic/quic_protocol.h"
13
14 using std::map;
15 using std::string;
16
17 namespace net {
18
19 namespace test {
20 class QuicStreamSequencerPeer;
21 }  // namespace test
22
23 class QuicSession;
24 class ReliableQuicStream;
25
26 // Buffers frames until we have something which can be passed
27 // up to the next layer.
28 // TOOD(alyssar) add some checks for overflow attempts [1, 256,] [2, 256]
29 class NET_EXPORT_PRIVATE QuicStreamSequencer {
30  public:
31   explicit QuicStreamSequencer(ReliableQuicStream* quic_stream);
32   QuicStreamSequencer(size_t max_frame_memory,
33                       ReliableQuicStream* quic_stream);
34
35   virtual ~QuicStreamSequencer();
36
37   // Returns the expected value of OnStreamFrame for this frame.
38   bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const;
39
40   // If the frame is the next one we need in order to process in-order data,
41   // ProcessData will be immediately called on the stream until all buffered
42   // data is processed or the stream fails to consume data.  Any unconsumed
43   // data will be buffered.
44   //
45   // If the frame is not the next in line, it will either be buffered, and
46   // this will return true, or it will be rejected and this will return false.
47   bool OnStreamFrame(const QuicStreamFrame& frame);
48
49   // Once data is buffered, it's up to the stream to read it when the stream
50   // can handle more data.  The following three functions make that possible.
51
52   // Fills in up to iov_len iovecs with the next readable regions.  Returns the
53   // number of iovs used.  Non-destructive of the underlying data.
54   int GetReadableRegions(iovec* iov, size_t iov_len);
55
56   // Copies the data into the iov_len buffers provided.  Returns the number of
57   // bytes read.  Any buffered data no longer in use will be released.
58   int Readv(const struct iovec* iov, size_t iov_len);
59
60   // Consumes |num_bytes| data.  Used in conjunction with |GetReadableRegions|
61   // to do zero-copy reads.
62   void MarkConsumed(size_t num_bytes);
63
64   // Returns true if the sequncer has bytes available for reading.
65   bool HasBytesToRead() const;
66
67   // Returns true if the sequencer has delivered the fin.
68   bool IsClosed() const;
69
70   // Returns true if the sequencer has received this frame before.
71   bool IsDuplicate(const QuicStreamFrame& frame) const;
72
73   // Calls |ProcessRawData| on |stream_| for each buffered frame that may
74   // be processed.
75   void FlushBufferedFrames();
76
77   // Blocks processing of frames until |FlushBufferedFrames| is called.
78   void SetBlockedUntilFlush();
79
80   size_t num_bytes_buffered() const {
81     return num_bytes_buffered_;
82   }
83
84  private:
85   friend class test::QuicStreamSequencerPeer;
86
87   // Wait until we've seen 'offset' bytes, and then terminate the stream.
88   void CloseStreamAtOffset(QuicStreamOffset offset);
89
90   // If we've received a FIN and have processed all remaining data, then inform
91   // the stream of FIN, and clear buffers.
92   bool MaybeCloseStream();
93
94   // Called whenever bytes are consumed by the stream. Updates
95   // num_bytes_consumed_ and num_bytes_buffered_.
96   void RecordBytesConsumed(size_t bytes_consumed);
97
98   // The stream which owns this sequencer.
99   ReliableQuicStream* stream_;
100
101   // The last data consumed by the stream.
102   QuicStreamOffset num_bytes_consumed_;
103
104   // TODO(alyssar) use something better than strings.
105   typedef map<QuicStreamOffset, string> FrameMap;
106
107   // Stores buffered frames (maps from sequence number -> frame data as string).
108   FrameMap frames_;
109
110   // The maximum memory the sequencer can buffer.
111   size_t max_frame_memory_;
112
113   // The offset, if any, we got a stream termination for.  When this many bytes
114   // have been processed, the sequencer will be closed.
115   QuicStreamOffset close_offset_;
116
117   // If true, the sequencer is blocked from passing data to the stream and will
118   // buffer all new incoming data until FlushBufferedFrames is called.
119   bool blocked_;
120
121   // Tracks how many bytes the sequencer has buffered.
122   size_t num_bytes_buffered_;
123 };
124
125 }  // namespace net
126
127 #endif  // NET_QUIC_QUIC_STREAM_SEQUENCER_H_