OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/bidirectional_stream_quic_impl.h" | 5 #include "net/quic/chromium/bidirectional_stream_quic_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl( | 23 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl( |
24 std::unique_ptr<QuicChromiumClientSession::Handle> session) | 24 std::unique_ptr<QuicChromiumClientSession::Handle> session) |
25 : session_(std::move(session)), | 25 : session_(std::move(session)), |
26 stream_(nullptr), | 26 stream_(nullptr), |
27 request_info_(nullptr), | 27 request_info_(nullptr), |
28 delegate_(nullptr), | 28 delegate_(nullptr), |
29 response_status_(OK), | 29 response_status_(OK), |
30 negotiated_protocol_(kProtoUnknown), | 30 negotiated_protocol_(kProtoUnknown), |
31 expect_trailers_(true), | |
31 read_buffer_len_(0), | 32 read_buffer_len_(0), |
32 headers_bytes_received_(0), | 33 headers_bytes_received_(0), |
33 headers_bytes_sent_(0), | 34 headers_bytes_sent_(0), |
34 closed_stream_received_bytes_(0), | 35 closed_stream_received_bytes_(0), |
35 closed_stream_sent_bytes_(0), | 36 closed_stream_sent_bytes_(0), |
36 closed_is_first_stream_(false), | 37 closed_is_first_stream_(false), |
37 has_sent_headers_(false), | 38 has_sent_headers_(false), |
38 send_request_headers_automatically_(true), | 39 send_request_headers_automatically_(true), |
39 weak_factory_(this) {} | 40 weak_factory_(this) {} |
40 | 41 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 read_buffer_len_ = buffer_len; | 122 read_buffer_len_ = buffer_len; |
122 return ERR_IO_PENDING; | 123 return ERR_IO_PENDING; |
123 } | 124 } |
124 | 125 |
125 if (rv < 0) | 126 if (rv < 0) |
126 return rv; | 127 return rv; |
127 | 128 |
128 if (stream_->IsDoneReading()) { | 129 if (stream_->IsDoneReading()) { |
129 // If the write side is closed, OnFinRead() will call | 130 // If the write side is closed, OnFinRead() will call |
130 // BidirectionalStreamQuicImpl::OnClose(). | 131 // BidirectionalStreamQuicImpl::OnClose(). |
132 expect_trailers_ = false; | |
131 stream_->OnFinRead(); | 133 stream_->OnFinRead(); |
132 } | 134 } |
133 return rv; | 135 return rv; |
134 } | 136 } |
135 | 137 |
136 void BidirectionalStreamQuicImpl::SendData(const scoped_refptr<IOBuffer>& data, | 138 void BidirectionalStreamQuicImpl::SendData(const scoped_refptr<IOBuffer>& data, |
137 int length, | 139 int length, |
138 bool end_stream) { | 140 bool end_stream) { |
139 DCHECK(length > 0 || (length == 0 && end_stream)); | 141 DCHECK(length > 0 || (length == 0 && end_stream)); |
140 if (!stream_) { | 142 if (!stream_) { |
(...skipping 12 matching lines...) Expand all Loading... | |
153 bundler = | 155 bundler = |
154 session_->CreatePacketBundler(QuicConnection::SEND_ACK_IF_PENDING); | 156 session_->CreatePacketBundler(QuicConnection::SEND_ACK_IF_PENDING); |
155 SendRequestHeaders(); | 157 SendRequestHeaders(); |
156 } | 158 } |
157 | 159 |
158 QuicStringPiece string_data(data->data(), length); | 160 QuicStringPiece string_data(data->data(), length); |
159 int rv = stream_->WriteStreamData( | 161 int rv = stream_->WriteStreamData( |
160 string_data, end_stream, | 162 string_data, end_stream, |
161 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 163 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
162 weak_factory_.GetWeakPtr())); | 164 weak_factory_.GetWeakPtr())); |
163 DCHECK(rv == OK || rv == ERR_IO_PENDING); | 165 if (rv != ERR_IO_PENDING) { |
164 if (rv == OK) { | |
165 base::ThreadTaskRunnerHandle::Get()->PostTask( | 166 base::ThreadTaskRunnerHandle::Get()->PostTask( |
166 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 167 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
167 weak_factory_.GetWeakPtr(), OK)); | 168 weak_factory_.GetWeakPtr(), OK)); |
xunjieli
2017/05/31 00:25:22
Need to pass |rv| instead of hardcoding OK here.
Ryan Hamilton
2017/05/31 02:49:55
I could do that but OnSendDataComplete has:
DCH
Ryan Hamilton
2017/05/31 14:18:26
Oh. I confused myself. Fixed to pass |rv| and clea
| |
168 } | 169 } |
169 } | 170 } |
170 | 171 |
171 void BidirectionalStreamQuicImpl::SendvData( | 172 void BidirectionalStreamQuicImpl::SendvData( |
172 const std::vector<scoped_refptr<IOBuffer>>& buffers, | 173 const std::vector<scoped_refptr<IOBuffer>>& buffers, |
173 const std::vector<int>& lengths, | 174 const std::vector<int>& lengths, |
174 bool end_stream) { | 175 bool end_stream) { |
175 DCHECK_EQ(buffers.size(), lengths.size()); | 176 DCHECK_EQ(buffers.size(), lengths.size()); |
176 | 177 |
177 if (!stream_) { | 178 if (!stream_) { |
178 LOG(ERROR) << "Trying to send data after stream has been destroyed."; | 179 LOG(ERROR) << "Trying to send data after stream has been destroyed."; |
179 base::ThreadTaskRunnerHandle::Get()->PostTask( | 180 base::ThreadTaskRunnerHandle::Get()->PostTask( |
180 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::NotifyError, | 181 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::NotifyError, |
181 weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); | 182 weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); |
182 return; | 183 return; |
183 } | 184 } |
184 | 185 |
185 std::unique_ptr<QuicConnection::ScopedPacketBundler> bundler( | 186 std::unique_ptr<QuicConnection::ScopedPacketBundler> bundler( |
186 session_->CreatePacketBundler(QuicConnection::SEND_ACK_IF_PENDING)); | 187 session_->CreatePacketBundler(QuicConnection::SEND_ACK_IF_PENDING)); |
187 if (!has_sent_headers_) { | 188 if (!has_sent_headers_) { |
188 DCHECK(!send_request_headers_automatically_); | 189 DCHECK(!send_request_headers_automatically_); |
189 SendRequestHeaders(); | 190 SendRequestHeaders(); |
190 } | 191 } |
191 | 192 |
192 int rv = stream_->WritevStreamData( | 193 int rv = stream_->WritevStreamData( |
193 buffers, lengths, end_stream, | 194 buffers, lengths, end_stream, |
194 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 195 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
195 weak_factory_.GetWeakPtr())); | 196 weak_factory_.GetWeakPtr())); |
196 | 197 |
197 DCHECK(rv == OK || rv == ERR_IO_PENDING); | 198 if (rv != ERR_IO_PENDING) { |
198 if (rv == OK) { | |
199 base::ThreadTaskRunnerHandle::Get()->PostTask( | 199 base::ThreadTaskRunnerHandle::Get()->PostTask( |
200 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 200 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
201 weak_factory_.GetWeakPtr(), OK)); | 201 weak_factory_.GetWeakPtr(), OK)); |
xunjieli
2017/05/31 00:25:22
Need to pass |rv| instead of hardcoding OK here.
Ryan Hamilton
2017/05/31 02:49:55
Ditto. Happy to do whatever you prefer.
Ryan Hamilton
2017/05/31 14:18:26
Done.
| |
202 } | 202 } |
203 } | 203 } |
204 | 204 |
205 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { | 205 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { |
206 return negotiated_protocol_; | 206 return negotiated_protocol_; |
207 } | 207 } |
208 | 208 |
209 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const { | 209 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const { |
210 if (stream_) | 210 if (stream_) |
211 return headers_bytes_received_ + stream_->stream_bytes_read(); | 211 return headers_bytes_received_ + stream_->stream_bytes_read(); |
(...skipping 13 matching lines...) Expand all Loading... | |
225 is_first_stream = stream_->IsFirstStream(); | 225 is_first_stream = stream_->IsFirstStream(); |
226 if (is_first_stream) { | 226 if (is_first_stream) { |
227 load_timing_info->socket_reused = false; | 227 load_timing_info->socket_reused = false; |
228 load_timing_info->connect_timing = connect_timing_; | 228 load_timing_info->connect_timing = connect_timing_; |
229 } else { | 229 } else { |
230 load_timing_info->socket_reused = true; | 230 load_timing_info->socket_reused = true; |
231 } | 231 } |
232 return true; | 232 return true; |
233 } | 233 } |
234 | 234 |
235 void BidirectionalStreamQuicImpl::OnClose() { | |
236 DCHECK(stream_); | |
237 | |
238 if (stream_->connection_error() != QUIC_NO_ERROR || | |
239 stream_->stream_error() != QUIC_STREAM_NO_ERROR) { | |
240 NotifyError(session_->IsCryptoHandshakeConfirmed() | |
241 ? ERR_QUIC_PROTOCOL_ERROR | |
242 : ERR_QUIC_HANDSHAKE_FAILED); | |
243 return; | |
244 } | |
245 | |
246 if (!stream_->fin_sent() || !stream_->fin_received()) { | |
247 // The connection must have been closed by the peer with QUIC_NO_ERROR, | |
248 // which is improper. | |
249 NotifyError(ERR_UNEXPECTED); | |
250 return; | |
251 } | |
252 | |
253 // The connection was closed normally so there is no need to notify | |
254 // the delegate. | |
255 ResetStream(); | |
256 } | |
257 | |
258 void BidirectionalStreamQuicImpl::OnError(int error) { | |
259 NotifyError(error); | |
260 } | |
261 | |
262 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { | 235 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { |
263 DCHECK_NE(ERR_IO_PENDING, rv); | 236 DCHECK_NE(ERR_IO_PENDING, rv); |
264 DCHECK(rv == OK || !stream_); | 237 DCHECK(rv == OK || !stream_); |
265 if (rv == OK) { | 238 if (rv == OK) { |
266 stream_ = session_->ReleaseStream(this); | 239 stream_ = session_->ReleaseStream(); |
267 NotifyStreamReady(); | 240 NotifyStreamReady(); |
268 | 241 |
269 rv = stream_->ReadInitialHeaders( | 242 rv = stream_->ReadInitialHeaders( |
270 &initial_headers_, | 243 &initial_headers_, |
271 base::Bind(&BidirectionalStreamQuicImpl::OnReadInitialHeadersComplete, | 244 base::Bind(&BidirectionalStreamQuicImpl::OnReadInitialHeadersComplete, |
272 weak_factory_.GetWeakPtr())); | 245 weak_factory_.GetWeakPtr())); |
273 if (rv == ERR_IO_PENDING) | 246 if (rv == ERR_IO_PENDING) |
274 return; | 247 return; |
275 | 248 |
276 OnReadInitialHeadersComplete(rv); | 249 OnReadInitialHeadersComplete(rv); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 base::Bind(&BidirectionalStreamQuicImpl::OnReadTrailingHeadersComplete, | 285 base::Bind(&BidirectionalStreamQuicImpl::OnReadTrailingHeadersComplete, |
313 weak_factory_.GetWeakPtr())); | 286 weak_factory_.GetWeakPtr())); |
314 | 287 |
315 if (rv != ERR_IO_PENDING) | 288 if (rv != ERR_IO_PENDING) |
316 OnReadTrailingHeadersComplete(rv); | 289 OnReadTrailingHeadersComplete(rv); |
317 } | 290 } |
318 | 291 |
319 void BidirectionalStreamQuicImpl::OnReadTrailingHeadersComplete(int rv) { | 292 void BidirectionalStreamQuicImpl::OnReadTrailingHeadersComplete(int rv) { |
320 DCHECK_NE(ERR_IO_PENDING, rv); | 293 DCHECK_NE(ERR_IO_PENDING, rv); |
321 if (rv < 0) { | 294 if (rv < 0) { |
322 NotifyError(rv); | 295 if (expect_trailers_) |
296 NotifyError(rv); | |
323 return; | 297 return; |
324 } | 298 } |
325 | 299 |
326 headers_bytes_received_ += rv; | 300 headers_bytes_received_ += rv; |
327 | 301 |
328 if (delegate_) | 302 if (delegate_) |
329 delegate_->OnTrailersReceived(trailing_headers_); | 303 delegate_->OnTrailersReceived(trailing_headers_); |
330 } | 304 } |
331 | 305 |
332 void BidirectionalStreamQuicImpl::OnReadDataComplete(int rv) { | 306 void BidirectionalStreamQuicImpl::OnReadDataComplete(int rv) { |
333 DCHECK_GE(rv, 0); | |
334 read_buffer_ = nullptr; | 307 read_buffer_ = nullptr; |
335 read_buffer_len_ = 0; | 308 read_buffer_len_ = 0; |
336 | 309 |
337 if (stream_->IsDoneReading()) { | 310 if (stream_->IsDoneReading()) { |
338 // If the write side is closed, OnFinRead() will call | 311 // If the write side is closed, OnFinRead() will call |
339 // BidirectionalStreamQuicImpl::OnClose(). | 312 // BidirectionalStreamQuicImpl::OnClose(). |
340 stream_->OnFinRead(); | 313 stream_->OnFinRead(); |
341 } | 314 } |
342 | 315 |
343 if (delegate_) | 316 if (!delegate_) |
317 return; | |
318 | |
319 if (rv < 0) | |
320 NotifyError(rv); | |
321 else | |
344 delegate_->OnDataRead(rv); | 322 delegate_->OnDataRead(rv); |
345 } | 323 } |
346 | 324 |
347 void BidirectionalStreamQuicImpl::NotifyError(int error) { | 325 void BidirectionalStreamQuicImpl::NotifyError(int error) { |
348 DCHECK_NE(OK, error); | 326 DCHECK_NE(OK, error); |
349 DCHECK_NE(ERR_IO_PENDING, error); | 327 DCHECK_NE(ERR_IO_PENDING, error); |
350 | 328 |
351 ResetStream(); | 329 ResetStream(); |
352 if (delegate_) { | 330 if (delegate_) { |
353 response_status_ = error; | 331 response_status_ = error; |
(...skipping 13 matching lines...) Expand all Loading... | |
367 if (delegate_) | 345 if (delegate_) |
368 delegate_->OnStreamReady(has_sent_headers_); | 346 delegate_->OnStreamReady(has_sent_headers_); |
369 } | 347 } |
370 | 348 |
371 void BidirectionalStreamQuicImpl::ResetStream() { | 349 void BidirectionalStreamQuicImpl::ResetStream() { |
372 if (!stream_) | 350 if (!stream_) |
373 return; | 351 return; |
374 closed_stream_received_bytes_ = stream_->stream_bytes_read(); | 352 closed_stream_received_bytes_ = stream_->stream_bytes_read(); |
375 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); | 353 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); |
376 closed_is_first_stream_ = stream_->IsFirstStream(); | 354 closed_is_first_stream_ = stream_->IsFirstStream(); |
377 stream_->ClearDelegate(); | |
378 stream_ = nullptr; | |
379 } | 355 } |
380 | 356 |
381 } // namespace net | 357 } // namespace net |
OLD | NEW |