| 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; | 89 protected: |
| 90 // Sends as much of 'data' to the connection as the connection will consume, |
| 91 // and then buffers any remaining data in queued_data_. |
| 92 void WriteOrBufferData(base::StringPiece data, bool fin); |
| 129 | 93 |
| 130 void set_visitor(Visitor* visitor) { visitor_ = visitor; } | 94 // Sends as many bytes in the first |count| buffers of |iov| to the connection |
| 131 | 95 // as the connection will consume. |
| 132 QuicSpdyCompressor* compressor(); | 96 // If |ack_notifier_delegate| is provided, then it will be notified once all |
| 133 | 97 // the ACKs for this write have been received. |
| 134 // Gets the SSL connection information. | 98 // Returns the number of bytes consumed by the connection. |
| 135 bool GetSSLInfo(SSLInfo* ssl_info); | 99 QuicConsumedData WritevData( |
| 136 | 100 const struct iovec* iov, |
| 137 bool headers_decompressed() const { return headers_decompressed_; } | 101 int iov_count, |
| 138 | 102 bool fin, |
| 139 protected: | 103 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); |
| 140 // Returns a pair with the number of bytes consumed from data, and a boolean | |
| 141 // indicating if the fin bit was consumed. This does not indicate the data | |
| 142 // has been sent on the wire: it may have been turned into a packet and queued | |
| 143 // if the socket was unexpectedly blocked. | |
| 144 // | |
| 145 // The default implementation always consumed all bytes and any fin, but | |
| 146 // this behavior is not guaranteed for subclasses so callers should check the | |
| 147 // return value. | |
| 148 virtual QuicConsumedData WriteData(base::StringPiece data, bool fin); | |
| 149 | 104 |
| 150 // 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. |
| 151 virtual void CloseReadSide(); | 106 virtual void CloseReadSide(); |
| 152 | 107 |
| 153 // Close the write side of the socket. Further writes will fail. | 108 // Close the write side of the socket. Further writes will fail. |
| 154 void CloseWriteSide(); | 109 void CloseWriteSide(); |
| 155 | 110 |
| 156 bool HasBufferedData(); | 111 bool HasBufferedData(); |
| 157 | 112 |
| 158 bool fin_buffered() { return fin_buffered_; } | 113 bool fin_buffered() { return fin_buffered_; } |
| 159 | 114 |
| 160 QuicSession* session() { return session_; } | 115 QuicSession* session() { return session_; } |
| 161 | 116 |
| 162 // Sets priority_ to priority. This should only be called before bytes are | 117 const QuicStreamSequencer* sequencer() const { return &sequencer_; } |
| 163 // written to the server. | 118 QuicStreamSequencer* sequencer() { return &sequencer_; } |
| 164 void set_priority(QuicPriority priority); | |
| 165 // This is protected because external classes should use EffectivePriority | |
| 166 // instead. | |
| 167 QuicPriority priority() const { return priority_; } | |
| 168 | |
| 169 // Sends as much of 'data' to the connection as the connection will consume, | |
| 170 // and then buffers any remaining data in queued_data_. | |
| 171 // Returns (data.size(), true) as it always consumed all data: it returns for | |
| 172 // convenience to have the same return type as WriteDataInternal. | |
| 173 QuicConsumedData WriteOrBuffer(base::StringPiece data, bool fin); | |
| 174 | |
| 175 // Sends as much of 'data' to the connection as the connection will consume. | |
| 176 // Returns the number of bytes consumed by the connection. | |
| 177 QuicConsumedData WriteDataInternal(base::StringPiece data, bool fin); | |
| 178 | |
| 179 // Sends as many bytes in the first |count| buffers of |iov| to the connection | |
| 180 // as the connection will consume. | |
| 181 // If |ack_notifier_delegate| is provided, then it will be notified once all | |
| 182 // the ACKs for this write have been received. | |
| 183 // Returns the number of bytes consumed by the connection. | |
| 184 QuicConsumedData WritevDataInternal( | |
| 185 const struct iovec* iov, | |
| 186 int iov_count, | |
| 187 bool fin, | |
| 188 QuicAckNotifier::DelegateInterface* ack_notifier_delegate); | |
| 189 | 119 |
| 190 private: | 120 private: |
| 191 friend class test::ReliableQuicStreamPeer; | 121 friend class test::ReliableQuicStreamPeer; |
| 192 friend class QuicStreamUtils; | 122 friend class QuicStreamUtils; |
| 193 | 123 |
| 194 uint32 ProcessHeaderData(); | |
| 195 | |
| 196 uint32 StripPriorityAndHeaderId(const char* data, uint32 data_len); | |
| 197 | |
| 198 std::list<string> queued_data_; | 124 std::list<string> queued_data_; |
| 199 | 125 |
| 200 QuicStreamSequencer sequencer_; | 126 QuicStreamSequencer sequencer_; |
| 201 QuicStreamId id_; | 127 QuicStreamId id_; |
| 202 QuicSession* session_; | 128 QuicSession* session_; |
| 203 // Optional visitor of this stream to be notified when the stream is closed. | |
| 204 Visitor* visitor_; | |
| 205 // 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 |
| 206 // framing, encryption overhead etc. | 130 // framing, encryption overhead etc. |
| 207 uint64 stream_bytes_read_; | 131 uint64 stream_bytes_read_; |
| 208 uint64 stream_bytes_written_; | 132 uint64 stream_bytes_written_; |
| 209 // True if the headers have been completely decompresssed. | |
| 210 bool headers_decompressed_; | |
| 211 // The priority of the stream, once parsed. | |
| 212 QuicPriority priority_; | |
| 213 // ID of the header block sent by the peer, once parsed. | |
| 214 QuicHeaderId headers_id_; | |
| 215 // Buffer into which we write bytes from priority_ and headers_id_ | |
| 216 // until each is fully parsed. | |
| 217 string headers_id_and_priority_buffer_; | |
| 218 // Contains a copy of the decompressed headers_ until they are consumed | |
| 219 // via ProcessData or Readv. | |
| 220 string decompressed_headers_; | |
| 221 // True if an error was encountered during decompression. | |
| 222 bool decompression_failed_; | |
| 223 | 133 |
| 224 // 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 |
| 225 // visitor or sequencer in the RstStreamFrame. | 135 // visitor or sequencer in the RstStreamFrame. |
| 226 QuicRstStreamErrorCode stream_error_; | 136 QuicRstStreamErrorCode stream_error_; |
| 227 // 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_| |
| 228 // 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 |
| 229 // should check |connection_error_|. | 139 // should check |connection_error_|. |
| 230 QuicErrorCode connection_error_; | 140 QuicErrorCode connection_error_; |
| 231 | 141 |
| 232 // 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. |
| 233 bool read_side_closed_; | 143 bool read_side_closed_; |
| 234 // 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. |
| 235 bool write_side_closed_; | 145 bool write_side_closed_; |
| 236 | 146 |
| 237 // True if the priority has been read, false otherwise. | |
| 238 bool priority_parsed_; | |
| 239 bool fin_buffered_; | 147 bool fin_buffered_; |
| 240 bool fin_sent_; | 148 bool fin_sent_; |
| 241 | 149 |
| 242 // 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. |
| 243 bool is_server_; | 151 bool is_server_; |
| 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream); |
| 244 }; | 154 }; |
| 245 | 155 |
| 246 } // namespace net | 156 } // namespace net |
| 247 | 157 |
| 248 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ | 158 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ |
| OLD | NEW |