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 #include "net/quic/quic_reliable_client_stream.h"
7 #include "base/callback_helpers.h"
8 #include "net/base/net_errors.h"
9 #include "net/quic/quic_spdy_session.h"
10 #include "net/quic/quic_write_blocked_list.h"
14 QuicReliableClientStream::QuicReliableClientStream(QuicStreamId id
,
15 QuicSpdySession
* session
,
16 const BoundNetLog
& net_log
)
17 : QuicDataStream(id
, session
), net_log_(net_log
), delegate_(nullptr) {
20 QuicReliableClientStream::~QuicReliableClientStream() {
22 delegate_
->OnClose(connection_error());
25 void QuicReliableClientStream::OnStreamHeadersComplete(bool fin
,
27 QuicDataStream::OnStreamHeadersComplete(fin
, frame_len
);
29 delegate_
->OnHeadersAvailable(decompressed_headers());
30 MarkHeadersConsumed(decompressed_headers().length());
34 uint32
QuicReliableClientStream::ProcessData(const char* data
,
36 // TODO(rch): buffer data if we don't have a delegate.
38 DLOG(ERROR
) << "Missing delegate";
39 Reset(QUIC_STREAM_CANCELLED
);
43 if (!FinishedReadingHeaders()) {
44 // Buffer the data in the sequencer until the headers have been read.
48 int rv
= delegate_
->OnDataReceived(data
, data_len
);
50 DLOG(ERROR
) << "Delegate refused data, rv: " << rv
;
51 Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
57 void QuicReliableClientStream::OnClose() {
59 delegate_
->OnClose(connection_error());
62 ReliableQuicStream::OnClose();
65 void QuicReliableClientStream::OnCanWrite() {
66 ReliableQuicStream::OnCanWrite();
68 if (!HasBufferedData() && !callback_
.is_null()) {
69 base::ResetAndReturn(&callback_
).Run(OK
);
73 QuicPriority
QuicReliableClientStream::EffectivePriority() const {
74 if (delegate_
&& delegate_
->HasSendHeadersComplete()) {
75 return QuicDataStream::EffectivePriority();
77 return QuicWriteBlockedList::kHighestPriority
;
80 int QuicReliableClientStream::WriteStreamData(
81 base::StringPiece data
,
83 const CompletionCallback
& callback
) {
84 // We should not have data buffered.
85 DCHECK(!HasBufferedData());
86 // Writes the data, or buffers it.
87 WriteOrBufferData(data
, fin
, nullptr);
88 if (!HasBufferedData()) {
93 return ERR_IO_PENDING
;
96 void QuicReliableClientStream::SetDelegate(
97 QuicReliableClientStream::Delegate
* delegate
) {
98 DCHECK(!(delegate_
&& delegate
));
102 void QuicReliableClientStream::OnError(int error
) {
104 QuicReliableClientStream::Delegate
* delegate
= delegate_
;
106 delegate
->OnError(error
);
110 bool QuicReliableClientStream::CanWrite(const CompletionCallback
& callback
) {
111 bool can_write
= session()->connection()->CanWrite(HAS_RETRANSMITTABLE_DATA
);
113 session()->MarkConnectionLevelWriteBlocked(id(), EffectivePriority());
114 DCHECK(callback_
.is_null());
115 callback_
= callback
;