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_session.h"
10 #include "net/quic/quic_write_blocked_list.h"
14 QuicReliableClientStream::QuicReliableClientStream(QuicStreamId id
,
16 const BoundNetLog
& net_log
)
17 : QuicDataStream(id
, session
),
22 QuicReliableClientStream::~QuicReliableClientStream() {
24 delegate_
->OnClose(connection_error());
27 uint32
QuicReliableClientStream::ProcessData(const char* data
,
29 // TODO(rch): buffer data if we don't have a delegate.
31 DLOG(ERROR
) << "Missing delegate";
32 Reset(QUIC_STREAM_CANCELLED
);
36 int rv
= delegate_
->OnDataReceived(data
, data_len
);
38 DLOG(ERROR
) << "Delegate refused data, rv: " << rv
;
39 Reset(QUIC_BAD_APPLICATION_PAYLOAD
);
45 void QuicReliableClientStream::OnClose() {
47 delegate_
->OnClose(connection_error());
50 ReliableQuicStream::OnClose();
53 void QuicReliableClientStream::OnCanWrite() {
54 ReliableQuicStream::OnCanWrite();
56 if (!HasBufferedData() && !callback_
.is_null()) {
57 base::ResetAndReturn(&callback_
).Run(OK
);
61 QuicPriority
QuicReliableClientStream::EffectivePriority() const {
62 if (delegate_
&& delegate_
->HasSendHeadersComplete()) {
63 return QuicDataStream::EffectivePriority();
65 return QuicWriteBlockedList::kHighestPriority
;
68 int QuicReliableClientStream::WriteStreamData(
69 base::StringPiece data
,
71 const CompletionCallback
& callback
) {
72 // We should not have data buffered.
73 DCHECK(!HasBufferedData());
74 // Writes the data, or buffers it.
75 WriteOrBufferData(data
, fin
, nullptr);
76 if (!HasBufferedData()) {
81 return ERR_IO_PENDING
;
84 void QuicReliableClientStream::SetDelegate(
85 QuicReliableClientStream::Delegate
* delegate
) {
86 DCHECK(!(delegate_
&& delegate
));
90 void QuicReliableClientStream::OnError(int error
) {
92 QuicReliableClientStream::Delegate
* delegate
= delegate_
;
94 delegate
->OnError(error
);
98 bool QuicReliableClientStream::CanWrite(const CompletionCallback
& callback
) {
99 bool can_write
= session()->connection()->CanWrite(HAS_RETRANSMITTABLE_DATA
);
101 session()->MarkWriteBlocked(id(), EffectivePriority());
102 DCHECK(callback_
.is_null());
103 callback_
= callback
;