Chromium Code Reviews| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 current_ai_(NULL), | 135 current_ai_(NULL), |
| 136 read_watcher_(this), | 136 read_watcher_(this), |
| 137 write_watcher_(this), | 137 write_watcher_(this), |
| 138 read_callback_(NULL), | 138 read_callback_(NULL), |
| 139 write_callback_(NULL), | 139 write_callback_(NULL), |
| 140 next_connect_state_(CONNECT_STATE_NONE), | 140 next_connect_state_(CONNECT_STATE_NONE), |
| 141 connect_os_error_(0), | 141 connect_os_error_(0), |
| 142 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), | 142 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)), |
| 143 previously_disconnected_(false), | 143 previously_disconnected_(false), |
| 144 use_tcp_fastopen_(false), | 144 use_tcp_fastopen_(false), |
| 145 tcp_fastopen_connected_(false) { | 145 tcp_fastopen_connected_(false), |
| 146 connect_start_time_(), | |
|
willchan no longer on Chromium
2011/06/09 15:08:54
No need to explicitly invoke the empty constructor
Gagan
2011/06/09 19:49:26
Done.
| |
| 147 num_bytes_read_(0), | |
| 148 rtt_ms_(0) { | |
| 146 scoped_refptr<NetLog::EventParameters> params; | 149 scoped_refptr<NetLog::EventParameters> params; |
| 147 if (source.is_valid()) | 150 if (source.is_valid()) |
| 148 params = new NetLogSourceParameter("source_dependency", source); | 151 params = new NetLogSourceParameter("source_dependency", source); |
| 149 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 152 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
| 150 | 153 |
| 151 if (is_tcp_fastopen_enabled()) | 154 if (is_tcp_fastopen_enabled()) |
| 152 use_tcp_fastopen_ = true; | 155 use_tcp_fastopen_ = true; |
| 153 } | 156 } |
| 154 | 157 |
| 155 TCPClientSocketLibevent::~TCPClientSocketLibevent() { | 158 TCPClientSocketLibevent::~TCPClientSocketLibevent() { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 size_t addr_len = sizeof(addr_storage); | 299 size_t addr_len = sizeof(addr_storage); |
| 297 if (!bind_address_->ToSockAddr(addr, &addr_len)) | 300 if (!bind_address_->ToSockAddr(addr, &addr_len)) |
| 298 return ERR_INVALID_ARGUMENT; | 301 return ERR_INVALID_ARGUMENT; |
| 299 if (HANDLE_EINTR(bind(socket_, addr, addr_len))) | 302 if (HANDLE_EINTR(bind(socket_, addr, addr_len))) |
| 300 return MapSystemError(errno); | 303 return MapSystemError(errno); |
| 301 } | 304 } |
| 302 } | 305 } |
| 303 | 306 |
| 304 // Connect the socket. | 307 // Connect the socket. |
| 305 if (!use_tcp_fastopen_) { | 308 if (!use_tcp_fastopen_) { |
| 309 connect_start_time_ = base::Time::Now(); | |
| 306 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, | 310 if (!HANDLE_EINTR(connect(socket_, current_ai_->ai_addr, |
| 307 static_cast<int>(current_ai_->ai_addrlen)))) { | 311 static_cast<int>(current_ai_->ai_addrlen)))) { |
| 308 // Connected without waiting! | 312 // Connected without waiting! |
| 309 return OK; | 313 return OK; |
| 310 } | 314 } |
| 311 } else { | 315 } else { |
| 312 // With TCP FastOpen, we pretend that the socket is connected. | 316 // With TCP FastOpen, we pretend that the socket is connected. |
| 313 DCHECK(!tcp_fastopen_connected_); | 317 DCHECK(!tcp_fastopen_connected_); |
| 314 return OK; | 318 return OK; |
| 315 } | 319 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 337 int os_error = connect_os_error_; | 341 int os_error = connect_os_error_; |
| 338 connect_os_error_ = 0; | 342 connect_os_error_ = 0; |
| 339 scoped_refptr<NetLog::EventParameters> params; | 343 scoped_refptr<NetLog::EventParameters> params; |
| 340 if (result != OK) | 344 if (result != OK) |
| 341 params = new NetLogIntegerParameter("os_error", os_error); | 345 params = new NetLogIntegerParameter("os_error", os_error); |
| 342 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); | 346 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, params); |
| 343 | 347 |
| 344 write_socket_watcher_.StopWatchingFileDescriptor(); | 348 write_socket_watcher_.StopWatchingFileDescriptor(); |
| 345 | 349 |
| 346 if (result == OK) { | 350 if (result == OK) { |
| 351 rtt_ms_ = static_cast<double>((base::Time::Now() - | |
|
willchan no longer on Chromium
2011/06/09 15:08:54
Why is this kept as a double? Seems better to use
Gagan
2011/06/09 19:49:26
Initially i chose double because i didn't want to
| |
| 352 connect_start_time_).InMicroseconds()) | |
| 353 / 1000.0; | |
| 347 use_history_.set_was_ever_connected(); | 354 use_history_.set_was_ever_connected(); |
| 348 return OK; // Done! | 355 return OK; // Done! |
| 349 } | 356 } |
| 350 | 357 |
| 351 // Close whatever partially connected socket we currently have. | 358 // Close whatever partially connected socket we currently have. |
| 352 DoDisconnect(); | 359 DoDisconnect(); |
| 353 | 360 |
| 354 // Try to fall back to the next address in the list. | 361 // Try to fall back to the next address in the list. |
| 355 if (current_ai_->ai_next) { | 362 if (current_ai_->ai_next) { |
| 356 next_connect_state_ = CONNECT_STATE_CONNECT; | 363 next_connect_state_ = CONNECT_STATE_CONNECT; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 CompletionCallback* callback) { | 430 CompletionCallback* callback) { |
| 424 DCHECK(CalledOnValidThread()); | 431 DCHECK(CalledOnValidThread()); |
| 425 DCHECK_NE(kInvalidSocket, socket_); | 432 DCHECK_NE(kInvalidSocket, socket_); |
| 426 DCHECK(!waiting_connect()); | 433 DCHECK(!waiting_connect()); |
| 427 DCHECK(!read_callback_); | 434 DCHECK(!read_callback_); |
| 428 // Synchronous operation not supported | 435 // Synchronous operation not supported |
| 429 DCHECK(callback); | 436 DCHECK(callback); |
| 430 DCHECK_GT(buf_len, 0); | 437 DCHECK_GT(buf_len, 0); |
| 431 | 438 |
| 432 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 439 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
| 440 LOG(ERROR) << "this = " << (int*) this << "\tnread = " << nread; | |
|
Mike Belshe
2011/06/09 16:08:52
nit: please remove the debugging lines.
Gagan
2011/06/09 19:49:26
Will remove in the last round.
For now, please acc
Gagan
2011/06/14 18:25:02
Removed.
| |
| 433 if (nread >= 0) { | 441 if (nread >= 0) { |
| 434 base::StatsCounter read_bytes("tcp.read_bytes"); | 442 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 435 read_bytes.Add(nread); | 443 read_bytes.Add(nread); |
| 444 num_bytes_read_ += static_cast<int64>(nread); | |
| 445 LOG(ERROR) << "this = " << (int*) this << "\tRead " << nread; | |
| 436 if (nread > 0) | 446 if (nread > 0) |
| 437 use_history_.set_was_used_to_convey_data(); | 447 use_history_.set_was_used_to_convey_data(); |
| 438 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, | 448 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, |
| 439 buf->data()); | 449 buf->data()); |
| 440 return nread; | 450 return nread; |
| 441 } | 451 } |
| 442 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 452 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 443 DVLOG(1) << "read failed, errno " << errno; | 453 DVLOG(1) << "read failed, errno " << errno; |
| 444 return MapSystemError(errno); | 454 return MapSystemError(errno); |
| 445 } | 455 } |
| 446 | 456 |
| 447 if (!MessageLoopForIO::current()->WatchFileDescriptor( | 457 if (!MessageLoopForIO::current()->WatchFileDescriptor( |
| 448 socket_, true, MessageLoopForIO::WATCH_READ, | 458 socket_, true, MessageLoopForIO::WATCH_READ, |
| 449 &read_socket_watcher_, &read_watcher_)) { | 459 &read_socket_watcher_, &read_watcher_)) { |
| 450 DVLOG(1) << "WatchFileDescriptor failed on read, errno " << errno; | 460 DVLOG(1) << "WatchFileDescriptor failed on read, errno " << errno; |
| 451 return MapSystemError(errno); | 461 return MapSystemError(errno); |
| 452 } | 462 } |
| 453 | 463 |
| 464 LOG(ERROR) << "this = " << (int*) this << "\tread_buf_len_ = " << read_buf_len _; | |
| 454 read_buf_ = buf; | 465 read_buf_ = buf; |
| 455 read_buf_len_ = buf_len; | 466 read_buf_len_ = buf_len; |
| 456 read_callback_ = callback; | 467 read_callback_ = callback; |
| 457 return ERR_IO_PENDING; | 468 return ERR_IO_PENDING; |
| 458 } | 469 } |
| 459 | 470 |
| 460 int TCPClientSocketLibevent::Write(IOBuffer* buf, | 471 int TCPClientSocketLibevent::Write(IOBuffer* buf, |
| 461 int buf_len, | 472 int buf_len, |
| 462 CompletionCallback* callback) { | 473 CompletionCallback* callback) { |
| 463 DCHECK(CalledOnValidThread()); | 474 DCHECK(CalledOnValidThread()); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 620 void TCPClientSocketLibevent::DidCompleteRead() { | 631 void TCPClientSocketLibevent::DidCompleteRead() { |
| 621 int bytes_transferred; | 632 int bytes_transferred; |
| 622 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 633 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), |
| 623 read_buf_len_)); | 634 read_buf_len_)); |
| 624 | 635 |
| 625 int result; | 636 int result; |
| 626 if (bytes_transferred >= 0) { | 637 if (bytes_transferred >= 0) { |
| 627 result = bytes_transferred; | 638 result = bytes_transferred; |
| 628 base::StatsCounter read_bytes("tcp.read_bytes"); | 639 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 629 read_bytes.Add(bytes_transferred); | 640 read_bytes.Add(bytes_transferred); |
| 641 num_bytes_read_ += static_cast<int64>(bytes_transferred); | |
| 642 LOG(ERROR) << "this = " << (int*) this << "\tRead " << bytes_transferred; | |
| 630 if (bytes_transferred > 0) | 643 if (bytes_transferred > 0) |
| 631 use_history_.set_was_used_to_convey_data(); | 644 use_history_.set_was_used_to_convey_data(); |
| 632 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, | 645 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, |
| 633 read_buf_->data()); | 646 read_buf_->data()); |
| 634 } else { | 647 } else { |
| 635 result = MapSystemError(errno); | 648 result = MapSystemError(errno); |
| 636 } | 649 } |
| 637 | 650 |
| 638 if (result != ERR_IO_PENDING) { | 651 if (result != ERR_IO_PENDING) { |
| 639 read_buf_ = NULL; | 652 read_buf_ = NULL; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 710 | 723 |
| 711 bool TCPClientSocketLibevent::WasEverUsed() const { | 724 bool TCPClientSocketLibevent::WasEverUsed() const { |
| 712 return use_history_.was_used_to_convey_data(); | 725 return use_history_.was_used_to_convey_data(); |
| 713 } | 726 } |
| 714 | 727 |
| 715 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { | 728 bool TCPClientSocketLibevent::UsingTCPFastOpen() const { |
| 716 return use_tcp_fastopen_; | 729 return use_tcp_fastopen_; |
| 717 } | 730 } |
| 718 | 731 |
| 719 } // namespace net | 732 } // namespace net |
| OLD | NEW |