1// Copyright 2013 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 streams which deliver data to/from an application.
6// In each direction, the data on such a stream first contains compressed
7// headers then body data.
8
9#ifndef NET_QUIC_QUIC_DATA_STREAM_H_
10#define NET_QUIC_QUIC_DATA_STREAM_H_
11
12#include <sys/types.h>
13
14#include <list>
15
16#include "base/basictypes.h"
17#include "base/strings/string_piece.h"
18#include "net/base/iovec.h"
19#include "net/base/net_export.h"
20#include "net/quic/quic_ack_notifier.h"
21#include "net/quic/quic_protocol.h"
22#include "net/quic/quic_stream_sequencer.h"
23#include "net/quic/reliable_quic_stream.h"
24#include "net/spdy/spdy_framer.h"
25
26namespace net {
27
28namespace test {
29class QuicDataStreamPeer;
30class ReliableQuicStreamPeer;
31}  // namespace test
32
33class IPEndPoint;
34class QuicSession;
35class SSLInfo;
36
37// All this does right now is send data to subclasses via the sequencer.
38class NET_EXPORT_PRIVATE QuicDataStream : public ReliableQuicStream {
39 public:
40  // Visitor receives callbacks from the stream.
41  class Visitor {
42   public:
43    Visitor() {}
44
45    // Called when the stream is closed.
46    virtual void OnClose(QuicDataStream* stream) = 0;
47
48   protected:
49    virtual ~Visitor() {}
50
51   private:
52    DISALLOW_COPY_AND_ASSIGN(Visitor);
53  };
54
55  QuicDataStream(QuicStreamId id, QuicSession* session);
56
57  virtual ~QuicDataStream();
58
59  // ReliableQuicStream implementation
60  virtual void OnClose() OVERRIDE;
61  virtual uint32 ProcessRawData(const char* data, uint32 data_len) OVERRIDE;
62  // By default, this is the same as priority(), however it allows streams
63  // to temporarily alter effective priority.   For example if a SPDY stream has
64  // compressed but not written headers it can write the headers with a higher
65  // priority.
66  virtual QuicPriority EffectivePriority() const OVERRIDE;
67
68  // Overridden by subclasses to process data.  The headers will be delivered
69  // via OnStreamHeaders, so only data will be delivered through this method.
70  virtual uint32 ProcessData(const char* data, uint32 data_len) = 0;
71
72  // Called by the session when decompressed headers data is received
73  // for this stream.
74  // May be called multiple times, with each call providing additional headers
75  // data until OnStreamHeadersComplete is called.
76  virtual void OnStreamHeaders(base::StringPiece headers_data);
77
78  // Called by the session when headers with a priority have been received
79  // for this stream.  This method will only be called for server streams.
80  virtual void OnStreamHeadersPriority(QuicPriority priority);
81
82  // Called by the session when decompressed headers have been completely
83  // delilvered to this stream.  If |fin| is true, then this stream
84  // should be closed; no more data will be sent by the peer.
85  virtual void OnStreamHeadersComplete(bool fin, size_t frame_len);
86
87  // Writes the headers contained in |header_block| to the dedicated
88  // headers stream.
89  virtual size_t WriteHeaders(
90      const SpdyHeaderBlock& header_block,
91      bool fin,
92      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
93
94  // This block of functions wraps the sequencer's functions of the same
95  // name.  These methods return uncompressed data until that has
96  // been fully processed.  Then they simply delegate to the sequencer.
97  virtual size_t Readv(const struct iovec* iov, size_t iov_len);
98  virtual int GetReadableRegions(iovec* iov, size_t iov_len);
99  // Returns true when all data has been read from the peer, including the fin.
100  virtual bool IsDoneReading() const;
101  virtual bool HasBytesToRead() const;
102
103  void set_visitor(Visitor* visitor) { visitor_ = visitor; }
104
105  bool headers_decompressed() const { return headers_decompressed_; }
106
107  const IPEndPoint& GetPeerAddress();
108
109  // Gets the SSL connection information.
110  bool GetSSLInfo(SSLInfo* ssl_info);
111
112 protected:
113  // Sets priority_ to priority.  This should only be called before bytes are
114  // written to the server.
115  void set_priority(QuicPriority priority);
116  // This is protected because external classes should use EffectivePriority
117  // instead.
118  QuicPriority priority() const { return priority_; }
119
120 private:
121  friend class test::QuicDataStreamPeer;
122  friend class test::ReliableQuicStreamPeer;
123  friend class QuicStreamUtils;
124
125  uint32 ProcessHeaderData();
126
127  bool FinishedReadingHeaders();
128
129  Visitor* visitor_;
130  // True if the headers have been completely decompresssed.
131  bool headers_decompressed_;
132  // The priority of the stream, once parsed.
133  QuicPriority priority_;
134  // Contains a copy of the decompressed headers until they are consumed
135  // via ProcessData or Readv.
136  string decompressed_headers_;
137  // True if an error was encountered during decompression.
138  bool decompression_failed_;
139  // True if the priority has been read, false otherwise.
140  bool priority_parsed_;
141
142  DISALLOW_COPY_AND_ASSIGN(QuicDataStream);
143};
144
145}  // namespace net
146
147#endif  // NET_QUIC_QUIC_DATA_STREAM_H_
148