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/socket/socket_bio_adapter.h" | 5 #include "net/socket/socket_bio_adapter.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/metrics/field_trial.h" | |
davidben
2017/02/10 23:33:48
No longer used.
xunjieli
2017/02/13 20:28:18
Done.
| |
14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/socket/socket.h" | |
17 #include "net/socket/stream_socket.h" | 19 #include "net/socket/stream_socket.h" |
18 #include "net/ssl/openssl_ssl_util.h" | 20 #include "net/ssl/openssl_ssl_util.h" |
19 #include "third_party/boringssl/src/include/openssl/bio.h" | 21 #include "third_party/boringssl/src/include/openssl/bio.h" |
20 | 22 |
21 namespace net { | 23 namespace net { |
22 | 24 |
23 SocketBIOAdapter::SocketBIOAdapter(StreamSocket* socket, | 25 SocketBIOAdapter::SocketBIOAdapter(StreamSocket* socket, |
24 int read_buffer_capacity, | 26 int read_buffer_capacity, |
25 int write_buffer_capacity, | 27 int write_buffer_capacity, |
26 Delegate* delegate) | 28 Delegate* delegate) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 | 82 |
81 if (read_result_ == 0) { | 83 if (read_result_ == 0) { |
82 // Instantiate the read buffer and read from the socket. Although only |len| | 84 // Instantiate the read buffer and read from the socket. Although only |len| |
83 // bytes were requested, intentionally read to the full buffer size. The SSL | 85 // bytes were requested, intentionally read to the full buffer size. The SSL |
84 // layer reads the record header and body in separate reads to avoid | 86 // layer reads the record header and body in separate reads to avoid |
85 // overreading, but issuing one is more efficient. SSL sockets are not | 87 // overreading, but issuing one is more efficient. SSL sockets are not |
86 // reused after shutdown for non-SSL traffic, so overreading is fine. | 88 // reused after shutdown for non-SSL traffic, so overreading is fine. |
87 DCHECK(!read_buffer_); | 89 DCHECK(!read_buffer_); |
88 DCHECK_EQ(0, read_offset_); | 90 DCHECK_EQ(0, read_offset_); |
89 read_buffer_ = new IOBuffer(read_buffer_capacity_); | 91 read_buffer_ = new IOBuffer(read_buffer_capacity_); |
90 int result = socket_->Read(read_buffer_.get(), read_buffer_capacity_, | 92 int result = ERR_READ_IF_READY_NOT_IMPLEMENTED; |
91 read_callback_); | 93 if (base::FeatureList::IsEnabled(Socket::kReadIfReadyExperiment)) { |
davidben
2017/02/10 23:33:48
#include "base/feature_list.h"
xunjieli
2017/02/13 20:28:18
Done.
| |
94 result = socket_->ReadIfReady( | |
95 read_buffer_.get(), read_buffer_capacity_, | |
96 base::Bind(&SocketBIOAdapter::OnSocketReadIfReadyComplete, | |
97 weak_factory_.GetWeakPtr())); | |
98 if (result == ERR_IO_PENDING) | |
99 read_buffer_ = nullptr; | |
100 } | |
101 if (result == ERR_READ_IF_READY_NOT_IMPLEMENTED) { | |
102 result = socket_->Read(read_buffer_.get(), read_buffer_capacity_, | |
103 read_callback_); | |
104 } | |
92 if (result == ERR_IO_PENDING) { | 105 if (result == ERR_IO_PENDING) { |
93 read_result_ = ERR_IO_PENDING; | 106 read_result_ = ERR_IO_PENDING; |
94 } else { | 107 } else { |
95 HandleSocketReadResult(result); | 108 HandleSocketReadResult(result); |
96 } | 109 } |
97 } | 110 } |
98 | 111 |
99 // There is a pending Read(). Inform the caller to retry when it completes. | 112 // There is a pending Read(). Inform the caller to retry when it completes. |
100 if (read_result_ == ERR_IO_PENDING) { | 113 if (read_result_ == ERR_IO_PENDING) { |
101 BIO_set_retry_read(bio()); | 114 BIO_set_retry_read(bio()); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 | 149 |
137 // The read buffer is no longer needed. | 150 // The read buffer is no longer needed. |
138 if (read_result_ <= 0) | 151 if (read_result_ <= 0) |
139 read_buffer_ = nullptr; | 152 read_buffer_ = nullptr; |
140 } | 153 } |
141 | 154 |
142 void SocketBIOAdapter::OnSocketReadComplete(int result) { | 155 void SocketBIOAdapter::OnSocketReadComplete(int result) { |
143 DCHECK_EQ(ERR_IO_PENDING, read_result_); | 156 DCHECK_EQ(ERR_IO_PENDING, read_result_); |
144 | 157 |
145 HandleSocketReadResult(result); | 158 HandleSocketReadResult(result); |
146 delegate_->OnReadReady(); | 159 delegate_->OnReadReady(read_result_ > 0 ? OK : read_result_); |
160 } | |
161 | |
162 void SocketBIOAdapter::OnSocketReadIfReadyComplete(int result) { | |
163 DCHECK_EQ(ERR_IO_PENDING, read_result_); | |
164 DCHECK_GE(OK, result); | |
165 | |
166 // Do not use HandleSocketReadResult() because |result == OK| doesn't mean EOF | |
167 read_result_ = result; | |
168 | |
169 delegate_->OnReadReady(read_result_); | |
147 } | 170 } |
148 | 171 |
149 int SocketBIOAdapter::BIOWrite(const char* in, int len) { | 172 int SocketBIOAdapter::BIOWrite(const char* in, int len) { |
150 if (len <= 0) | 173 if (len <= 0) |
151 return len; | 174 return len; |
152 | 175 |
153 // If the write buffer is not empty, there must be a pending Write() to flush | 176 // If the write buffer is not empty, there must be a pending Write() to flush |
154 // it. | 177 // it. |
155 DCHECK(write_buffer_used_ == 0 || write_error_ == ERR_IO_PENDING); | 178 DCHECK(write_buffer_used_ == 0 || write_error_ == ERR_IO_PENDING); |
156 | 179 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
207 // have been empty.) | 230 // have been empty.) |
208 SocketWrite(); | 231 SocketWrite(); |
209 | 232 |
210 // If a read-interrupting write error was synchronously discovered, | 233 // If a read-interrupting write error was synchronously discovered, |
211 // asynchronously notify OnReadReady. See https://crbug.com/249848. Avoid | 234 // asynchronously notify OnReadReady. See https://crbug.com/249848. Avoid |
212 // reentrancy by deferring it to a later event loop iteration. | 235 // reentrancy by deferring it to a later event loop iteration. |
213 if (write_error_ != OK && write_error_ != ERR_IO_PENDING && | 236 if (write_error_ != OK && write_error_ != ERR_IO_PENDING && |
214 read_result_ == ERR_IO_PENDING) { | 237 read_result_ == ERR_IO_PENDING) { |
215 base::ThreadTaskRunnerHandle::Get()->PostTask( | 238 base::ThreadTaskRunnerHandle::Get()->PostTask( |
216 FROM_HERE, base::Bind(&SocketBIOAdapter::CallOnReadReady, | 239 FROM_HERE, base::Bind(&SocketBIOAdapter::CallOnReadReady, |
217 weak_factory_.GetWeakPtr())); | 240 weak_factory_.GetWeakPtr(), write_error_)); |
218 } | 241 } |
219 | 242 |
220 return bytes_copied; | 243 return bytes_copied; |
221 } | 244 } |
222 | 245 |
223 void SocketBIOAdapter::SocketWrite() { | 246 void SocketBIOAdapter::SocketWrite() { |
224 while (write_error_ == OK && write_buffer_used_ > 0) { | 247 while (write_error_ == OK && write_buffer_used_ > 0) { |
225 int write_size = | 248 int write_size = |
226 std::min(write_buffer_used_, write_buffer_->RemainingCapacity()); | 249 std::min(write_buffer_used_, write_buffer_->RemainingCapacity()); |
227 int result = | 250 int result = |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 | 287 |
265 bool was_full = write_buffer_used_ == write_buffer_->capacity(); | 288 bool was_full = write_buffer_used_ == write_buffer_->capacity(); |
266 | 289 |
267 HandleSocketWriteResult(result); | 290 HandleSocketWriteResult(result); |
268 SocketWrite(); | 291 SocketWrite(); |
269 | 292 |
270 // If transitioning from being unable to accept data to being able to, signal | 293 // If transitioning from being unable to accept data to being able to, signal |
271 // OnWriteReady. | 294 // OnWriteReady. |
272 if (was_full) { | 295 if (was_full) { |
273 base::WeakPtr<SocketBIOAdapter> guard(weak_factory_.GetWeakPtr()); | 296 base::WeakPtr<SocketBIOAdapter> guard(weak_factory_.GetWeakPtr()); |
274 delegate_->OnWriteReady(); | 297 DCHECK_GE(OK, write_error_); |
298 delegate_->OnWriteReady(write_error_); | |
275 // OnWriteReady may delete the adapter. | 299 // OnWriteReady may delete the adapter. |
276 if (!guard) | 300 if (!guard) |
277 return; | 301 return; |
278 } | 302 } |
279 | 303 |
280 // Write errors are fed back into BIO_read once the read buffer is empty. If | 304 // Write errors are fed back into BIO_read once the read buffer is empty. If |
281 // BIO_read is currently blocked, signal early that a read result is ready. | 305 // BIO_read is currently blocked, signal early that a read result is ready. |
282 if (result < 0 && read_result_ == ERR_IO_PENDING) | 306 if (result < 0 && read_result_ == ERR_IO_PENDING) |
283 delegate_->OnReadReady(); | 307 delegate_->OnReadReady(result); |
284 } | 308 } |
285 | 309 |
286 void SocketBIOAdapter::CallOnReadReady() { | 310 void SocketBIOAdapter::CallOnReadReady(int rv) { |
287 if (read_result_ == ERR_IO_PENDING) | 311 if (read_result_ == ERR_IO_PENDING) |
288 delegate_->OnReadReady(); | 312 delegate_->OnReadReady(rv); |
289 } | 313 } |
290 | 314 |
291 SocketBIOAdapter* SocketBIOAdapter::GetAdapter(BIO* bio) { | 315 SocketBIOAdapter* SocketBIOAdapter::GetAdapter(BIO* bio) { |
292 DCHECK_EQ(&kBIOMethod, bio->method); | 316 DCHECK_EQ(&kBIOMethod, bio->method); |
293 SocketBIOAdapter* adapter = reinterpret_cast<SocketBIOAdapter*>(bio->ptr); | 317 SocketBIOAdapter* adapter = reinterpret_cast<SocketBIOAdapter*>(bio->ptr); |
294 if (adapter) | 318 if (adapter) |
295 DCHECK_EQ(bio, adapter->bio()); | 319 DCHECK_EQ(bio, adapter->bio()); |
296 return adapter; | 320 return adapter; |
297 } | 321 } |
298 | 322 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 SocketBIOAdapter::BIOReadWrapper, | 365 SocketBIOAdapter::BIOReadWrapper, |
342 nullptr, // puts | 366 nullptr, // puts |
343 nullptr, // gets | 367 nullptr, // gets |
344 SocketBIOAdapter::BIOCtrlWrapper, | 368 SocketBIOAdapter::BIOCtrlWrapper, |
345 nullptr, // create | 369 nullptr, // create |
346 nullptr, // destroy | 370 nullptr, // destroy |
347 nullptr, // callback_ctrl | 371 nullptr, // callback_ctrl |
348 }; | 372 }; |
349 | 373 |
350 } // namespace net | 374 } // namespace net |
OLD | NEW |