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