1 // Copyright 2013 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 // The base class for streams which deliver data to/from an application.
6 // In each direction, the data on such a stream first contains compressed
7 // headers then body data.
9 #ifndef NET_QUIC_QUIC_DATA_STREAM_H_
10 #define NET_QUIC_QUIC_DATA_STREAM_H_
12 #include <sys/types.h>
16 #include "base/basictypes.h"
17 #include "base/strings/string_piece.h"
18 #include "net/base/iovec.h"
19 #include "net/base/net_export.h"
20 #include "net/quic/quic_ack_notifier.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_spdy_compressor.h"
23 #include "net/quic/quic_spdy_decompressor.h"
24 #include "net/quic/quic_stream_sequencer.h"
25 #include "net/quic/reliable_quic_stream.h"
30 class QuicDataStreamPeer
;
31 class ReliableQuicStreamPeer
;
38 // All this does right now is send data to subclasses via the sequencer.
39 class NET_EXPORT_PRIVATE QuicDataStream
: public ReliableQuicStream
,
40 public QuicSpdyDecompressor::Visitor
{
42 // Visitor receives callbacks from the stream.
47 // Called when the stream is closed.
48 virtual void OnClose(QuicDataStream
* stream
) = 0;
54 DISALLOW_COPY_AND_ASSIGN(Visitor
);
57 QuicDataStream(QuicStreamId id
, QuicSession
* session
);
59 virtual ~QuicDataStream();
61 // ReliableQuicStream implementation
62 virtual void OnClose() OVERRIDE
;
63 virtual uint32
ProcessRawData(const char* data
, uint32 data_len
) OVERRIDE
;
64 // By default, this is the same as priority(), however it allows streams
65 // to temporarily alter effective priority. For example if a SPDY stream has
66 // compressed but not written headers it can write the headers with a higher
68 virtual QuicPriority
EffectivePriority() const OVERRIDE
;
70 // QuicSpdyDecompressor::Visitor implementation.
71 virtual bool OnDecompressedData(base::StringPiece data
) OVERRIDE
;
72 virtual void OnDecompressionError() OVERRIDE
;
74 // Overridden by subclasses to process data. For QUIC_VERSION_12 or less,
75 // data will be delivered in order, first the decompressed headers, then
76 // the body. For later QUIC versions, the headers will be delivered via
77 // OnStreamHeaders, and only the data will be delivered through this method.
78 virtual uint32
ProcessData(const char* data
, uint32 data_len
) = 0;
80 // Called by the session when decompressed headers data is received
81 // for this stream. Only called for versions greater than QUIC_VERSION_12.
82 // May be called multiple times, with each call providing additional headers
83 // data until OnStreamHeadersComplete is called.
84 virtual void OnStreamHeaders(base::StringPiece headers_data
);
86 // Called by the session when headers with a priority have been received
87 // for this stream. This method will only be called for server streams.
88 virtual void OnStreamHeadersPriority(QuicPriority priority
);
90 // Called by the session when decompressed headers have been completely
91 // delilvered to this stream. If |fin| is true, then this stream
92 // should be closed; no more data will be sent by the peer.
93 // Only called for versions greater than QUIC_VERSION_12.
94 virtual void OnStreamHeadersComplete(bool fin
, size_t frame_len
);
96 // Writes the headers contained in |header_block| to the dedicated
98 virtual size_t WriteHeaders(const SpdyHeaderBlock
& header_block
,
101 // This block of functions wraps the sequencer's functions of the same
102 // name. These methods return uncompressed data until that has
103 // been fully processed. Then they simply delegate to the sequencer.
104 virtual size_t Readv(const struct iovec
* iov
, size_t iov_len
);
105 virtual int GetReadableRegions(iovec
* iov
, size_t iov_len
);
106 // Returns true when all data has been read from the peer, including the fin.
107 virtual bool IsDoneReading() const;
108 virtual bool HasBytesToRead() const;
110 // Called by the session when a decompression blocked stream
111 // becomes unblocked.
112 virtual void OnDecompressorAvailable();
114 void set_visitor(Visitor
* visitor
) { visitor_
= visitor
; }
116 bool headers_decompressed() const { return headers_decompressed_
; }
118 const IPEndPoint
& GetPeerAddress();
120 QuicSpdyCompressor
* compressor();
122 // Gets the SSL connection information.
123 bool GetSSLInfo(SSLInfo
* ssl_info
);
126 // Sets priority_ to priority. This should only be called before bytes are
127 // written to the server.
128 void set_priority(QuicPriority priority
);
129 // This is protected because external classes should use EffectivePriority
131 QuicPriority
priority() const { return priority_
; }
134 friend class test::QuicDataStreamPeer
;
135 friend class test::ReliableQuicStreamPeer
;
136 friend class QuicStreamUtils
;
138 // Processes raw stream data for QUIC_VERSION_12 and earlier.
139 uint32
ProcessRawData12(const char* data
, uint32 data_len
);
141 uint32
ProcessHeaderData();
143 uint32
StripPriorityAndHeaderId(const char* data
, uint32 data_len
);
145 bool FinishedReadingHeaders();
148 // True if the headers have been completely decompresssed.
149 bool headers_decompressed_
;
150 // The priority of the stream, once parsed.
151 QuicPriority priority_
;
152 // ID of the header block sent by the peer, once parsed.
153 QuicHeaderId headers_id_
;
154 // Buffer into which we write bytes from priority_ and headers_id_
155 // until each is fully parsed.
156 string headers_id_and_priority_buffer_
;
157 // Contains a copy of the decompressed headers until they are consumed
158 // via ProcessData or Readv.
159 string decompressed_headers_
;
160 // True if an error was encountered during decompression.
161 bool decompression_failed_
;
162 // True if the priority has been read, false otherwise.
163 bool priority_parsed_
;
165 DISALLOW_COPY_AND_ASSIGN(QuicDataStream
);
170 #endif // NET_QUIC_QUIC_DATA_STREAM_H_