reliable_quic_stream.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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; 29 30// All this does right now is send data to subclasses via the sequencer. 31class NET_EXPORT_PRIVATE ReliableQuicStream : public 32 QuicSpdyDecompressor::Visitor { 33 public: 34 // Visitor receives callbacks from the stream. 35 class Visitor { 36 public: 37 Visitor() {} 38 39 // Called when the stream is closed. 40 virtual void OnClose(ReliableQuicStream* stream) = 0; 41 42 protected: 43 virtual ~Visitor() {} 44 45 private: 46 DISALLOW_COPY_AND_ASSIGN(Visitor); 47 }; 48 49 ReliableQuicStream(QuicStreamId id, 50 QuicSession* session); 51 52 virtual ~ReliableQuicStream(); 53 54 bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const; 55 virtual bool OnStreamFrame(const QuicStreamFrame& frame); 56 57 virtual void OnCanWrite(); 58 59 // Called by the session just before the stream is deleted. 60 virtual void OnClose(); 61 62 // Called when we get a stream reset from the client. 63 virtual void OnStreamReset(QuicRstStreamErrorCode error); 64 65 // Called when we get or send a connection close, and should immediately 66 // close the stream. This is not passed through the sequencer, 67 // but is handled immediately. 68 virtual void ConnectionClose(QuicErrorCode error, bool from_peer); 69 70 // Called when we should process a stream termination or 71 // stream close from the peer. 72 virtual void TerminateFromPeer(bool half_close); 73 74 virtual uint32 ProcessRawData(const char* data, uint32 data_len); 75 virtual uint32 ProcessHeaderData(); 76 77 virtual uint32 ProcessData(const char* data, uint32 data_len) = 0; 78 79 virtual bool OnDecompressedData(base::StringPiece data) OVERRIDE; 80 virtual void OnDecompressionError() OVERRIDE; 81 82 // Called to close the stream from this end. 83 virtual void Close(QuicRstStreamErrorCode error); 84 85 // This block of functions wraps the sequencer's functions of the same 86 // name. These methods return uncompressed data until that has 87 // been fully processed. Then they simply delegate to the sequencer. 88 virtual size_t Readv(const struct iovec* iov, size_t iov_len); 89 virtual int GetReadableRegions(iovec* iov, size_t iov_len); 90 virtual bool IsHalfClosed() const; 91 virtual bool HasBytesToRead() const; 92 93 // Called by the session when a decompression blocked stream 94 // becomes unblocked. 95 virtual void OnDecompressorAvailable(); 96 97 QuicStreamId id() const { return id_; } 98 99 QuicRstStreamErrorCode stream_error() const { return stream_error_; } 100 QuicErrorCode connection_error() const { return connection_error_; } 101 102 bool read_side_closed() const { return read_side_closed_; } 103 bool write_side_closed() const { return write_side_closed_; } 104 105 uint64 stream_bytes_read() { return stream_bytes_read_; } 106 uint64 stream_bytes_written() { return stream_bytes_written_; } 107 108 const IPEndPoint& GetPeerAddress() const; 109 110 Visitor* visitor() { return visitor_; } 111 void set_visitor(Visitor* visitor) { visitor_ = visitor; } 112 113 QuicSpdyCompressor* compressor(); 114 115 protected: 116 // Returns a pair with the number of bytes consumed from data, and a boolean 117 // indicating if the fin bit was consumed. This does not indicate the data 118 // has been sent on the wire: it may have been turned into a packet and queued 119 // if the socket was unexpectedly blocked. 120 // 121 // The default implementation always consumed all bytes and any fin, but 122 // this behavior is not guaranteed for subclasses so callers should check the 123 // return value. 124 virtual QuicConsumedData WriteData(base::StringPiece data, bool fin); 125 126 // Close the read side of the socket. Further frames will not be accepted. 127 virtual void CloseReadSide(); 128 129 // Close the write side of the socket. Further writes will fail. 130 void CloseWriteSide(); 131 132 bool fin_buffered() { return fin_buffered_; } 133 134 QuicSession* session() { return session_; } 135 136 // Sends as much of 'data' to the connection as the connection will consume, 137 // and then buffers any remaining data in queued_data_. 138 // Returns (data.size(), true) as it always consumed all data: it returns for 139 // convenience to have the same return type as WriteDataInternal. 140 QuicConsumedData WriteOrBuffer(base::StringPiece data, bool fin); 141 142 // Sends as much of 'data' to the connection as the connection will consume. 143 // Returns the number of bytes consumed by the connection. 144 QuicConsumedData WriteDataInternal(base::StringPiece data, bool fin); 145 146 private: 147 friend class test::ReliableQuicStreamPeer; 148 friend class QuicStreamUtils; 149 150 std::list<string> queued_data_; 151 152 QuicStreamSequencer sequencer_; 153 QuicStreamId id_; 154 QuicSession* session_; 155 // Optional visitor of this stream to be notified when the stream is closed. 156 Visitor* visitor_; 157 // Bytes read and written refer to payload bytes only: they do not include 158 // framing, encryption overhead etc. 159 uint64 stream_bytes_read_; 160 uint64 stream_bytes_written_; 161 // True if the headers have been completely decompresssed. 162 bool headers_decompressed_; 163 // ID of the header block sent by the peer, once parsed. 164 QuicHeaderId headers_id_; 165 // Buffer into which we write bytes from the headers_id_ 166 // until it is fully parsed. 167 string headers_id_buffer_; 168 // Contains a copy of the decompressed headers_ until they are consumed 169 // via ProcessData or Readv. 170 string decompressed_headers_; 171 172 // Stream error code received from a RstStreamFrame or error code sent by the 173 // visitor or sequencer in the RstStreamFrame. 174 QuicRstStreamErrorCode stream_error_; 175 // Connection error code due to which the stream was closed. |stream_error_| 176 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers 177 // should check |connection_error_|. 178 QuicErrorCode connection_error_; 179 180 // True if the read side is closed and further frames should be rejected. 181 bool read_side_closed_; 182 // True if the write side is closed, and further writes should fail. 183 bool write_side_closed_; 184 185 bool fin_buffered_; 186 bool fin_sent_; 187}; 188 189} // namespace net 190 191#endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_ 192