| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/profiler/scoped_tracker.h" | 12 #include "base/profiler/scoped_tracker.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/socket_performance_watcher.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 20 TCPClientSocket::TCPClientSocket(const AddressList& addresses, | 21 TCPClientSocket::TCPClientSocket( |
| 21 net::NetLog* net_log, | 22 const AddressList& addresses, |
| 22 const net::NetLog::Source& source) | 23 scoped_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
| 23 : socket_(new TCPSocket(net_log, source)), | 24 net::NetLog* net_log, |
| 25 const net::NetLog::Source& source) |
| 26 : socket_performance_watcher_( |
| 27 socket_performance_watcher.get() |
| 28 ? socket_performance_watcher.get()->GetWeakPtr() |
| 29 : base::WeakPtr<SocketPerformanceWatcher>()), |
| 30 socket_(new TCPSocket(std::move(socket_performance_watcher), |
| 31 net_log, |
| 32 source)), |
| 24 addresses_(addresses), | 33 addresses_(addresses), |
| 25 current_address_index_(-1), | 34 current_address_index_(-1), |
| 26 next_connect_state_(CONNECT_STATE_NONE), | 35 next_connect_state_(CONNECT_STATE_NONE), |
| 27 previously_disconnected_(false), | 36 previously_disconnected_(false), |
| 28 total_received_bytes_(0) {} | 37 total_received_bytes_(0), |
| 38 notify_reset_socket_performance_watcher_(false) {} |
| 29 | 39 |
| 30 TCPClientSocket::TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, | 40 TCPClientSocket::TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, |
| 31 const IPEndPoint& peer_address) | 41 const IPEndPoint& peer_address) |
| 32 : socket_(std::move(connected_socket)), | 42 : socket_(std::move(connected_socket)), |
| 33 addresses_(AddressList(peer_address)), | 43 addresses_(AddressList(peer_address)), |
| 34 current_address_index_(0), | 44 current_address_index_(0), |
| 35 next_connect_state_(CONNECT_STATE_NONE), | 45 next_connect_state_(CONNECT_STATE_NONE), |
| 36 previously_disconnected_(false), | 46 previously_disconnected_(false), |
| 37 total_received_bytes_(0) { | 47 total_received_bytes_(0), |
| 48 notify_reset_socket_performance_watcher_(false) { |
| 38 DCHECK(socket_); | 49 DCHECK(socket_); |
| 39 | 50 |
| 40 socket_->SetDefaultOptionsForClient(); | 51 socket_->SetDefaultOptionsForClient(); |
| 41 use_history_.set_was_ever_connected(); | 52 use_history_.set_was_ever_connected(); |
| 42 } | 53 } |
| 43 | 54 |
| 44 TCPClientSocket::~TCPClientSocket() { | 55 TCPClientSocket::~TCPClientSocket() { |
| 45 Disconnect(); | 56 Disconnect(); |
| 46 } | 57 } |
| 47 | 58 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 if (bind_address_) { | 156 if (bind_address_) { |
| 146 result = socket_->Bind(*bind_address_); | 157 result = socket_->Bind(*bind_address_); |
| 147 if (result != OK) { | 158 if (result != OK) { |
| 148 socket_->Close(); | 159 socket_->Close(); |
| 149 return result; | 160 return result; |
| 150 } | 161 } |
| 151 } | 162 } |
| 152 } | 163 } |
| 153 } | 164 } |
| 154 | 165 |
| 166 if (socket_performance_watcher_ && notify_reset_socket_performance_watcher_) { |
| 167 socket_performance_watcher_->Reset(); |
| 168 } |
| 169 |
| 155 // |socket_| is owned by this class and the callback won't be run once | 170 // |socket_| is owned by this class and the callback won't be run once |
| 156 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. | 171 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. |
| 157 return socket_->Connect(endpoint, | 172 return socket_->Connect(endpoint, |
| 158 base::Bind(&TCPClientSocket::DidCompleteConnect, | 173 base::Bind(&TCPClientSocket::DidCompleteConnect, |
| 159 base::Unretained(this))); | 174 base::Unretained(this))); |
| 160 } | 175 } |
| 161 | 176 |
| 162 int TCPClientSocket::DoConnectComplete(int result) { | 177 int TCPClientSocket::DoConnectComplete(int result) { |
| 163 if (result == OK) { | 178 if (result == OK) { |
| 164 use_history_.set_was_ever_connected(); | 179 use_history_.set_was_ever_connected(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 188 bind_address_.reset(); | 203 bind_address_.reset(); |
| 189 } | 204 } |
| 190 | 205 |
| 191 void TCPClientSocket::DoDisconnect() { | 206 void TCPClientSocket::DoDisconnect() { |
| 192 total_received_bytes_ = 0; | 207 total_received_bytes_ = 0; |
| 193 EmitTCPMetricsHistogramsOnDisconnect(); | 208 EmitTCPMetricsHistogramsOnDisconnect(); |
| 194 // If connecting or already connected, record that the socket has been | 209 // If connecting or already connected, record that the socket has been |
| 195 // disconnected. | 210 // disconnected. |
| 196 previously_disconnected_ = socket_->IsValid() && current_address_index_ >= 0; | 211 previously_disconnected_ = socket_->IsValid() && current_address_index_ >= 0; |
| 197 socket_->Close(); | 212 socket_->Close(); |
| 213 notify_reset_socket_performance_watcher_ = true; |
| 198 } | 214 } |
| 199 | 215 |
| 200 bool TCPClientSocket::IsConnected() const { | 216 bool TCPClientSocket::IsConnected() const { |
| 201 return socket_->IsConnected(); | 217 return socket_->IsConnected(); |
| 202 } | 218 } |
| 203 | 219 |
| 204 bool TCPClientSocket::IsConnectedAndIdle() const { | 220 bool TCPClientSocket::IsConnectedAndIdle() const { |
| 205 return socket_->IsConnectedAndIdle(); | 221 return socket_->IsConnectedAndIdle(); |
| 206 } | 222 } |
| 207 | 223 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 void TCPClientSocket::EmitTCPMetricsHistogramsOnDisconnect() { | 395 void TCPClientSocket::EmitTCPMetricsHistogramsOnDisconnect() { |
| 380 base::TimeDelta rtt; | 396 base::TimeDelta rtt; |
| 381 if (socket_->GetEstimatedRoundTripTime(&rtt)) { | 397 if (socket_->GetEstimatedRoundTripTime(&rtt)) { |
| 382 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TcpRtt.AtDisconnect", rtt, | 398 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TcpRtt.AtDisconnect", rtt, |
| 383 base::TimeDelta::FromMilliseconds(1), | 399 base::TimeDelta::FromMilliseconds(1), |
| 384 base::TimeDelta::FromMinutes(10), 100); | 400 base::TimeDelta::FromMinutes(10), 100); |
| 385 } | 401 } |
| 386 } | 402 } |
| 387 | 403 |
| 388 } // namespace net | 404 } // namespace net |
| OLD | NEW |