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