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