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.
5 // The base class for client/server reliable streams.
7 #ifndef NET_QUIC_RELIABLE_QUIC_STREAM_H_
8 #define NET_QUIC_RELIABLE_QUIC_STREAM_H_
10 #include <sys/types.h>
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_ack_notifier.h"
18 #include "net/quic/quic_protocol.h"
19 #include "net/quic/quic_stream_sequencer.h"
24 class ReliableQuicStreamPeer
;
31 class NET_EXPORT_PRIVATE ReliableQuicStream
{
33 ReliableQuicStream(QuicStreamId id
,
34 QuicSession
* session
);
36 virtual ~ReliableQuicStream();
38 bool WillAcceptStreamFrame(const QuicStreamFrame
& frame
) const;
40 // Called when a (potentially duplicate) stream frame has been received
41 // for this stream. Returns false if this frame can not be accepted
42 // because there is too much data already buffered.
43 virtual bool OnStreamFrame(const QuicStreamFrame
& frame
);
45 // Called when the connection becomes writeable to allow the stream
46 // to write any pending data.
47 virtual void OnCanWrite();
49 // Called by the session just before the stream is deleted.
50 virtual void OnClose();
52 // Called when we get a stream reset from the peer.
53 virtual void OnStreamReset(QuicRstStreamErrorCode error
);
55 // Called when we get or send a connection close, and should immediately
56 // close the stream. This is not passed through the sequencer,
57 // but is handled immediately.
58 virtual void OnConnectionClosed(QuicErrorCode error
, bool from_peer
);
60 // Called when the final data has been read.
61 virtual void OnFinRead();
63 virtual uint32
ProcessRawData(const char* data
, uint32 data_len
) = 0;
65 // Called to reset the stream from this end.
66 virtual void Reset(QuicRstStreamErrorCode error
);
68 // Called to close the entire connection from this end.
69 virtual void CloseConnection(QuicErrorCode error
);
70 virtual void CloseConnectionWithDetails(QuicErrorCode error
,
71 const string
& details
);
73 // Returns the effective priority for the stream. This value may change
74 // during the life of the stream.
75 virtual QuicPriority
EffectivePriority() const = 0;
77 QuicStreamId
id() const { return id_
; }
79 QuicRstStreamErrorCode
stream_error() const { return stream_error_
; }
80 QuicErrorCode
connection_error() const { return connection_error_
; }
82 bool read_side_closed() const { return read_side_closed_
; }
83 bool write_side_closed() const { return write_side_closed_
; }
85 uint64
stream_bytes_read() { return stream_bytes_read_
; }
86 uint64
stream_bytes_written() { return stream_bytes_written_
; }
88 QuicVersion
version();
91 // Sends as much of 'data' to the connection as the connection will consume,
92 // and then buffers any remaining data in queued_data_.
93 void WriteOrBufferData(base::StringPiece data
, bool fin
);
95 // Sends as many bytes in the first |count| buffers of |iov| to the connection
96 // as the connection will consume.
97 // If |ack_notifier_delegate| is provided, then it will be notified once all
98 // the ACKs for this write have been received.
99 // Returns the number of bytes consumed by the connection.
100 QuicConsumedData
WritevData(
101 const struct iovec
* iov
,
104 QuicAckNotifier::DelegateInterface
* ack_notifier_delegate
);
106 // Close the read side of the socket. Further frames will not be accepted.
107 virtual void CloseReadSide();
109 // Close the write side of the socket. Further writes will fail.
110 void CloseWriteSide();
112 bool HasBufferedData();
114 bool fin_buffered() { return fin_buffered_
; }
116 QuicSession
* session() { return session_
; }
118 const QuicStreamSequencer
* sequencer() const { return &sequencer_
; }
119 QuicStreamSequencer
* sequencer() { return &sequencer_
; }
122 friend class test::ReliableQuicStreamPeer
;
123 friend class QuicStreamUtils
;
125 std::list
<string
> queued_data_
;
127 QuicStreamSequencer sequencer_
;
129 QuicSession
* session_
;
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_
;
135 // Stream error code received from a RstStreamFrame or error code sent by the
136 // visitor or sequencer in the RstStreamFrame.
137 QuicRstStreamErrorCode stream_error_
;
138 // Connection error code due to which the stream was closed. |stream_error_|
139 // is set to |QUIC_STREAM_CONNECTION_ERROR| when this happens and consumers
140 // should check |connection_error_|.
141 QuicErrorCode connection_error_
;
143 // True if the read side is closed and further frames should be rejected.
144 bool read_side_closed_
;
145 // True if the write side is closed, and further writes should fail.
146 bool write_side_closed_
;
151 // True if the session this stream is running under is a server session.
154 DISALLOW_COPY_AND_ASSIGN(ReliableQuicStream
);
159 #endif // NET_QUIC_RELIABLE_QUIC_STREAM_H_