| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <netinet/tcp.h> | 8 #include <netinet/tcp.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 | 10 |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 base::Bind(&TCPSocketPosix::ReadCompleted, | 283 base::Bind(&TCPSocketPosix::ReadCompleted, |
| 284 // Grab a reference to |buf| so that ReadCompleted() can still | 284 // Grab a reference to |buf| so that ReadCompleted() can still |
| 285 // use it when Read() completes, as otherwise, this transfers | 285 // use it when Read() completes, as otherwise, this transfers |
| 286 // ownership of buf to socket. | 286 // ownership of buf to socket. |
| 287 base::Unretained(this), make_scoped_refptr(buf), callback)); | 287 base::Unretained(this), make_scoped_refptr(buf), callback)); |
| 288 if (rv != ERR_IO_PENDING) | 288 if (rv != ERR_IO_PENDING) |
| 289 rv = HandleReadCompleted(buf, rv); | 289 rv = HandleReadCompleted(buf, rv); |
| 290 return rv; | 290 return rv; |
| 291 } | 291 } |
| 292 | 292 |
| 293 int TCPSocketPosix::ReadIfReady(IOBuffer* buf, |
| 294 int buf_len, |
| 295 const CompletionCallback& callback) { |
| 296 DCHECK(socket_); |
| 297 DCHECK(!callback.is_null()); |
| 298 |
| 299 int rv = socket_->ReadIfReady( |
| 300 buf, buf_len, base::Bind(&TCPSocketPosix::ReadIfReadyCompleted, |
| 301 base::Unretained(this), callback)); |
| 302 if (rv != ERR_IO_PENDING) |
| 303 rv = HandleReadCompleted(buf, rv); |
| 304 return rv; |
| 305 } |
| 306 |
| 293 int TCPSocketPosix::Write(IOBuffer* buf, | 307 int TCPSocketPosix::Write(IOBuffer* buf, |
| 294 int buf_len, | 308 int buf_len, |
| 295 const CompletionCallback& callback) { | 309 const CompletionCallback& callback) { |
| 296 DCHECK(socket_); | 310 DCHECK(socket_); |
| 297 DCHECK(!callback.is_null()); | 311 DCHECK(!callback.is_null()); |
| 298 | 312 |
| 299 CompletionCallback write_callback = | 313 CompletionCallback write_callback = |
| 300 base::Bind(&TCPSocketPosix::WriteCompleted, | 314 base::Bind(&TCPSocketPosix::WriteCompleted, |
| 301 // Grab a reference to |buf| so that WriteCompleted() can still | 315 // Grab a reference to |buf| so that WriteCompleted() can still |
| 302 // use it when Write() completes, as otherwise, this transfers | 316 // use it when Write() completes, as otherwise, this transfers |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 | 595 |
| 582 net_log_.EndEvent( | 596 net_log_.EndEvent( |
| 583 NetLogEventType::TCP_CONNECT, | 597 NetLogEventType::TCP_CONNECT, |
| 584 CreateNetLogSourceAddressCallback(storage.addr, storage.addr_len)); | 598 CreateNetLogSourceAddressCallback(storage.addr, storage.addr_len)); |
| 585 } | 599 } |
| 586 | 600 |
| 587 void TCPSocketPosix::ReadCompleted(const scoped_refptr<IOBuffer>& buf, | 601 void TCPSocketPosix::ReadCompleted(const scoped_refptr<IOBuffer>& buf, |
| 588 const CompletionCallback& callback, | 602 const CompletionCallback& callback, |
| 589 int rv) { | 603 int rv) { |
| 590 DCHECK_NE(ERR_IO_PENDING, rv); | 604 DCHECK_NE(ERR_IO_PENDING, rv); |
| 605 |
| 591 callback.Run(HandleReadCompleted(buf.get(), rv)); | 606 callback.Run(HandleReadCompleted(buf.get(), rv)); |
| 592 } | 607 } |
| 593 | 608 |
| 609 void TCPSocketPosix::ReadIfReadyCompleted(const CompletionCallback& callback, |
| 610 int rv) { |
| 611 DCHECK_NE(ERR_IO_PENDING, rv); |
| 612 DCHECK_GE(OK, rv); |
| 613 |
| 614 HandleReadCompletedHelper(rv); |
| 615 callback.Run(rv); |
| 616 } |
| 617 |
| 594 int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) { | 618 int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) { |
| 619 HandleReadCompletedHelper(rv); |
| 620 |
| 621 if (rv < 0) |
| 622 return rv; |
| 623 |
| 624 // Notify the watcher only if at least 1 byte was read. |
| 625 if (rv > 0) |
| 626 NotifySocketPerformanceWatcher(); |
| 627 |
| 628 net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, rv, |
| 629 buf->data()); |
| 630 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv); |
| 631 |
| 632 return rv; |
| 633 } |
| 634 |
| 635 void TCPSocketPosix::HandleReadCompletedHelper(int rv) { |
| 595 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | 636 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { |
| 596 // A TCP FastOpen connect-with-write was attempted. This read was a | 637 // A TCP FastOpen connect-with-write was attempted. This read was a |
| 597 // subsequent read, which either succeeded or failed. If the read | 638 // subsequent read, which either succeeded or failed. If the read |
| 598 // succeeded, the socket is considered connected via TCP FastOpen. | 639 // succeeded, the socket is considered connected via TCP FastOpen. |
| 599 // If the read failed, TCP FastOpen is (conservatively) turned off for all | 640 // If the read failed, TCP FastOpen is (conservatively) turned off for all |
| 600 // subsequent connections. TCP FastOpen status is recorded in both cases. | 641 // subsequent connections. TCP FastOpen status is recorded in both cases. |
| 601 // TODO (jri): This currently results in conservative behavior, where TCP | 642 // TODO (jri): This currently results in conservative behavior, where TCP |
| 602 // FastOpen is turned off on _any_ error. Implement optimizations, | 643 // FastOpen is turned off on _any_ error. Implement optimizations, |
| 603 // such as turning off TCP FastOpen on more specific errors, and | 644 // such as turning off TCP FastOpen on more specific errors, and |
| 604 // re-attempting TCP FastOpen after a certain amount of time has passed. | 645 // re-attempting TCP FastOpen after a certain amount of time has passed. |
| 605 if (rv >= 0) | 646 if (rv >= 0) |
| 606 tcp_fastopen_connected_ = true; | 647 tcp_fastopen_connected_ = true; |
| 607 else | 648 else |
| 608 g_tcp_fastopen_has_failed = true; | 649 g_tcp_fastopen_has_failed = true; |
| 609 UpdateTCPFastOpenStatusAfterRead(); | 650 UpdateTCPFastOpenStatusAfterRead(); |
| 610 } | 651 } |
| 611 | 652 |
| 612 if (rv < 0) { | 653 if (rv < 0) { |
| 613 net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR, | 654 net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR, |
| 614 CreateNetLogSocketErrorCallback(rv, errno)); | 655 CreateNetLogSocketErrorCallback(rv, errno)); |
| 615 return rv; | |
| 616 } | 656 } |
| 617 | |
| 618 // Notify the watcher only if at least 1 byte was read. | |
| 619 if (rv > 0) | |
| 620 NotifySocketPerformanceWatcher(); | |
| 621 | |
| 622 net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, rv, | |
| 623 buf->data()); | |
| 624 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv); | |
| 625 | |
| 626 return rv; | |
| 627 } | 657 } |
| 628 | 658 |
| 629 void TCPSocketPosix::WriteCompleted(const scoped_refptr<IOBuffer>& buf, | 659 void TCPSocketPosix::WriteCompleted(const scoped_refptr<IOBuffer>& buf, |
| 630 const CompletionCallback& callback, | 660 const CompletionCallback& callback, |
| 631 int rv) { | 661 int rv) { |
| 632 DCHECK_NE(ERR_IO_PENDING, rv); | 662 DCHECK_NE(ERR_IO_PENDING, rv); |
| 633 callback.Run(HandleWriteCompleted(buf.get(), rv)); | 663 callback.Run(HandleWriteCompleted(buf.get(), rv)); |
| 634 } | 664 } |
| 635 | 665 |
| 636 int TCPSocketPosix::HandleWriteCompleted(IOBuffer* buf, int rv) { | 666 int TCPSocketPosix::HandleWriteCompleted(IOBuffer* buf, int rv) { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 if (info.tcpi_rtt > 0) { | 840 if (info.tcpi_rtt > 0) { |
| 811 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); | 841 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); |
| 812 return true; | 842 return true; |
| 813 } | 843 } |
| 814 } | 844 } |
| 815 #endif // defined(TCP_INFO) | 845 #endif // defined(TCP_INFO) |
| 816 return false; | 846 return false; |
| 817 } | 847 } |
| 818 | 848 |
| 819 } // namespace net | 849 } // namespace net |
| OLD | NEW |