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) { | |
142 scoped_refptr<NetLog::EventParameters> params; | 141 scoped_refptr<NetLog::EventParameters> params; |
143 if (source.is_valid()) | 142 if (source.is_valid()) |
144 params = new NetLogSourceParameter("source_dependency", source); | 143 params = new NetLogSourceParameter("source_dependency", source); |
145 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 144 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
146 | 145 |
147 if (is_tcp_fastopen_enabled()) | 146 if (is_tcp_fastopen_enabled()) |
148 use_tcp_fastopen_ = true; | 147 use_tcp_fastopen_ = true; |
149 } | 148 } |
150 | 149 |
151 TCPClientSocketLibevent::~TCPClientSocketLibevent() { | 150 TCPClientSocketLibevent::~TCPClientSocketLibevent() { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 size_t addr_len = sizeof(addr_storage); | 291 size_t addr_len = sizeof(addr_storage); |
293 if (!bind_address_->ToSockAddr(addr, &addr_len)) | 292 if (!bind_address_->ToSockAddr(addr, &addr_len)) |
294 return ERR_INVALID_ARGUMENT; | 293 return ERR_INVALID_ARGUMENT; |
295 if (HANDLE_EINTR(bind(socket_, addr, addr_len))) | 294 if (HANDLE_EINTR(bind(socket_, addr, addr_len))) |
296 return MapSystemError(errno); | 295 return MapSystemError(errno); |
297 } | 296 } |
298 } | 297 } |
299 | 298 |
300 // Connect the socket. | 299 // Connect the socket. |
301 if (!use_tcp_fastopen_) { | 300 if (!use_tcp_fastopen_) { |
302 connect_start_time_ = base::TimeTicks::Now(); | |
303 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, | 301 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, |
304 static_cast<int>(current_ai_->ai_addrlen)))) { | 302 static_cast<int>(current_ai_->ai_addrlen)))) { |
305 // Connected without waiting! | 303 // Connected without waiting! |
306 return OK; | 304 return OK; |
307 } | 305 } |
308 } else { | 306 } else { |
309 // With TCP FastOpen, we pretend that the socket is connected. | 307 // With TCP FastOpen, we pretend that the socket is connected. |
310 DCHECK(!tcp_fastopen_connected_); | 308 DCHECK(!tcp_fastopen_connected_); |
311 return OK; | 309 return OK; |
312 } | 310 } |
(...skipping 21 matching lines...) Expand all Loading... |
334 int os_error = connect_os_error_; | 332 int os_error = connect_os_error_; |
335 connect_os_error_ = 0; | 333 connect_os_error_ = 0; |
336 scoped_refptr<NetLog::EventParameters> params; | 334 scoped_refptr<NetLog::EventParameters> params; |
337 if (result != OK) | 335 if (result != OK) |
338 params = new NetLogIntegerParameter("os_error", os_error); | 336 params = new NetLogIntegerParameter("os_error", os_error); |
339 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); | 337 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); |
340 | 338 |
341 write_socket_watcher_.StopWatchingFileDescriptor(); | 339 write_socket_watcher_.StopWatchingFileDescriptor(); |
342 | 340 |
343 if (result == OK) { | 341 if (result == OK) { |
344 connect_time_micros_ = base::TimeTicks::Now() - connect_start_time_; | |
345 use_history_.set_was_ever_connected(); | 342 use_history_.set_was_ever_connected(); |
346 return OK; // Done! | 343 return OK; // Done! |
347 } | 344 } |
348 | 345 |
349 // Close whatever partially connected socket we currently have. | 346 // Close whatever partially connected socket we currently have. |
350 DoDisconnect(); | 347 DoDisconnect(); |
351 | 348 |
352 // Try to fall back to the next address in the list. | 349 // Try to fall back to the next address in the list. |
353 if (current_ai_->ai_next) { | 350 if (current_ai_->ai_next) { |
354 next_connect_state_ = CONNECT_STATE_CONNECT; | 351 next_connect_state_ = CONNECT_STATE_CONNECT; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 DCHECK(!waiting_connect()); | 433 DCHECK(!waiting_connect()); |
437 DCHECK(!read_callback_); | 434 DCHECK(!read_callback_); |
438 // Synchronous operation not supported | 435 // Synchronous operation not supported |
439 DCHECK(callback); | 436 DCHECK(callback); |
440 DCHECK_GT(buf_len, 0); | 437 DCHECK_GT(buf_len, 0); |
441 | 438 |
442 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 439 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
443 if (nread >= 0) { | 440 if (nread >= 0) { |
444 base::StatsCounter read_bytes("tcp.read_bytes"); | 441 base::StatsCounter read_bytes("tcp.read_bytes"); |
445 read_bytes.Add(nread); | 442 read_bytes.Add(nread); |
446 num_bytes_read_ += static_cast<int64>(nread); | |
447 if (nread > 0) | 443 if (nread > 0) |
448 use_history_.set_was_used_to_convey_data(); | 444 use_history_.set_was_used_to_convey_data(); |
449 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, | 445 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, |
450 buf->data()); | 446 buf->data()); |
451 return nread; | 447 return nread; |
452 } | 448 } |
453 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 449 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
454 DVLOG(1) << "read failed, errno " << errno; | 450 DVLOG(1) << "read failed, errno " << errno; |
455 return MapSystemError(errno); | 451 return MapSystemError(errno); |
456 } | 452 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 void TCPClientSocketLibevent::DidCompleteRead() { | 627 void TCPClientSocketLibevent::DidCompleteRead() { |
632 int bytes_transferred; | 628 int bytes_transferred; |
633 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 629 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
634 read_buf_len_)); | 630 read_buf_len_)); |
635 | 631 |
636 int result; | 632 int result; |
637 if (bytes_transferred >= 0) { | 633 if (bytes_transferred >= 0) { |
638 result = bytes_transferred; | 634 result = bytes_transferred; |
639 base::StatsCounter read_bytes("tcp.read_bytes"); | 635 base::StatsCounter read_bytes("tcp.read_bytes"); |
640 read_bytes.Add(bytes_transferred); | 636 read_bytes.Add(bytes_transferred); |
641 num_bytes_read_ += static_cast<int64>(bytes_transferred); | |
642 if (bytes_transferred > 0) | 637 if (bytes_transferred > 0) |
643 use_history_.set_was_used_to_convey_data(); | 638 use_history_.set_was_used_to_convey_data(); |
644 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, | 639 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, |
645 read_buf_->data()); | 640 read_buf_->data()); |
646 } else { | 641 } else { |
647 result = MapSystemError(errno); | 642 result = MapSystemError(errno); |
648 } | 643 } |
649 | 644 |
650 if (result != ERR_IO_PENDING) { | 645 if (result != ERR_IO_PENDING) { |
651 read_buf_ = NULL; | 646 read_buf_ = NULL; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 } | 716 } |
722 | 717 |
723 bool TCPClientSocketLibevent::WasEverUsed() const { | 718 bool TCPClientSocketLibevent::WasEverUsed() const { |
724 return use_history_.was_used_to_convey_data(); | 719 return use_history_.was_used_to_convey_data(); |
725 } | 720 } |
726 | 721 |
727 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { | 722 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { |
728 return use_tcp_fastopen_; | 723 return use_tcp_fastopen_; |
729 } | 724 } |
730 | 725 |
731 int64 TCPClientSocketLibevent::NumBytesRead() const { | |
732 return num_bytes_read_; | |
733 } | |
734 | |
735 base::TimeDelta TCPClientSocketLibevent::GetConnectTimeMicros() const { | |
736 return connect_time_micros_; | |
737 } | |
738 | |
739 } // namespace net | 726 } // namespace net |
OLD | NEW |