| 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_(socket_performance_watcher.get()), |
| 27 socket_(new TCPSocket(std::move(socket_performance_watcher), |
| 28 net_log, |
| 29 source)), |
| 24 addresses_(addresses), | 30 addresses_(addresses), |
| 25 current_address_index_(-1), | 31 current_address_index_(-1), |
| 26 next_connect_state_(CONNECT_STATE_NONE), | 32 next_connect_state_(CONNECT_STATE_NONE), |
| 27 previously_disconnected_(false), | 33 previously_disconnected_(false), |
| 28 total_received_bytes_(0) {} | 34 total_received_bytes_(0), |
| 35 notify_reset_socket_performance_watcher_(false) {} |
| 29 | 36 |
| 30 TCPClientSocket::TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, | 37 TCPClientSocket::TCPClientSocket(scoped_ptr<TCPSocket> connected_socket, |
| 31 const IPEndPoint& peer_address) | 38 const IPEndPoint& peer_address) |
| 32 : socket_(std::move(connected_socket)), | 39 : socket_performance_watcher_(nullptr), |
| 40 socket_(std::move(connected_socket)), |
| 33 addresses_(AddressList(peer_address)), | 41 addresses_(AddressList(peer_address)), |
| 34 current_address_index_(0), | 42 current_address_index_(0), |
| 35 next_connect_state_(CONNECT_STATE_NONE), | 43 next_connect_state_(CONNECT_STATE_NONE), |
| 36 previously_disconnected_(false), | 44 previously_disconnected_(false), |
| 37 total_received_bytes_(0) { | 45 total_received_bytes_(0), |
| 46 notify_reset_socket_performance_watcher_(false) { |
| 38 DCHECK(socket_); | 47 DCHECK(socket_); |
| 39 | 48 |
| 40 socket_->SetDefaultOptionsForClient(); | 49 socket_->SetDefaultOptionsForClient(); |
| 41 use_history_.set_was_ever_connected(); | 50 use_history_.set_was_ever_connected(); |
| 42 } | 51 } |
| 43 | 52 |
| 44 TCPClientSocket::~TCPClientSocket() { | 53 TCPClientSocket::~TCPClientSocket() { |
| 45 Disconnect(); | 54 Disconnect(); |
| 46 } | 55 } |
| 47 | 56 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 if (bind_address_) { | 154 if (bind_address_) { |
| 146 result = socket_->Bind(*bind_address_); | 155 result = socket_->Bind(*bind_address_); |
| 147 if (result != OK) { | 156 if (result != OK) { |
| 148 socket_->Close(); | 157 socket_->Close(); |
| 149 return result; | 158 return result; |
| 150 } | 159 } |
| 151 } | 160 } |
| 152 } | 161 } |
| 153 } | 162 } |
| 154 | 163 |
| 164 if (socket_performance_watcher_ && notify_reset_socket_performance_watcher_) { |
| 165 socket_performance_watcher_->Reset(); |
| 166 } |
| 167 |
| 155 // |socket_| is owned by this class and the callback won't be run once | 168 // |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. | 169 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. |
| 157 return socket_->Connect(endpoint, | 170 return socket_->Connect(endpoint, |
| 158 base::Bind(&TCPClientSocket::DidCompleteConnect, | 171 base::Bind(&TCPClientSocket::DidCompleteConnect, |
| 159 base::Unretained(this))); | 172 base::Unretained(this))); |
| 160 } | 173 } |
| 161 | 174 |
| 162 int TCPClientSocket::DoConnectComplete(int result) { | 175 int TCPClientSocket::DoConnectComplete(int result) { |
| 163 if (result == OK) { | 176 if (result == OK) { |
| 164 use_history_.set_was_ever_connected(); | 177 use_history_.set_was_ever_connected(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 188 bind_address_.reset(); | 201 bind_address_.reset(); |
| 189 } | 202 } |
| 190 | 203 |
| 191 void TCPClientSocket::DoDisconnect() { | 204 void TCPClientSocket::DoDisconnect() { |
| 192 total_received_bytes_ = 0; | 205 total_received_bytes_ = 0; |
| 193 EmitTCPMetricsHistogramsOnDisconnect(); | 206 EmitTCPMetricsHistogramsOnDisconnect(); |
| 194 // If connecting or already connected, record that the socket has been | 207 // If connecting or already connected, record that the socket has been |
| 195 // disconnected. | 208 // disconnected. |
| 196 previously_disconnected_ = socket_->IsValid() && current_address_index_ >= 0; | 209 previously_disconnected_ = socket_->IsValid() && current_address_index_ >= 0; |
| 197 socket_->Close(); | 210 socket_->Close(); |
| 211 notify_reset_socket_performance_watcher_ = true; |
| 198 } | 212 } |
| 199 | 213 |
| 200 bool TCPClientSocket::IsConnected() const { | 214 bool TCPClientSocket::IsConnected() const { |
| 201 return socket_->IsConnected(); | 215 return socket_->IsConnected(); |
| 202 } | 216 } |
| 203 | 217 |
| 204 bool TCPClientSocket::IsConnectedAndIdle() const { | 218 bool TCPClientSocket::IsConnectedAndIdle() const { |
| 205 return socket_->IsConnectedAndIdle(); | 219 return socket_->IsConnectedAndIdle(); |
| 206 } | 220 } |
| 207 | 221 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 void TCPClientSocket::EmitTCPMetricsHistogramsOnDisconnect() { | 389 void TCPClientSocket::EmitTCPMetricsHistogramsOnDisconnect() { |
| 376 base::TimeDelta rtt; | 390 base::TimeDelta rtt; |
| 377 if (socket_->GetEstimatedRoundTripTime(&rtt)) { | 391 if (socket_->GetEstimatedRoundTripTime(&rtt)) { |
| 378 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TcpRtt.AtDisconnect", rtt, | 392 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TcpRtt.AtDisconnect", rtt, |
| 379 base::TimeDelta::FromMilliseconds(1), | 393 base::TimeDelta::FromMilliseconds(1), |
| 380 base::TimeDelta::FromMinutes(10), 100); | 394 base::TimeDelta::FromMinutes(10), 100); |
| 381 } | 395 } |
| 382 } | 396 } |
| 383 | 397 |
| 384 } // namespace net | 398 } // namespace net |
| OLD | NEW |