| 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 212 |
| 213 next_connect_state_ = CONNECT_STATE_CONNECT_COMPLETE; | 213 next_connect_state_ = CONNECT_STATE_CONNECT_COMPLETE; |
| 214 | 214 |
| 215 // Create a non-blocking socket. | 215 // Create a non-blocking socket. |
| 216 connect_os_error_ = CreateSocket(current_ai_); | 216 connect_os_error_ = CreateSocket(current_ai_); |
| 217 if (connect_os_error_) | 217 if (connect_os_error_) |
| 218 return MapSystemError(connect_os_error_); | 218 return MapSystemError(connect_os_error_); |
| 219 | 219 |
| 220 // Connect the socket. | 220 // Connect the socket. |
| 221 if (!use_tcp_fastopen_) { | 221 if (!use_tcp_fastopen_) { |
| 222 connect_start_time_ = base::Time::Now(); |
| 222 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, | 223 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, |
| 223 static_cast<int>(current_ai_->ai_addrlen)))) { | 224 static_cast<int>(current_ai_->ai_addrlen)))) { |
| 224 // Connected without waiting! | 225 // Connected without waiting! |
| 225 return OK; | 226 return OK; |
| 226 } | 227 } |
| 227 } else { | 228 } else { |
| 228 // With TCP FastOpen, we pretend that the socket is connected. | 229 // With TCP FastOpen, we pretend that the socket is connected. |
| 229 DCHECK(!tcp_fastopen_connected_); | 230 DCHECK(!tcp_fastopen_connected_); |
| 230 return OK; | 231 return OK; |
| 231 } | 232 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 253 int os_error = connect_os_error_; | 254 int os_error = connect_os_error_; |
| 254 connect_os_error_ = 0; | 255 connect_os_error_ = 0; |
| 255 scoped_refptr<NetLog::EventParameters> params; | 256 scoped_refptr<NetLog::EventParameters> params; |
| 256 if (result != OK) | 257 if (result != OK) |
| 257 params = new NetLogIntegerParameter("os_error", os_error); | 258 params = new NetLogIntegerParameter("os_error", os_error); |
| 258 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); | 259 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); |
| 259 | 260 |
| 260 write_socket_watcher_.StopWatchingFileDescriptor(); | 261 write_socket_watcher_.StopWatchingFileDescriptor(); |
| 261 | 262 |
| 262 if (result == OK) { | 263 if (result == OK) { |
| 264 rtt_ms_ = static_cast<double>((base::Time::Now() - |
| 265 connect_start_time_).InMicroseconds()) |
| 266 / 1000.0; |
| 263 use_history_.set_was_ever_connected(); | 267 use_history_.set_was_ever_connected(); |
| 264 return OK; // Done! | 268 return OK; // Done! |
| 265 } | 269 } |
| 266 | 270 |
| 267 // Close whatever partially connected socket we currently have. | 271 // Close whatever partially connected socket we currently have. |
| 268 DoDisconnect(); | 272 DoDisconnect(); |
| 269 | 273 |
| 270 // Try to fall back to the next address in the list. | 274 // Try to fall back to the next address in the list. |
| 271 if (current_ai_->ai_next) { | 275 if (current_ai_->ai_next) { |
| 272 next_connect_state_ = CONNECT_STATE_CONNECT; | 276 next_connect_state_ = CONNECT_STATE_CONNECT; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 DCHECK(!waiting_connect()); | 346 DCHECK(!waiting_connect()); |
| 343 DCHECK(!read_callback_); | 347 DCHECK(!read_callback_); |
| 344 // Synchronous operation not supported | 348 // Synchronous operation not supported |
| 345 DCHECK(callback); | 349 DCHECK(callback); |
| 346 DCHECK_GT(buf_len, 0); | 350 DCHECK_GT(buf_len, 0); |
| 347 | 351 |
| 348 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 352 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
| 349 if (nread >= 0) { | 353 if (nread >= 0) { |
| 350 base::StatsCounter read_bytes("tcp.read_bytes"); | 354 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 351 read_bytes.Add(nread); | 355 read_bytes.Add(nread); |
| 356 num_bytes_read_ += nread; |
| 352 if (nread > 0) | 357 if (nread > 0) |
| 353 use_history_.set_was_used_to_convey_data(); | 358 use_history_.set_was_used_to_convey_data(); |
| 354 LogByteTransfer( | 359 LogByteTransfer( |
| 355 net_log_, NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, buf->data()); | 360 net_log_, NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, buf->data()); |
| 356 return nread; | 361 return nread; |
| 357 } | 362 } |
| 358 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 363 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 359 DVLOG(1) << "read failed, errno " << errno; | 364 DVLOG(1) << "read failed, errno " << errno; |
| 360 return MapSystemError(errno); | 365 return MapSystemError(errno); |
| 361 } | 366 } |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 | 655 |
| 651 bool TCPClientSocketLibevent::WasEverUsed() const { | 656 bool TCPClientSocketLibevent::WasEverUsed() const { |
| 652 return use_history_.was_used_to_convey_data(); | 657 return use_history_.was_used_to_convey_data(); |
| 653 } | 658 } |
| 654 | 659 |
| 655 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { | 660 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { |
| 656 return use_tcp_fastopen_; | 661 return use_tcp_fastopen_; |
| 657 } | 662 } |
| 658 | 663 |
| 659 } // namespace net | 664 } // namespace net |
| OLD | NEW |