reliable_quic_stream.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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_ack_notifier.h" 18#include "net/quic/quic_spdy_compressor.h" 19#include "net/quic/quic_spdy_decompressor.h" 20#include "net/quic/quic_stream_sequencer.h" 21 22namespace net { 23 24namespace test { 25class ReliableQuicStreamPeer; 26} // namespace test 27 28class IPEndPoint; 29class QuicSession; 30class SSLInfo; 31 32#define ENDPOINT (is_server_ ? "Server: " : " Client: ") 33 34// All this does right now is send data to subclasses via the sequencer. 35class NET_EXPORT_PRIVATE ReliableQuicStream : public 36 QuicSpdyDecompressor::Visitor { 37 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, 54 QuicSession* session); 55 56 virtual ~ReliableQuicStream(); 57 58 bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const; 59 60 // Called when a (potentially duplicate) stream frame has been received 61 // for this stream. Returns false if this frame can not be accepted 62 // because there is too much data already buffered. 63 virtual bool OnStreamFrame(const QuicStreamFrame& frame); 64 65 // Called when the connection becomes writeable to allow the stream 66 // to write any pending data. 67 virtual void OnCanWrite(); 68 69 // Called by the session just before the stream is deleted. 70 virtual void OnClose(); 71 72 // Called when we get a stream reset from the peer. 73 virtual void OnStreamReset(QuicRstStreamErrorCode error); 74 75 // Called when we get or send a connection close, and should immediately 76 // close the stream. This is not passed through the sequencer, 77 // but is handled immediately. 78 virtual void OnConnectionClosed(QuicErrorCode error, bool from_peer); 79 80 // Called when the final data has been read. 81 virtual void OnFinRead(); 82 83 virtual uint32 ProcessRawData(const char* data, uint32 data_len); 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 90 // Called to reset the stream from this end. 91 virtual void Reset(QuicRstStreamErrorCode error); 92 93 // Called to close the entire connection from this end. 94 virtual void CloseConnection(QuicErrorCode error); 95 virtual void CloseConnectionWithDetails(QuicErrorCode error, 96 const string& details); 97 98 // This block of functions wraps the sequencer's functions of the same 99 // name. These methods return uncompressed data until that has 100 // been fully processed. Then they simply delegate to the sequencer. 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 117 QuicStreamId id() const { return id_; } 118 119 QuicRstStreamErrorCode stream_error() const { return stream_error_; } 120 QuicErrorCode connection_error() const { return connection_error_; } 121 122 bool read_side_closed() const { return read_side_closed_; } 123 bool write_side_closed() const { return write_side_closed_; } 124 125 uint64 stream_bytes_read() { return stream_bytes_read_; } 126 uint64 stream_bytes_written() { return stream_bytes_written_; } 127 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: 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 150 // Close the read side of the socket. Further frames will not be accepted. 151 virtual void CloseReadSide(); 152 153 // Close the write side of the socket. Further writes will fail. 154 void CloseWriteSide(); 155 156 bool HasBufferedData(); 157 158 bool fin_buffered() { return fin_buffered_; } 159 160 QuicSession* session() { return session_; } 161 162 // Sets priority_ to priority. This should only be called before bytes are 163 // written to the server. 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 190 private: 191 friend class test::ReliableQuicStreamPeer; 192 friend class QuicStreamUtils; 193 194 uint32 ProcessHeaderData(); 195 196 uint32 StripPriorityAndHeaderId(const char* data, uint32 data_len); 197 198 std::list<string> queued_data_; 199 200 QuicStreamSequencer sequencer_; 201 QuicStreamId id_; 202 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 206 // framing, encryption overhead etc. 207 uint64 stream_bytes_read_; 208 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 224 // Stream error code received from a RstStreamFrame or error code sent by the 225 // visitor or sequencer in the RstStreamFrame. 226 QuicRstStreamErrorCode stream_error_; 227 // 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 229 // should check |connection_error_|. 230 QuicErrorCode connection_error_; 231 232 // True if the read side is closed and further frames should be rejected. 233 bool read_side_closed_; 234 // True if the write side is closed, and further writes should fail. 235 bool write_side_closed_; 236 237 // True if the priority has been read, false otherwise. 238 bool priority_parsed_; 239 bool fin_buffered_; 240 bool fin_sent_; 241 242 // True if the session this stream is running under is a server session. 243 bool is_server_; 244}; 245 246} // namespace net 247 248#endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ 249