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 // The base class for client/server reliable streams. | 5 // The base class for client/server reliable streams. |
6 | 6 |
7 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_ | 7 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_ |
8 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_ | 8 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_ |
9 | 9 |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include <list> | 12 #include <list> |
13 | 13 |
14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
15 #include "net/base/iovec.h" | 15 #include "net/base/iovec.h" |
16 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
17 #include "net/quic/quic_ack_notifier.h" | 17 #include "net/quic/quic_ack_notifier.h" |
| 18 #include "net/quic/quic_protocol.h" |
18 #include "net/quic/quic_spdy_compressor.h" | 19 #include "net/quic/quic_spdy_compressor.h" |
19 #include "net/quic/quic_spdy_decompressor.h" | |
20 #include "net/quic/quic_stream_sequencer.h" | 20 #include "net/quic/quic_stream_sequencer.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 namespace test { | 24 namespace test { |
25 class ReliableQuicStreamPeer; | 25 class ReliableQuicStreamPeer; |
26 } // namespace test | 26 } // namespace test |
27 | 27 |
28 class IPEndPoint; | 28 class IPEndPoint; |
29 class QuicSession; | 29 class QuicSession; |
30 class SSLInfo; | 30 class SSLInfo; |
31 | 31 |
32 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | 32 class NET_EXPORT_PRIVATE ReliableQuicStream { |
33 | |
34 // All this does right now is send data to subclasses via the sequencer. | |
35 class NET_EXPORT_PRIVATE ReliableQuicStream : public | |
36 QuicSpdyDecompressor::Visitor { | |
37 public: | 33 public: |
38 // Visitor receives callbacks from the stream. | |
39 class Visitor { | |
40 public: | |
41 Visitor() {} | |
42 | |
43 // Called when the stream is closed. | |
44 virtual void OnClose(ReliableQuicStream* stream) = 0; | |
45 | |
46 protected: | |
47 virtual ~Visitor() {} | |
48 | |
49 private: | |
50 DISALLOW_COPY_AND_ASSIGN(Visitor); | |
51 }; | |
52 | |
53 ReliableQuicStream(QuicStreamId id, | 34 ReliableQuicStream(QuicStreamId id, |
54 QuicSession* session); | 35 QuicSession* session); |
55 | 36 |
56 virtual ~ReliableQuicStream(); | 37 virtual ~ReliableQuicStream(); |
57 | 38 |
58 bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const; | 39 bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const; |
59 | 40 |
60 // Called when a (potentially duplicate) stream frame has been received | 41 // Called when a (potentially duplicate) stream frame has been received |
61 // for this stream. Returns false if this frame can not be accepted | 42 // for this stream. Returns false if this frame can not be accepted |
62 // because there is too much data already buffered. | 43 // because there is too much data already buffered. |
(...skipping 10 matching lines...) Expand all Loading... |
73 virtual void OnStreamReset(QuicRstStreamErrorCode error); | 54 virtual void OnStreamReset(QuicRstStreamErrorCode error); |
74 | 55 |
75 // Called when we get or send a connection close, and should immediately | 56 // Called when we get or send a connection close, and should immediately |
76 // close the stream. This is not passed through the sequencer, | 57 // close the stream. This is not passed through the sequencer, |
77 // but is handled immediately. | 58 // but is handled immediately. |
78 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer); | 59 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer); |
79 | 60 |
80 // Called when the final data has been read. | 61 // Called when the final data has been read. |
81 virtual void OnFinRead(); | 62 virtual void OnFinRead(); |
82 | 63 |
83 virtual uint32 ProcessRawData(const char* data, uint32 data_len); | 64 virtual uint32 ProcessRawData(const char* data, uint32 data_len) = 0; |
84 | |
85 virtual uint32 ProcessData(const char* data, uint32 data_len) = 0; | |
86 | |
87 virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE; | |
88 virtual void OnDecompressionError() OVERRIDE; | |
89 | 65 |
90 // Called to reset the stream from this end. | 66 // Called to reset the stream from this end. |
91 virtual void Reset(QuicRstStreamErrorCode error); | 67 virtual void Reset(QuicRstStreamErrorCode error); |
92 | 68 |
93 // Called to close the entire connection from this end. | 69 // Called to close the entire connection from this end. |
94 virtual void CloseConnection(QuicErrorCode error); | 70 virtual void CloseConnection(QuicErrorCode error); |
95 virtual void CloseConnectionWithDetails(QuicErrorCode error, | 71 virtual void CloseConnectionWithDetails(QuicErrorCode error, |
96 const string& details); | 72 const string& details); |
97 | 73 |
98 // This block of functions wraps the sequencer's functions of the same | 74 // Returns the effective priority for the stream. This value may change |
99 // name. These methods return uncompressed data until that has | 75 // during the life of the stream. |
100 // been fully processed. Then they simply delegate to the sequencer. | 76 virtual QuicPriority EffectivePriority() const = 0; |
101 virtual size_t Readv(const struct iovec* iov, size_t iov_len); | |
102 virtual int GetReadableRegions(iovec* iov, size_t iov_len); | |
103 // Returns true when all data has been read from the peer, including the fin. | |
104 virtual bool IsDoneReading() const; | |
105 virtual bool HasBytesToRead() const; | |
106 | |
107 // Called by the session when a decompression blocked stream | |
108 // becomes unblocked. | |
109 virtual void OnDecompressorAvailable(); | |
110 | |
111 // By default, this is the same as priority(), however it allows streams | |
112 // to temporarily alter effective priority. For example if a SPDY stream has | |
113 // compressed but not written headers it can write the headers with a higher | |
114 // priority. | |
115 virtual QuicPriority EffectivePriority() const; | |
116 | 77 |
117 QuicStreamId id() const { return id_; } | 78 QuicStreamId id() const { return id_; } |
118 | 79 |
119 QuicRstStreamErrorCode stream_error() const { return stream_error_; } | 80 QuicRstStreamErrorCode stream_error() const { return stream_error_; } |
120 QuicErrorCode connection_error() const { return connection_error_; } | 81 QuicErrorCode connection_error() const { return connection_error_; } |
121 | 82 |
122 bool read_side_closed() const { return read_side_closed_; } | 83 bool read_side_closed() const { return read_side_closed_; } |
123 bool write_side_closed() const { return write_side_closed_; } | 84 bool write_side_closed() const { return write_side_closed_; } |
124 | 85 |
125 uint64 stream_bytes_read() { return stream_bytes_read_; } | 86 uint64 stream_bytes_read() { return stream_bytes_read_; } |
126 uint64 stream_bytes_written() { return stream_bytes_written_; } | 87 uint64 stream_bytes_written() { return stream_bytes_written_; } |
127 | 88 |
128 const IPEndPoint& GetPeerAddress() const; | |
129 | |
130 void set_visitor(Visitor* visitor) { visitor_ = visitor; } | |
131 | |
132 QuicSpdyCompressor* compressor(); | |
133 | |
134 // Gets the SSL connection information. | |
135 bool GetSSLInfo(SSLInfo* ssl_info); | |
136 | |
137 bool headers_decompressed() const { return headers_decompressed_; } | |
138 | |
139 protected: | 89 protected: |
140 // Sends as much of 'data' to the connection as the connection will consume, | 90 // Sends as much of 'data' to the connection as the connection will consume, |
141 // and then buffers any remaining data in queued_data_. | 91 // and then buffers any remaining data in queued_data_. |
142 void WriteOrBufferData(base::StringPiece data, bool fin); | 92 void WriteOrBufferData(base::StringPiece data, bool fin); |
143 | 93 |
144 // Sends as many bytes in the first |count| buffers of |iov| to the connection | 94 // Sends as many bytes in the first |count| buffers of |iov| to the connection |
145 // as the connection will consume. | 95 // as the connection will consume. |
146 // If |ack_notifier_delegate| is provided, then it will be notified once all | 96 // If |ack_notifier_delegate| is provided, then it will be notified once all |
147 // the ACKs for this write have been received. | 97 // the ACKs for this write have been received. |
148 // Returns the number of bytes consumed by the connection. | 98 // Returns the number of bytes consumed by the connection. |
149 QuicConsumedData WritevData( | 99 QuicConsumedData WritevData( |
150 const struct iovec* iov, | 100 const struct iovec* iov, |
151 int iov_count, | 101 int iov_count, |
152 bool fin, | 102 bool fin, |
153 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); | 103 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); |
154 | 104 |
155 // Close the read side of the socket. Further frames will not be accepted. | 105 // Close the read side of the socket. Further frames will not be accepted. |
156 virtual void CloseReadSide(); | 106 virtual void CloseReadSide(); |
157 | 107 |
158 // Close the write side of the socket. Further writes will fail. | 108 // Close the write side of the socket. Further writes will fail. |
159 void CloseWriteSide(); | 109 void CloseWriteSide(); |
160 | 110 |
161 bool HasBufferedData(); | 111 bool HasBufferedData(); |
162 | 112 |
163 bool fin_buffered() { return fin_buffered_; } | 113 bool fin_buffered() { return fin_buffered_; } |
164 | 114 |
165 QuicSession* session() { return session_; } | 115 QuicSession* session() { return session_; } |
166 | 116 |
167 // Sets priority_ to priority. This should only be called before bytes are | 117 const QuicStreamSequencer* sequencer() const { return &sequencer_; } |
168 // written to the server. | 118 QuicStreamSequencer* sequencer() { return &sequencer_; } |
169 void set_priority(QuicPriority priority); | |
170 // This is protected because external classes should use EffectivePriority | |
171 // instead. | |
172 QuicPriority priority() const { return priority_; } | |
173 | 119 |
174 private: | 120 private: |
175 friend class test::ReliableQuicStreamPeer; | 121 friend class test::ReliableQuicStreamPeer; |
176 friend class QuicStreamUtils; | 122 friend class QuicStreamUtils; |
177 | 123 |
178 uint32 ProcessHeaderData(); | |
179 | |
180 uint32 StripPriorityAndHeaderId(const char* data, uint32 data_len); | |
181 | |
182 std::list<string> queued_data_; | 124 std::list<string> queued_data_; |
183 | 125 |
184 QuicStreamSequencer sequencer_; | 126 QuicStreamSequencer sequencer_; |
185 QuicStreamId id_; | 127 QuicStreamId id_; |
186 QuicSession* session_; | 128 QuicSession* session_; |
187 // Optional visitor of this stream to be notified when the stream is closed. | |
188 Visitor* visitor_; | |
189 // Bytes read and written refer to payload bytes only: they do not include | 129 // Bytes read and written refer to payload bytes only: they do not include |
190 // framing, encryption overhead etc. | 130 // framing, encryption overhead etc. |
191 uint64 stream_bytes_read_; | 131 uint64 stream_bytes_read_; |
192 uint64 stream_bytes_written_; | 132 uint64 stream_bytes_written_; |
193 // True if the headers have been completely decompresssed. | |
194 bool headers_decompressed_; | |
195 // The priority of the stream, once parsed. | |
196 QuicPriority priority_; | |
197 // ID of the header block sent by the peer, once parsed. | |
198 QuicHeaderId headers_id_; | |
199 // Buffer into which we write bytes from priority_ and headers_id_ | |
200 // until each is fully parsed. | |
201 string headers_id_and_priority_buffer_; | |
202 // Contains a copy of the decompressed headers_ until they are consumed | |
203 // via ProcessData or Readv. | |
204 string decompressed_headers_; | |
205 // True if an error was encountered during decompression. | |
206 bool decompression_failed_; | |
207 | 133 |
208 // Stream error code received from a RstStreamFrame or error code sent by the | 134 // Stream error code received from a RstStreamFrame or error code sent by the |
209 // visitor or sequencer in the RstStreamFrame. | 135 // visitor or sequencer in the RstStreamFrame. |
210 QuicRstStreamErrorCode stream_error_; | 136 QuicRstStreamErrorCode stream_error_; |
211 // Connection error code due to which the stream was closed. |stream_error_| | 137 // Connection error code due to which the stream was closed. |stream_error_| |
212 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers | 138 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers |
213 // should check |connection_error_|. | 139 // should check |connection_error_|. |
214 QuicErrorCode connection_error_; | 140 QuicErrorCode connection_error_; |
215 | 141 |
216 // True if the read side is closed and further frames should be rejected. | 142 // True if the read side is closed and further frames should be rejected. |
217 bool read_side_closed_; | 143 bool read_side_closed_; |
218 // True if the write side is closed, and further writes should fail. | 144 // True if the write side is closed, and further writes should fail. |
219 bool write_side_closed_; | 145 bool write_side_closed_; |
220 | 146 |
221 // True if the priority has been read, false otherwise. | |
222 bool priority_parsed_; | |
223 bool fin_buffered_; | 147 bool fin_buffered_; |
224 bool fin_sent_; | 148 bool fin_sent_; |
225 | 149 |
226 // True if the session this stream is running under is a server session. | 150 // True if the session this stream is running under is a server session. |
227 bool is_server_; | 151 bool is_server_; |
| 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream); |
228 }; | 154 }; |
229 | 155 |
230 } // namespace net | 156 } // namespace net |
231 | 157 |
232 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ | 158 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ |
OLD | NEW |