Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(628)

Side by Side Diff: net/quic/chromium/quic_chromium_client_stream.cc

Issue 2880643004: Move the async write handling from QuicChromiumClientStream to the Handle (Closed)
Patch Set: Rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/chromium/quic_chromium_client_stream.h" 5 #include "net/quic/chromium/quic_chromium_client_stream.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 66
67 int rv = stream_->Read(read_body_buffer_, read_body_buffer_len_); 67 int rv = stream_->Read(read_body_buffer_, read_body_buffer_len_);
68 if (rv == ERR_IO_PENDING) 68 if (rv == ERR_IO_PENDING)
69 return; // Spurrious, likely because of trailers? 69 return; // Spurrious, likely because of trailers?
70 70
71 read_body_buffer_ = nullptr; 71 read_body_buffer_ = nullptr;
72 read_body_buffer_len_ = 0; 72 read_body_buffer_len_ = 0;
73 ResetAndReturn(&read_body_callback_).Run(rv); 73 ResetAndReturn(&read_body_callback_).Run(rv);
74 } 74 }
75 75
76 void QuicChromiumClientStream::Handle::OnCanWrite() {
77 if (!write_callback_)
78 return;
79
80 base::ResetAndReturn(&write_callback_).Run(OK);
81 }
82
76 void QuicChromiumClientStream::Handle::OnClose() { 83 void QuicChromiumClientStream::Handle::OnClose() {
77 if (stream_) 84 if (stream_)
78 SaveState(); 85 SaveState();
79 stream_ = nullptr; 86 stream_ = nullptr;
80 if (delegate_) { 87 if (delegate_) {
81 auto* delegate = delegate_; 88 auto* delegate = delegate_;
82 delegate_ = nullptr; 89 delegate_ = nullptr;
83 delegate->OnClose(); 90 delegate->OnClose();
84 } 91 }
85 } 92 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 return stream_->WriteHeaders(std::move(header_block), fin, 144 return stream_->WriteHeaders(std::move(header_block), fin,
138 ack_notifier_delegate); 145 ack_notifier_delegate);
139 } 146 }
140 147
141 int QuicChromiumClientStream::Handle::WriteStreamData( 148 int QuicChromiumClientStream::Handle::WriteStreamData(
142 base::StringPiece data, 149 base::StringPiece data,
143 bool fin, 150 bool fin,
144 const CompletionCallback& callback) { 151 const CompletionCallback& callback) {
145 if (!stream_) 152 if (!stream_)
146 return ERR_CONNECTION_CLOSED; 153 return ERR_CONNECTION_CLOSED;
147 return stream_->WriteStreamData(data, fin, callback); 154
155 if (stream_->WriteStreamData(data, fin))
156 return OK;
157
158 write_callback_ = callback;
159 return ERR_IO_PENDING;
148 } 160 }
149 161
150 int QuicChromiumClientStream::Handle::WritevStreamData( 162 int QuicChromiumClientStream::Handle::WritevStreamData(
151 const std::vector<scoped_refptr<IOBuffer>>& buffers, 163 const std::vector<scoped_refptr<IOBuffer>>& buffers,
152 const std::vector<int>& lengths, 164 const std::vector<int>& lengths,
153 bool fin, 165 bool fin,
154 const CompletionCallback& callback) { 166 const CompletionCallback& callback) {
155 if (!stream_) 167 if (!stream_)
156 return ERR_CONNECTION_CLOSED; 168 return ERR_CONNECTION_CLOSED;
157 return stream_->WritevStreamData(buffers, lengths, fin, callback); 169
170 if (stream_->WritevStreamData(buffers, lengths, fin))
171 return OK;
172
173 write_callback_ = callback;
174 return ERR_IO_PENDING;
158 } 175 }
159 176
160 int QuicChromiumClientStream::Handle::Read(IOBuffer* buf, int buf_len) { 177 int QuicChromiumClientStream::Handle::Read(IOBuffer* buf, int buf_len) {
161 if (!stream_) 178 if (!stream_)
162 return ERR_CONNECTION_CLOSED; 179 return ERR_CONNECTION_CLOSED;
163 return stream_->Read(buf, buf_len); 180 return stream_->Read(buf, buf_len);
164 } 181 }
165 182
166 void QuicChromiumClientStream::Handle::OnFinRead() { 183 void QuicChromiumClientStream::Handle::OnFinRead() {
167 if (stream_) 184 if (stream_)
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 if (handle_) { 396 if (handle_) {
380 handle_->OnClose(); 397 handle_->OnClose();
381 handle_ = nullptr; 398 handle_ = nullptr;
382 } 399 }
383 QuicStream::OnClose(); 400 QuicStream::OnClose();
384 } 401 }
385 402
386 void QuicChromiumClientStream::OnCanWrite() { 403 void QuicChromiumClientStream::OnCanWrite() {
387 QuicStream::OnCanWrite(); 404 QuicStream::OnCanWrite();
388 405
389 if (!HasBufferedData() && !write_callback_.is_null()) { 406 if (!HasBufferedData() && handle_)
390 base::ResetAndReturn(&write_callback_).Run(OK); 407 handle_->OnCanWrite();
391 }
392 } 408 }
393 409
394 size_t QuicChromiumClientStream::WriteHeaders( 410 size_t QuicChromiumClientStream::WriteHeaders(
395 SpdyHeaderBlock header_block, 411 SpdyHeaderBlock header_block,
396 bool fin, 412 bool fin,
397 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) { 413 QuicReferenceCountedPointer<QuicAckListenerInterface> ack_listener) {
398 if (!session()->IsCryptoHandshakeConfirmed()) { 414 if (!session()->IsCryptoHandshakeConfirmed()) {
399 auto entry = header_block.find(":method"); 415 auto entry = header_block.find(":method");
400 DCHECK(entry != header_block.end()); 416 DCHECK(entry != header_block.end());
401 DCHECK_NE("POST", entry->second); 417 DCHECK_NE("POST", entry->second);
402 } 418 }
403 net_log_.AddEvent( 419 net_log_.AddEvent(
404 NetLogEventType::QUIC_CHROMIUM_CLIENT_STREAM_SEND_REQUEST_HEADERS, 420 NetLogEventType::QUIC_CHROMIUM_CLIENT_STREAM_SEND_REQUEST_HEADERS,
405 base::Bind(&QuicRequestNetLogCallback, id(), &header_block, 421 base::Bind(&QuicRequestNetLogCallback, id(), &header_block,
406 QuicSpdyStream::priority())); 422 QuicSpdyStream::priority()));
407 size_t len = QuicSpdyStream::WriteHeaders(std::move(header_block), fin, 423 size_t len = QuicSpdyStream::WriteHeaders(std::move(header_block), fin,
408 std::move(ack_listener)); 424 std::move(ack_listener));
409 initial_headers_sent_ = true; 425 initial_headers_sent_ = true;
410 return len; 426 return len;
411 } 427 }
412 428
413 SpdyPriority QuicChromiumClientStream::priority() const { 429 SpdyPriority QuicChromiumClientStream::priority() const {
414 return initial_headers_sent_ ? QuicSpdyStream::priority() 430 return initial_headers_sent_ ? QuicSpdyStream::priority()
415 : kV3HighestPriority; 431 : kV3HighestPriority;
416 } 432 }
417 433
418 int QuicChromiumClientStream::WriteStreamData( 434 bool QuicChromiumClientStream::WriteStreamData(QuicStringPiece data, bool fin) {
419 QuicStringPiece data, 435 // Must not be called when data is buffered.
420 bool fin,
421 const CompletionCallback& callback) {
422 // We should not have data buffered.
423 DCHECK(!HasBufferedData()); 436 DCHECK(!HasBufferedData());
424 // Writes the data, or buffers it. 437 // Writes the data, or buffers it.
425 WriteOrBufferData(data, fin, nullptr); 438 WriteOrBufferData(data, fin, nullptr);
426 if (!HasBufferedData()) { 439 return !HasBufferedData(); // Was all data written?
427 return OK;
428 }
429
430 write_callback_ = callback;
431 return ERR_IO_PENDING;
432 } 440 }
433 441
434 int QuicChromiumClientStream::WritevStreamData( 442 bool QuicChromiumClientStream::WritevStreamData(
435 const std::vector<scoped_refptr<IOBuffer>>& buffers, 443 const std::vector<scoped_refptr<IOBuffer>>& buffers,
436 const std::vector<int>& lengths, 444 const std::vector<int>& lengths,
437 bool fin, 445 bool fin) {
438 const CompletionCallback& callback) {
439 // Must not be called when data is buffered. 446 // Must not be called when data is buffered.
440 DCHECK(!HasBufferedData()); 447 DCHECK(!HasBufferedData());
441 // Writes the data, or buffers it. 448 // Writes the data, or buffers it.
442 for (size_t i = 0; i < buffers.size(); ++i) { 449 for (size_t i = 0; i < buffers.size(); ++i) {
443 bool is_fin = fin && (i == buffers.size() - 1); 450 bool is_fin = fin && (i == buffers.size() - 1);
444 QuicStringPiece string_data(buffers[i]->data(), lengths[i]); 451 QuicStringPiece string_data(buffers[i]->data(), lengths[i]);
445 WriteOrBufferData(string_data, is_fin, nullptr); 452 WriteOrBufferData(string_data, is_fin, nullptr);
446 } 453 }
447 if (!HasBufferedData()) { 454 return !HasBufferedData(); // Was all data written?
448 return OK;
449 }
450
451 write_callback_ = callback;
452 return ERR_IO_PENDING;
453 } 455 }
454 456
455 std::unique_ptr<QuicChromiumClientStream::Handle> 457 std::unique_ptr<QuicChromiumClientStream::Handle>
456 QuicChromiumClientStream::CreateHandle( 458 QuicChromiumClientStream::CreateHandle(
457 QuicChromiumClientStream::Delegate* delegate) { 459 QuicChromiumClientStream::Delegate* delegate) {
458 DCHECK(!handle_); 460 DCHECK(!handle_);
459 auto handle = std::unique_ptr<QuicChromiumClientStream::Handle>( 461 auto handle = std::unique_ptr<QuicChromiumClientStream::Handle>(
460 new QuicChromiumClientStream::Handle(this, delegate)); 462 new QuicChromiumClientStream::Handle(this, delegate));
461 handle_ = handle.get(); 463 handle_ = handle.get();
462 464
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 573
572 void QuicChromiumClientStream::DisableConnectionMigration() { 574 void QuicChromiumClientStream::DisableConnectionMigration() {
573 can_migrate_ = false; 575 can_migrate_ = false;
574 } 576 }
575 577
576 bool QuicChromiumClientStream::IsFirstStream() { 578 bool QuicChromiumClientStream::IsFirstStream() {
577 return id() == kHeadersStreamId + 2; 579 return id() == kHeadersStreamId + 2;
578 } 580 }
579 581
580 } // namespace net 582 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/chromium/quic_chromium_client_stream.h ('k') | net/quic/chromium/quic_chromium_client_stream_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698