reliable_quic_stream.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// found in the LICENSE file.
45f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// The base class for client/server reliable streams.
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define NET_QUIC_RELIABLE_QUIC_STREAM_H_
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <sys/types.h>
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <list>
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/quic/quic_stream_sequencer.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace net {
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace test {
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class ReliableQuicStreamPeer;
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace test
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class IPEndPoint;
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class QuicSession;
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// All this does right now is send data to subclasses via the sequencer.
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class NET_EXPORT_PRIVATE ReliableQuicStream {
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) public:
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Visitor receives callbacks from the stream.
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  class Visitor {
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)   public:
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    Visitor() {}
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Called when the stream is closed.
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    virtual void OnClose(ReliableQuicStream* stream) = 0;
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)   protected:
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    virtual ~Visitor() {}
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)   private:
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(Visitor);
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  };
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ReliableQuicStream(QuicStreamId id,
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     QuicSession* session);
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual ~ReliableQuicStream();
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool WillAcceptStreamFrame(const QuicStreamFrame& frame) const;
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual bool OnStreamFrame(const QuicStreamFrame& frame);
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void OnCanWrite();
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Called by the session just before the stream is deleted.
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void OnClose();
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Called when we get a stream reset from the client.
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void OnStreamReset(QuicErrorCode error);
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
59  // Called when we get or send a connection close, and should immediately
60  // close the stream.  This is not passed through the sequencer,
61  // but is handled immediately.
62  virtual void ConnectionClose(QuicErrorCode error, bool from_peer);
63
64  // Called by the sequencer, when we should process a stream termination or
65  // stream close from the peer.
66  virtual void TerminateFromPeer(bool half_close);
67
68  virtual uint32 ProcessData(const char* data, uint32 data_len) = 0;
69
70  // Called to close the stream from this end.
71  virtual void Close(QuicErrorCode error);
72
73  // This block of functions wraps the sequencer's functions of the same
74  // name.
75  virtual bool IsHalfClosed() const;
76  virtual bool HasBytesToRead() const;
77
78  QuicStreamId id() const { return id_; }
79
80  QuicErrorCode error() const { return error_; }
81
82  bool read_side_closed() const { return read_side_closed_; }
83  bool write_side_closed() const { return write_side_closed_; }
84
85  const IPEndPoint& GetPeerAddress() const;
86
87  Visitor* visitor() { return visitor_; }
88  void set_visitor(Visitor* visitor) { visitor_ = visitor; }
89
90 protected:
91  // Returns a pair with the number of bytes consumed from data, and a boolean
92  // indicating if the fin bit was consumed.  This does not indicate the data
93  // has been sent on the wire: it may have been turned into a packet and queued
94  // if the socket was unexpectedly blocked.
95  //
96  // The default implementation always consumed all bytes and any fin, but
97  // this behavior is not guaranteed for subclasses so callers should check the
98  // return value.
99  virtual QuicConsumedData WriteData(base::StringPiece data, bool fin);
100
101  // Close the read side of the socket.  Further frames will not be accepted.
102  virtual void CloseReadSide();
103
104  // Close the write side of the socket.  Further writes will fail.
105  void CloseWriteSide();
106
107  QuicSession* session() { return session_; }
108
109  // Sends as much of 'data' to the connection as the connection will consume,
110  // and then buffers any remaining data in queued_data_.
111  // Returns (data.size(), true) as it always consumed all data: it returns for
112  // convenience to have the same return type as WriteDataInternal.
113  QuicConsumedData WriteOrBuffer(base::StringPiece data, bool fin);
114
115  // Sends as much of 'data' to the connection as the connection will consume.
116  // Returns the number of bytes consumed by the connection.
117  QuicConsumedData WriteDataInternal(base::StringPiece data, bool fin);
118
119 private:
120  friend class test::ReliableQuicStreamPeer;
121  friend class QuicStreamUtils;
122
123  std::list<string> queued_data_;
124
125  QuicStreamSequencer sequencer_;
126  QuicStreamId id_;
127  QuicSession* session_;
128  // Optional visitor of this stream to be notified when the stream is closed.
129  Visitor* visitor_;
130  // Bytes read and written refer to payload bytes only: they do not include
131  // framing, encryption overhead etc.
132  uint64 stream_bytes_read_;
133  uint64 stream_bytes_written_;
134  QuicErrorCode error_;
135  // True if the read side is closed and further frames should be rejected.
136  bool read_side_closed_;
137  // True if the write side is closed, and further writes should fail.
138  bool write_side_closed_;
139
140  bool fin_buffered_;
141  bool fin_sent_;
142};
143
144}  // namespace net
145
146#endif  // NET_QUIC_RELIABLE_QUIC_STREAM_H_
147