15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The base class for streams which deliver data to/from an application.
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// In each direction, the data on such a stream first contains compressed
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// headers then body data.
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef NET_QUIC_QUIC_DATA_STREAM_H_
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define NET_QUIC_QUIC_DATA_STREAM_H_
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/types.h>
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <list>
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_piece.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/iovec.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/net_export.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_ack_notifier.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_protocol.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/quic_stream_sequencer.h"
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/quic/reliable_quic_stream.h"
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/spdy/spdy_framer.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace net {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace test {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class QuicDataStreamPeer;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ReliableQuicStreamPeer;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace test
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class IPEndPoint;
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class QuicSession;
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class SSLInfo;
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// All this does right now is send data to subclasses via the sequencer.
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NET_EXPORT_PRIVATE QuicDataStream : public ReliableQuicStream {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Visitor receives callbacks from the stream.
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class Visitor {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   public:
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Visitor() {}
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Called when the stream is closed.
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual void OnClose(QuicDataStream* stream) = 0;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   protected:
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual ~Visitor() {}
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   private:
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(Visitor);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicDataStream(QuicStreamId id, QuicSession* session);
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~QuicDataStream();
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ReliableQuicStream implementation
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnClose() OVERRIDE;
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual uint32 ProcessRawData(const char* data, uint32 data_len) OVERRIDE;
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // By default, this is the same as priority(), however it allows streams
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // to temporarily alter effective priority.   For example if a SPDY stream has
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // compressed but not written headers it can write the headers with a higher
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // priority.
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual QuicPriority EffectivePriority() const OVERRIDE;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Overridden by subclasses to process data.  The headers will be delivered
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // via OnStreamHeaders, so only data will be delivered through this method.
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual uint32 ProcessData(const char* data, uint32 data_len) = 0;
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the session when decompressed headers data is received
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // for this stream.
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // May be called multiple times, with each call providing additional headers
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // data until OnStreamHeadersComplete is called.
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStreamHeaders(base::StringPiece headers_data);
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the session when headers with a priority have been received
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for this stream.  This method will only be called for server streams.
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStreamHeadersPriority(QuicPriority priority);
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Called by the session when decompressed headers have been completely
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // delilvered to this stream.  If |fin| is true, then this stream
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // should be closed; no more data will be sent by the peer.
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStreamHeadersComplete(bool fin, size_t frame_len);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Writes the headers contained in |header_block| to the dedicated
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // headers stream.
8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  virtual size_t WriteHeaders(
9023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      const SpdyHeaderBlock& header_block,
9123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      bool fin,
9223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      QuicAckNotifier::DelegateInterface* ack_notifier_delegate);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This block of functions wraps the sequencer's functions of the same
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // name.  These methods return uncompressed data until that has
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // been fully processed.  Then they simply delegate to the sequencer.
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual size_t Readv(const struct iovec* iov, size_t iov_len);
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual int GetReadableRegions(iovec* iov, size_t iov_len);
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns true when all data has been read from the peer, including the fin.
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsDoneReading() const;
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool HasBytesToRead() const;
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void set_visitor(Visitor* visitor) { visitor_ = visitor; }
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool headers_decompressed() const { return headers_decompressed_; }
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const IPEndPoint& GetPeerAddress();
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Gets the SSL connection information.
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool GetSSLInfo(SSLInfo* ssl_info);
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) protected:
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets priority_ to priority.  This should only be called before bytes are
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // written to the server.
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void set_priority(QuicPriority priority);
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This is protected because external classes should use EffectivePriority
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // instead.
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicPriority priority() const { return priority_; }
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend class test::QuicDataStreamPeer;
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend class test::ReliableQuicStreamPeer;
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend class QuicStreamUtils;
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 ProcessHeaderData();
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool FinishedReadingHeaders();
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Visitor* visitor_;
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // True if the headers have been completely decompresssed.
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool headers_decompressed_;
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The priority of the stream, once parsed.
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  QuicPriority priority_;
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Contains a copy of the decompressed headers until they are consumed
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // via ProcessData or Readv.
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  string decompressed_headers_;
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // True if an error was encountered during decompression.
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool decompression_failed_;
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // True if the priority has been read, false otherwise.
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool priority_parsed_;
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(QuicDataStream);
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace net
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // NET_QUIC_QUIC_DATA_STREAM_H_
148