| OLD | NEW |
| 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/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 bound_socket_(kInvalidSocket), | 130 bound_socket_(kInvalidSocket), |
| 131 addresses_(addresses), | 131 addresses_(addresses), |
| 132 current_address_index_(-1), | 132 current_address_index_(-1), |
| 133 read_watcher_(this), | 133 read_watcher_(this), |
| 134 write_watcher_(this), | 134 write_watcher_(this), |
| 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_(IsTCPFastOpenEnabled()), | 139 use_tcp_fastopen_(IsTCPFastOpenEnabled()), |
| 140 tcp_fastopen_connected_(false), | 140 tcp_fastopen_connected_(false) { |
| 141 num_bytes_read_(0) { | |
| 142 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 141 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 143 source.ToEventParametersCallback()); | 142 source.ToEventParametersCallback()); |
| 144 } | 143 } |
| 145 | 144 |
| 146 TCPClientSocketLibevent::~TCPClientSocketLibevent() { | 145 TCPClientSocketLibevent::~TCPClientSocketLibevent() { |
| 147 Disconnect(); | 146 Disconnect(); |
| 148 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 147 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 149 } | 148 } |
| 150 | 149 |
| 151 int TCPClientSocketLibevent::AdoptSocket(int socket) { | 150 int TCPClientSocketLibevent::AdoptSocket(int socket) { |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 return MapSystemError(errno); | 286 return MapSystemError(errno); |
| 288 } | 287 } |
| 289 } | 288 } |
| 290 | 289 |
| 291 // Connect the socket. | 290 // Connect the socket. |
| 292 if (!use_tcp_fastopen_) { | 291 if (!use_tcp_fastopen_) { |
| 293 SockaddrStorage storage; | 292 SockaddrStorage storage; |
| 294 if (!endpoint.ToSockAddr(storage.addr, &storage.addr_len)) | 293 if (!endpoint.ToSockAddr(storage.addr, &storage.addr_len)) |
| 295 return ERR_INVALID_ARGUMENT; | 294 return ERR_INVALID_ARGUMENT; |
| 296 | 295 |
| 297 connect_start_time_ = base::TimeTicks::Now(); | |
| 298 if (!HANDLE_EINTR(connect(socket_, storage.addr, storage.addr_len))) { | 296 if (!HANDLE_EINTR(connect(socket_, storage.addr, storage.addr_len))) { |
| 299 // Connected without waiting! | 297 // Connected without waiting! |
| 300 return OK; | 298 return OK; |
| 301 } | 299 } |
| 302 } else { | 300 } else { |
| 303 // With TCP FastOpen, we pretend that the socket is connected. | 301 // With TCP FastOpen, we pretend that the socket is connected. |
| 304 DCHECK(!tcp_fastopen_connected_); | 302 DCHECK(!tcp_fastopen_connected_); |
| 305 return OK; | 303 return OK; |
| 306 } | 304 } |
| 307 | 305 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 328 int os_error = connect_os_error_; | 326 int os_error = connect_os_error_; |
| 329 connect_os_error_ = 0; | 327 connect_os_error_ = 0; |
| 330 if (result != OK) { | 328 if (result != OK) { |
| 331 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, | 329 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, |
| 332 NetLog::IntegerCallback("os_error", os_error)); | 330 NetLog::IntegerCallback("os_error", os_error)); |
| 333 } else { | 331 } else { |
| 334 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT); | 332 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT); |
| 335 } | 333 } |
| 336 | 334 |
| 337 if (result == OK) { | 335 if (result == OK) { |
| 338 connect_time_micros_ = base::TimeTicks::Now() - connect_start_time_; | |
| 339 write_socket_watcher_.StopWatchingFileDescriptor(); | 336 write_socket_watcher_.StopWatchingFileDescriptor(); |
| 340 use_history_.set_was_ever_connected(); | 337 use_history_.set_was_ever_connected(); |
| 341 return OK; // Done! | 338 return OK; // Done! |
| 342 } | 339 } |
| 343 | 340 |
| 344 // Close whatever partially connected socket we currently have. | 341 // Close whatever partially connected socket we currently have. |
| 345 DoDisconnect(); | 342 DoDisconnect(); |
| 346 | 343 |
| 347 // Try to fall back to the next address in the list. | 344 // Try to fall back to the next address in the list. |
| 348 if (current_address_index_ + 1 < static_cast<int>(addresses_.size())) { | 345 if (current_address_index_ + 1 < static_cast<int>(addresses_.size())) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 DCHECK(!waiting_connect()); | 429 DCHECK(!waiting_connect()); |
| 433 DCHECK(read_callback_.is_null()); | 430 DCHECK(read_callback_.is_null()); |
| 434 // Synchronous operation not supported | 431 // Synchronous operation not supported |
| 435 DCHECK(!callback.is_null()); | 432 DCHECK(!callback.is_null()); |
| 436 DCHECK_GT(buf_len, 0); | 433 DCHECK_GT(buf_len, 0); |
| 437 | 434 |
| 438 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 435 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
| 439 if (nread >= 0) { | 436 if (nread >= 0) { |
| 440 base::StatsCounter read_bytes("tcp.read_bytes"); | 437 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 441 read_bytes.Add(nread); | 438 read_bytes.Add(nread); |
| 442 num_bytes_read_ += static_cast<int64>(nread); | |
| 443 if (nread > 0) | 439 if (nread > 0) |
| 444 use_history_.set_was_used_to_convey_data(); | 440 use_history_.set_was_used_to_convey_data(); |
| 445 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, | 441 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, |
| 446 buf->data()); | 442 buf->data()); |
| 447 return nread; | 443 return nread; |
| 448 } | 444 } |
| 449 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 445 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 450 int net_error = MapSystemError(errno); | 446 int net_error = MapSystemError(errno); |
| 451 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 447 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 452 CreateNetLogSocketErrorCallback(net_error, errno)); | 448 CreateNetLogSocketErrorCallback(net_error, errno)); |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 void TCPClientSocketLibevent::DidCompleteRead() { | 657 void TCPClientSocketLibevent::DidCompleteRead() { |
| 662 int bytes_transferred; | 658 int bytes_transferred; |
| 663 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 659 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
| 664 read_buf_len_)); | 660 read_buf_len_)); |
| 665 | 661 |
| 666 int result; | 662 int result; |
| 667 if (bytes_transferred >= 0) { | 663 if (bytes_transferred >= 0) { |
| 668 result = bytes_transferred; | 664 result = bytes_transferred; |
| 669 base::StatsCounter read_bytes("tcp.read_bytes"); | 665 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 670 read_bytes.Add(bytes_transferred); | 666 read_bytes.Add(bytes_transferred); |
| 671 num_bytes_read_ += static_cast<int64>(bytes_transferred); | |
| 672 if (bytes_transferred > 0) | 667 if (bytes_transferred > 0) |
| 673 use_history_.set_was_used_to_convey_data(); | 668 use_history_.set_was_used_to_convey_data(); |
| 674 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, | 669 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, |
| 675 read_buf_->data()); | 670 read_buf_->data()); |
| 676 } else { | 671 } else { |
| 677 result = MapSystemError(errno); | 672 result = MapSystemError(errno); |
| 678 if (result != ERR_IO_PENDING) { | 673 if (result != ERR_IO_PENDING) { |
| 679 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 674 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 680 CreateNetLogSocketErrorCallback(result, errno)); | 675 CreateNetLogSocketErrorCallback(result, errno)); |
| 681 } | 676 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 } | 757 } |
| 763 | 758 |
| 764 bool TCPClientSocketLibevent::WasEverUsed() const { | 759 bool TCPClientSocketLibevent::WasEverUsed() const { |
| 765 return use_history_.was_used_to_convey_data(); | 760 return use_history_.was_used_to_convey_data(); |
| 766 } | 761 } |
| 767 | 762 |
| 768 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { | 763 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { |
| 769 return use_tcp_fastopen_; | 764 return use_tcp_fastopen_; |
| 770 } | 765 } |
| 771 | 766 |
| 772 int64 TCPClientSocketLibevent::NumBytesRead() const { | |
| 773 return num_bytes_read_; | |
| 774 } | |
| 775 | |
| 776 base::TimeDelta TCPClientSocketLibevent::GetConnectTimeMicros() const { | |
| 777 return connect_time_micros_; | |
| 778 } | |
| 779 | |
| 780 bool TCPClientSocketLibevent::WasNpnNegotiated() const { | 767 bool TCPClientSocketLibevent::WasNpnNegotiated() const { |
| 781 return false; | 768 return false; |
| 782 } | 769 } |
| 783 | 770 |
| 784 NextProto TCPClientSocketLibevent::GetNegotiatedProtocol() const { | 771 NextProto TCPClientSocketLibevent::GetNegotiatedProtocol() const { |
| 785 return kProtoUnknown; | 772 return kProtoUnknown; |
| 786 } | 773 } |
| 787 | 774 |
| 788 bool TCPClientSocketLibevent::GetSSLInfo(SSLInfo* ssl_info) { | 775 bool TCPClientSocketLibevent::GetSSLInfo(SSLInfo* ssl_info) { |
| 789 return false; | 776 return false; |
| 790 } | 777 } |
| 791 | 778 |
| 792 } // namespace net | 779 } // namespace net |
| OLD | NEW |