OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/tcp_client_socket.h" | 5 #include "net/socket/tcp_client_socket.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netdb.h> | 9 #include <netdb.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 current_ai_(NULL), | 130 current_ai_(NULL), |
131 read_watcher_(this), | 131 read_watcher_(this), |
132 write_watcher_(this), | 132 write_watcher_(this), |
133 read_callback_(NULL), | 133 read_callback_(NULL), |
134 write_callback_(NULL), | 134 write_callback_(NULL), |
135 next_connect_state_(CONNECT_STATE_NONE), | 135 next_connect_state_(CONNECT_STATE_NONE), |
136 connect_os_error_(0), | 136 connect_os_error_(0), |
137 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), | 137 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), |
138 previously_disconnected_(false), | 138 previously_disconnected_(false), |
139 use_tcp_fastopen_(false), | 139 use_tcp_fastopen_(false), |
140 tcp_fastopen_connected_(false) { | 140 tcp_fastopen_connected_(false), |
| 141 num_bytes_read_(0) { |
141 scoped_refptr<NetLog::EventParameters> params; | 142 scoped_refptr<NetLog::EventParameters> params; |
142 if (source.is_valid()) | 143 if (source.is_valid()) |
143 params = new NetLogSourceParameter("source_dependency", source); | 144 params = new NetLogSourceParameter("source_dependency", source); |
144 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 145 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
145 | 146 |
146 if (is_tcp_fastopen_enabled()) | 147 if (is_tcp_fastopen_enabled()) |
147 use_tcp_fastopen_ = true; | 148 use_tcp_fastopen_ = true; |
148 } | 149 } |
149 | 150 |
150 TCPClientSocketLibevent::~TCPClientSocketLibevent() { | 151 TCPClientSocketLibevent::~TCPClientSocketLibevent() { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 size_t addr_len = sizeof(addr_storage); | 292 size_t addr_len = sizeof(addr_storage); |
292 if (!bind_address_->ToSockAddr(addr, &addr_len)) | 293 if (!bind_address_->ToSockAddr(addr, &addr_len)) |
293 return ERR_INVALID_ARGUMENT; | 294 return ERR_INVALID_ARGUMENT; |
294 if (HANDLE_EINTR(bind(socket_, addr, addr_len))) | 295 if (HANDLE_EINTR(bind(socket_, addr, addr_len))) |
295 return MapSystemError(errno); | 296 return MapSystemError(errno); |
296 } | 297 } |
297 } | 298 } |
298 | 299 |
299 // Connect the socket. | 300 // Connect the socket. |
300 if (!use_tcp_fastopen_) { | 301 if (!use_tcp_fastopen_) { |
| 302 connect_start_time_ = base::TimeTicks::Now(); |
301 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, | 303 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, |
302 static_cast<int>(current_ai_->ai_addrlen)))) { | 304 static_cast<int>(current_ai_->ai_addrlen)))) { |
303 // Connected without waiting! | 305 // Connected without waiting! |
304 return OK; | 306 return OK; |
305 } | 307 } |
306 } else { | 308 } else { |
307 // With TCP FastOpen, we pretend that the socket is connected. | 309 // With TCP FastOpen, we pretend that the socket is connected. |
308 DCHECK(!tcp_fastopen_connected_); | 310 DCHECK(!tcp_fastopen_connected_); |
309 return OK; | 311 return OK; |
310 } | 312 } |
(...skipping 21 matching lines...) Expand all Loading... |
332 int os_error = connect_os_error_; | 334 int os_error = connect_os_error_; |
333 connect_os_error_ = 0; | 335 connect_os_error_ = 0; |
334 scoped_refptr<NetLog::EventParameters> params; | 336 scoped_refptr<NetLog::EventParameters> params; |
335 if (result != OK) | 337 if (result != OK) |
336 params = new NetLogIntegerParameter("os_error", os_error); | 338 params = new NetLogIntegerParameter("os_error", os_error); |
337 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); | 339 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); |
338 | 340 |
339 write_socket_watcher_.StopWatchingFileDescriptor(); | 341 write_socket_watcher_.StopWatchingFileDescriptor(); |
340 | 342 |
341 if (result == OK) { | 343 if (result == OK) { |
| 344 connect_time_micros_ = base::TimeTicks::Now() - connect_start_time_; |
342 use_history_.set_was_ever_connected(); | 345 use_history_.set_was_ever_connected(); |
343 return OK; // Done! | 346 return OK; // Done! |
344 } | 347 } |
345 | 348 |
346 // Close whatever partially connected socket we currently have. | 349 // Close whatever partially connected socket we currently have. |
347 DoDisconnect(); | 350 DoDisconnect(); |
348 | 351 |
349 // Try to fall back to the next address in the list. | 352 // Try to fall back to the next address in the list. |
350 if (current_ai_->ai_next) { | 353 if (current_ai_->ai_next) { |
351 next_connect_state_ = CONNECT_STATE_CONNECT; | 354 next_connect_state_ = CONNECT_STATE_CONNECT; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 DCHECK(!waiting_connect()); | 436 DCHECK(!waiting_connect()); |
434 DCHECK(!read_callback_); | 437 DCHECK(!read_callback_); |
435 // Synchronous operation not supported | 438 // Synchronous operation not supported |
436 DCHECK(callback); | 439 DCHECK(callback); |
437 DCHECK_GT(buf_len, 0); | 440 DCHECK_GT(buf_len, 0); |
438 | 441 |
439 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 442 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
440 if (nread >= 0) { | 443 if (nread >= 0) { |
441 base::StatsCounter read_bytes("tcp.read_bytes"); | 444 base::StatsCounter read_bytes("tcp.read_bytes"); |
442 read_bytes.Add(nread); | 445 read_bytes.Add(nread); |
| 446 num_bytes_read_ += static_cast<int64>(nread); |
443 if (nread > 0) | 447 if (nread > 0) |
444 use_history_.set_was_used_to_convey_data(); | 448 use_history_.set_was_used_to_convey_data(); |
445 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, | 449 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, |
446 buf->data()); | 450 buf->data()); |
447 return nread; | 451 return nread; |
448 } | 452 } |
449 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 453 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
450 DVLOG(1) << "read failed, errno " << errno; | 454 DVLOG(1) << "read failed, errno " << errno; |
451 return MapSystemError(errno); | 455 return MapSystemError(errno); |
452 } | 456 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 void TCPClientSocketLibevent::DidCompleteRead() { | 631 void TCPClientSocketLibevent::DidCompleteRead() { |
628 int bytes_transferred; | 632 int bytes_transferred; |
629 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 633 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
630 read_buf_len_)); | 634 read_buf_len_)); |
631 | 635 |
632 int result; | 636 int result; |
633 if (bytes_transferred >= 0) { | 637 if (bytes_transferred >= 0) { |
634 result = bytes_transferred; | 638 result = bytes_transferred; |
635 base::StatsCounter read_bytes("tcp.read_bytes"); | 639 base::StatsCounter read_bytes("tcp.read_bytes"); |
636 read_bytes.Add(bytes_transferred); | 640 read_bytes.Add(bytes_transferred); |
| 641 num_bytes_read_ += static_cast<int64>(bytes_transferred); |
637 if (bytes_transferred > 0) | 642 if (bytes_transferred > 0) |
638 use_history_.set_was_used_to_convey_data(); | 643 use_history_.set_was_used_to_convey_data(); |
639 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, | 644 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, |
640 read_buf_->data()); | 645 read_buf_->data()); |
641 } else { | 646 } else { |
642 result = MapSystemError(errno); | 647 result = MapSystemError(errno); |
643 } | 648 } |
644 | 649 |
645 if (result != ERR_IO_PENDING) { | 650 if (result != ERR_IO_PENDING) { |
646 read_buf_ = NULL; | 651 read_buf_ = NULL; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
716 } | 721 } |
717 | 722 |
718 bool TCPClientSocketLibevent::WasEverUsed() const { | 723 bool TCPClientSocketLibevent::WasEverUsed() const { |
719 return use_history_.was_used_to_convey_data(); | 724 return use_history_.was_used_to_convey_data(); |
720 } | 725 } |
721 | 726 |
722 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { | 727 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { |
723 return use_tcp_fastopen_; | 728 return use_tcp_fastopen_; |
724 } | 729 } |
725 | 730 |
| 731 int64 TCPClientSocketLibevent::NumBytesRead() const { |
| 732 return num_bytes_read_; |
| 733 } |
| 734 |
| 735 base::TimeDelta TCPClientSocketLibevent::GetConnectTimeMicros() const { |
| 736 return connect_time_micros_; |
| 737 } |
| 738 |
726 } // namespace net | 739 } // namespace net |
OLD | NEW |