[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/quic/quic_reliable_client_stream.h" |
| 6 | |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 7 | #include "base/callback_helpers.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 8 | #include "net/base/net_errors.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 9 | #include "net/quic/quic_session.h" |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 10 | #include "net/quic/quic_write_blocked_list.h" |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 11 | |
| 12 | namespace net { |
| 13 | |
| 14 | QuicReliableClientStream::QuicReliableClientStream(QuicStreamId id, |
[email protected] | 0d10b59 | 2013-02-14 16:09:26 | [diff] [blame] | 15 | QuicSession* session, |
| 16 | const BoundNetLog& net_log) |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 17 | : QuicDataStream(id, session), |
[email protected] | 0d10b59 | 2013-02-14 16:09:26 | [diff] [blame] | 18 | net_log_(net_log), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 19 | delegate_(NULL) { |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | QuicReliableClientStream::~QuicReliableClientStream() { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 23 | if (delegate_) |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 24 | delegate_->OnClose(connection_error()); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | uint32 QuicReliableClientStream::ProcessData(const char* data, |
| 28 | uint32 data_len) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 29 | // TODO(rch): buffer data if we don't have a delegate. |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 30 | if (!delegate_) |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 31 | return ERR_ABORTED; |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 32 | |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 33 | int rv = delegate_->OnDataReceived(data, data_len); |
| 34 | if (rv != OK) { |
| 35 | DLOG(ERROR) << "Delegate refused data, rv: " << rv; |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 36 | Reset(QUIC_BAD_APPLICATION_PAYLOAD); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | return data_len; |
| 40 | } |
| 41 | |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 42 | void QuicReliableClientStream::OnFinRead() { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 43 | if (delegate_) { |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 44 | delegate_->OnClose(connection_error()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 45 | delegate_ = NULL; |
| 46 | } |
[email protected] | 2adef90e | 2013-12-02 20:26:57 | [diff] [blame] | 47 | ReliableQuicStream::OnFinRead(); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 50 | void QuicReliableClientStream::OnCanWrite() { |
| 51 | ReliableQuicStream::OnCanWrite(); |
| 52 | |
| 53 | if (!HasBufferedData() && !callback_.is_null()) { |
| 54 | base::ResetAndReturn(&callback_).Run(OK); |
| 55 | } |
| 56 | } |
| 57 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 58 | QuicPriority QuicReliableClientStream::EffectivePriority() const { |
[email protected] | 7c87ce7 | 2013-09-19 17:47:38 | [diff] [blame] | 59 | if (delegate_ && delegate_->HasSendHeadersComplete()) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 60 | return QuicDataStream::EffectivePriority(); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 61 | } |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 62 | return QuicWriteBlockedList::kHighestPriority; |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 65 | int QuicReliableClientStream::WriteStreamData( |
| 66 | base::StringPiece data, |
| 67 | bool fin, |
| 68 | const CompletionCallback& callback) { |
| 69 | // We should not have data buffered. |
| 70 | DCHECK(!HasBufferedData()); |
| 71 | // Writes the data, or buffers it. |
[email protected] | b9475b58 | 2014-03-20 20:04:33 | [diff] [blame] | 72 | WriteOrBufferData(data, fin, NULL); |
[email protected] | 6adeb92 | 2013-09-01 22:43:25 | [diff] [blame] | 73 | if (!HasBufferedData()) { |
| 74 | return OK; |
| 75 | } |
| 76 | |
| 77 | callback_ = callback; |
| 78 | return ERR_IO_PENDING; |
| 79 | } |
| 80 | |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 81 | void QuicReliableClientStream::SetDelegate( |
| 82 | QuicReliableClientStream::Delegate* delegate) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 83 | DCHECK((!delegate_ && delegate) || (delegate_ && !delegate)); |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 84 | delegate_ = delegate; |
| 85 | } |
| 86 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 87 | void QuicReliableClientStream::OnError(int error) { |
| 88 | if (delegate_) { |
| 89 | QuicReliableClientStream::Delegate* delegate = delegate_; |
| 90 | delegate_ = NULL; |
| 91 | delegate->OnError(error); |
| 92 | } |
| 93 | } |
| 94 | |
[email protected] | 13428d4 | 2013-11-26 18:50:36 | [diff] [blame] | 95 | bool QuicReliableClientStream::CanWrite(const CompletionCallback& callback) { |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 96 | bool can_write = session()->connection()->CanWrite(HAS_RETRANSMITTABLE_DATA); |
[email protected] | 13428d4 | 2013-11-26 18:50:36 | [diff] [blame] | 97 | if (!can_write) { |
| 98 | session()->MarkWriteBlocked(id(), EffectivePriority()); |
| 99 | DCHECK(callback_.is_null()); |
| 100 | callback_ = callback; |
| 101 | } |
| 102 | return can_write; |
| 103 | } |
| 104 | |
[email protected] | dd3fd0e | 2012-11-04 05:14:40 | [diff] [blame] | 105 | } // namespace net |