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

Side by Side Diff: net/quic/bidirectional_stream_quic_impl.cc

Issue 1744693002: Implement QUIC-based net::BidirectionalStream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@basecl
Patch Set: Removed dependency Created 4 years, 9 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
(Empty)
1 // Copyright 2016 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/bidirectional_stream_quic_impl.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/timer/timer.h"
11 #include "net/http/bidirectional_stream_request_info.h"
12 #include "net/socket/next_proto.h"
13 #include "net/spdy/spdy_header_block.h"
14 #include "net/spdy/spdy_http_utils.h"
15
16 namespace net {
17
18 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl(
19 const base::WeakPtr<QuicChromiumClientSession>& session)
20 : session_(session),
21 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()),
22 stream_(nullptr),
23 request_info_(nullptr),
24 delegate_(nullptr),
25 response_status_(OK),
26 negotiated_protocol_(kProtoUnknown),
27 read_buffer_len_(0),
28 headers_bytes_received_(0),
29 headers_bytes_sent_(0),
30 closed_stream_received_bytes_(0),
31 closed_stream_sent_bytes_(0),
32 has_sent_headers_(false),
33 has_received_headers_(false),
34 weak_factory_(this) {
35 DCHECK(session_);
36 session_->AddObserver(this);
37 }
38
39 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() {
40 Cancel();
41 if (session_)
42 session_->RemoveObserver(this);
43 }
44
45 void BidirectionalStreamQuicImpl::Start(
46 const BidirectionalStreamRequestInfo* request_info,
47 const BoundNetLog& net_log,
48 BidirectionalStreamJob::Delegate* delegate,
49 scoped_ptr<base::Timer> /* timer */) {
50 DCHECK(!stream_);
51
52 if (!session_) {
53 response_status_ = was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR
54 : ERR_QUIC_HANDSHAKE_FAILED;
55 NotifyError(response_status_);
56 return;
57 }
58
59 delegate_ = delegate;
60 request_info_ = request_info;
61
62 int rv = stream_request_.StartRequest(
63 session_, &stream_,
64 base::Bind(&BidirectionalStreamQuicImpl::OnStreamReady,
65 weak_factory_.GetWeakPtr()));
66 if (rv == OK) {
67 OnStreamReady(rv);
68 } else if (!was_handshake_confirmed_) {
69 response_status_ = ERR_QUIC_HANDSHAKE_FAILED;
70 NotifyError(response_status_);
71 }
72 }
73
74 int BidirectionalStreamQuicImpl::ReadData(IOBuffer* buf, int buf_len) {
75 DCHECK(buf);
76 DCHECK(buf_len);
77
78 if (!stream_) {
79 // If the stream is already closed, there is no body to read.
80 return response_status_;
81 }
82 int rv = stream_->Read(buf, buf_len);
83 if (rv != ERR_IO_PENDING) {
84 if (stream_->IsDoneReading()) {
85 stream_->OnFinRead(); // If write side is close, will call OnClose.
86 }
87 return rv;
88 }
89 // Read will complete asynchronously and Delegate::OnReadCompleted will be
90 // called upon completion.
91 read_buffer_ = buf;
92 read_buffer_len_ = buf_len;
93 return ERR_IO_PENDING;
94 }
95
96 void BidirectionalStreamQuicImpl::SendData(IOBuffer* data,
97 int length,
98 bool end_stream) {
99 DCHECK(stream_);
100
101 if (length > 0 || end_stream) {
102 base::StringPiece string_data(data->data(), length);
103 int rv = stream_->WriteStreamData(
104 string_data, end_stream,
105 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete,
106 weak_factory_.GetWeakPtr()));
107 DCHECK(rv == OK || rv == ERR_IO_PENDING);
108 if (rv == OK) {
109 base::ThreadTaskRunnerHandle::Get()->PostTask(
110 FROM_HERE,
111 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete,
112 weak_factory_.GetWeakPtr(), OK));
113 }
114 }
115 }
116
117 void BidirectionalStreamQuicImpl::Cancel() {
118 if (stream_) {
119 stream_->SetDelegate(nullptr);
120 stream_->Reset(QUIC_STREAM_CANCELLED);
121 ResetStream();
122 }
123 }
124
125 NextProto BidirectionalStreamQuicImpl::GetProtocol() const {
126 return negotiated_protocol_;
127 }
128
129 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const {
130 if (stream_)
131 return headers_bytes_received_ + stream_->stream_bytes_read();
132 return headers_bytes_received_ + closed_stream_received_bytes_;
133 }
134
135 int64_t BidirectionalStreamQuicImpl::GetTotalSentBytes() const {
136 if (stream_)
137 return headers_bytes_sent_ + stream_->stream_bytes_written();
138 return headers_bytes_sent_ + closed_stream_sent_bytes_;
139 }
140
141 void BidirectionalStreamQuicImpl::OnHeadersAvailable(
142 const SpdyHeaderBlock& headers,
143 size_t frame_len) {
144 headers_bytes_received_ += frame_len;
145 negotiated_protocol_ = kProtoQUIC1SPDY3;
146 if (!has_received_headers_) {
147 has_received_headers_ = true;
148 delegate_->OnHeadersReceived(headers);
149 } else {
150 if (stream_->IsDoneReading()) {
151 stream_->OnFinRead(); // If write side is close, will call OnClose
152 }
153 delegate_->OnTrailersReceived(headers);
154 }
155 }
156
157 void BidirectionalStreamQuicImpl::OnDataAvailable() {
158 // Return early if ReadData has not been called.
159 if (!read_buffer_)
160 return;
161
162 CHECK(read_buffer_);
163 CHECK_NE(0, read_buffer_len_);
164 int rv = ReadData(read_buffer_.get(), read_buffer_len_);
165 if (rv == ERR_IO_PENDING) {
166 // Spurrious notification. Wait for the next one.
167 return;
168 }
169 read_buffer_ = nullptr;
170 read_buffer_len_ = 0;
171 delegate_->OnDataRead(rv);
172 }
173
174 void BidirectionalStreamQuicImpl::OnClose(QuicErrorCode error) {
175 DCHECK(stream_);
176 if (error == QUIC_NO_ERROR &&
177 stream_->stream_error() == QUIC_STREAM_NO_ERROR) {
178 ResetStream();
179 return;
180 }
181 response_status_ = was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR
182 : ERR_QUIC_HANDSHAKE_FAILED;
183 ResetStream();
184 NotifyError(response_status_);
185 }
186
187 void BidirectionalStreamQuicImpl::OnError(int error) {
188 NotifyError(error);
189 }
190
191 bool BidirectionalStreamQuicImpl::HasSendHeadersComplete() {
192 return has_sent_headers_;
193 }
194
195 void BidirectionalStreamQuicImpl::OnCryptoHandshakeConfirmed() {
196 was_handshake_confirmed_ = true;
197 }
198
199 void BidirectionalStreamQuicImpl::OnSessionClosed(int error) {
200 DCHECK_NE(OK, error);
201 session_.reset();
202 NotifyError(error);
203 }
204
205 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) {
206 DCHECK_NE(ERR_IO_PENDING, rv);
207 DCHECK(rv == OK || !stream_);
208 if (rv == OK) {
209 stream_->SetDelegate(this);
210 SendRequestHeaders();
211 } else {
212 response_status_ = rv;
213 NotifyError(response_status_);
214 }
215 }
216
217 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) {
218 DCHECK(rv == OK || !stream_);
219 if (rv == OK) {
220 delegate_->OnDataSent();
221 } else {
222 NotifyError(rv);
223 }
224 }
225
226 void BidirectionalStreamQuicImpl::SendRequestHeaders() {
227 DCHECK(!has_sent_headers_);
228 DCHECK(stream_);
229
230 SpdyHeaderBlock headers;
231 HttpRequestInfo http_request_info;
232 http_request_info.url = request_info_->url;
233 http_request_info.method = request_info_->method;
234 http_request_info.extra_headers = request_info_->extra_headers;
235
236 CreateSpdyHeadersFromHttpRequest(http_request_info,
237 http_request_info.extra_headers, HTTP2, true,
238 &headers);
239 size_t frame_len = stream_->WriteHeaders(
240 headers, request_info_->end_stream_on_headers, nullptr);
241 headers_bytes_sent_ += frame_len;
242 has_sent_headers_ = true;
243 delegate_->OnHeadersSent();
244 }
245
246 void BidirectionalStreamQuicImpl::NotifyError(int error) {
247 DCHECK_NE(OK, error);
248 DCHECK_NE(ERR_IO_PENDING, error);
249
250 ResetStream();
251 delegate_->OnFailed(error);
252 }
253
254 void BidirectionalStreamQuicImpl::ResetStream() {
255 if (!stream_)
256 return;
257 closed_stream_received_bytes_ = stream_->stream_bytes_read();
258 closed_stream_sent_bytes_ = stream_->stream_bytes_written();
259 stream_->SetDelegate(nullptr);
260 stream_ = nullptr;
261 }
262
263 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698