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 19 matching lines...) Expand all Loading... |
330 int TCPClientSocketLibevent::DoConnectComplete(int result) { | 332 int TCPClientSocketLibevent::DoConnectComplete(int result) { |
331 // Log the end of this attempt (and any OS error it threw). | 333 // Log the end of this attempt (and any OS error it threw). |
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 if (result == OK) { | 341 if (result == OK) { |
| 342 connect_time_micros_ = base::TimeTicks::Now() - connect_start_time_; |
340 write_socket_watcher_.StopWatchingFileDescriptor(); | 343 write_socket_watcher_.StopWatchingFileDescriptor(); |
341 use_history_.set_was_ever_connected(); | 344 use_history_.set_was_ever_connected(); |
342 return OK; // Done! | 345 return OK; // Done! |
343 } | 346 } |
344 | 347 |
345 // Close whatever partially connected socket we currently have. | 348 // Close whatever partially connected socket we currently have. |
346 DoDisconnect(); | 349 DoDisconnect(); |
347 | 350 |
348 // Try to fall back to the next address in the list. | 351 // Try to fall back to the next address in the list. |
349 if (current_ai_->ai_next) { | 352 if (current_ai_->ai_next) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 DCHECK(!waiting_connect()); | 435 DCHECK(!waiting_connect()); |
433 DCHECK(!read_callback_); | 436 DCHECK(!read_callback_); |
434 // Synchronous operation not supported | 437 // Synchronous operation not supported |
435 DCHECK(callback); | 438 DCHECK(callback); |
436 DCHECK_GT(buf_len, 0); | 439 DCHECK_GT(buf_len, 0); |
437 | 440 |
438 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 441 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
439 if (nread >= 0) { | 442 if (nread >= 0) { |
440 base::StatsCounter read_bytes("tcp.read_bytes"); | 443 base::StatsCounter read_bytes("tcp.read_bytes"); |
441 read_bytes.Add(nread); | 444 read_bytes.Add(nread); |
| 445 num_bytes_read_ += static_cast<int64>(nread); |
442 if (nread > 0) | 446 if (nread > 0) |
443 use_history_.set_was_used_to_convey_data(); | 447 use_history_.set_was_used_to_convey_data(); |
444 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, | 448 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, |
445 buf->data()); | 449 buf->data()); |
446 return nread; | 450 return nread; |
447 } | 451 } |
448 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 452 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
449 DVLOG(1) << "read failed, errno " << errno; | 453 DVLOG(1) << "read failed, errno " << errno; |
450 return MapSystemError(errno); | 454 return MapSystemError(errno); |
451 } | 455 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 void TCPClientSocketLibevent::DidCompleteRead() { | 630 void TCPClientSocketLibevent::DidCompleteRead() { |
627 int bytes_transferred; | 631 int bytes_transferred; |
628 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 632 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
629 read_buf_len_)); | 633 read_buf_len_)); |
630 | 634 |
631 int result; | 635 int result; |
632 if (bytes_transferred >= 0) { | 636 if (bytes_transferred >= 0) { |
633 result = bytes_transferred; | 637 result = bytes_transferred; |
634 base::StatsCounter read_bytes("tcp.read_bytes"); | 638 base::StatsCounter read_bytes("tcp.read_bytes"); |
635 read_bytes.Add(bytes_transferred); | 639 read_bytes.Add(bytes_transferred); |
| 640 num_bytes_read_ += static_cast<int64>(bytes_transferred); |
636 if (bytes_transferred > 0) | 641 if (bytes_transferred > 0) |
637 use_history_.set_was_used_to_convey_data(); | 642 use_history_.set_was_used_to_convey_data(); |
638 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, | 643 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, |
639 read_buf_->data()); | 644 read_buf_->data()); |
640 } else { | 645 } else { |
641 result = MapSystemError(errno); | 646 result = MapSystemError(errno); |
642 } | 647 } |
643 | 648 |
644 if (result != ERR_IO_PENDING) { | 649 if (result != ERR_IO_PENDING) { |
645 read_buf_ = NULL; | 650 read_buf_ = NULL; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 } | 720 } |
716 | 721 |
717 bool TCPClientSocketLibevent::WasEverUsed() const { | 722 bool TCPClientSocketLibevent::WasEverUsed() const { |
718 return use_history_.was_used_to_convey_data(); | 723 return use_history_.was_used_to_convey_data(); |
719 } | 724 } |
720 | 725 |
721 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { | 726 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { |
722 return use_tcp_fastopen_; | 727 return use_tcp_fastopen_; |
723 } | 728 } |
724 | 729 |
| 730 int64 TCPClientSocketLibevent::NumBytesRead() const { |
| 731 return num_bytes_read_; |
| 732 } |
| 733 |
| 734 base::TimeDelta TCPClientSocketLibevent::GetConnectTimeMicros() const { |
| 735 return connect_time_micros_; |
| 736 } |
| 737 |
725 } // namespace net | 738 } // namespace net |
OLD | NEW |