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