Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: net/socket/tcp_socket_posix.cc

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Rebased Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/tcp_socket_posix.h ('k') | net/socket/tcp_socket_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 base::Bind(&TCPSocketPosix::ReadCompleted, 286 base::Bind(&TCPSocketPosix::ReadCompleted,
287 // Grab a reference to |buf| so that ReadCompleted() can still 287 // Grab a reference to |buf| so that ReadCompleted() can still
288 // use it when Read() completes, as otherwise, this transfers 288 // use it when Read() completes, as otherwise, this transfers
289 // ownership of buf to socket. 289 // ownership of buf to socket.
290 base::Unretained(this), make_scoped_refptr(buf), callback)); 290 base::Unretained(this), make_scoped_refptr(buf), callback));
291 if (rv != ERR_IO_PENDING) 291 if (rv != ERR_IO_PENDING)
292 rv = HandleReadCompleted(buf, rv); 292 rv = HandleReadCompleted(buf, rv);
293 return rv; 293 return rv;
294 } 294 }
295 295
296 int TCPSocketPosix::ReadIfReady(IOBuffer* buf,
297 int buf_len,
298 const CompletionCallback& callback) {
299 DCHECK(socket_);
300 DCHECK(!callback.is_null());
301
302 int rv =
303 socket_->ReadIfReady(buf, buf_len,
304 base::Bind(&TCPSocketPosix::ReadIfReadyCompleted,
305 base::Unretained(this), callback));
306 if (rv != ERR_IO_PENDING)
307 rv = HandleReadCompleted(buf, rv);
308 return rv;
309 }
310
296 int TCPSocketPosix::Write(IOBuffer* buf, 311 int TCPSocketPosix::Write(IOBuffer* buf,
297 int buf_len, 312 int buf_len,
298 const CompletionCallback& callback) { 313 const CompletionCallback& callback) {
299 DCHECK(socket_); 314 DCHECK(socket_);
300 DCHECK(!callback.is_null()); 315 DCHECK(!callback.is_null());
301 316
302 CompletionCallback write_callback = 317 CompletionCallback write_callback =
303 base::Bind(&TCPSocketPosix::WriteCompleted, 318 base::Bind(&TCPSocketPosix::WriteCompleted,
304 // Grab a reference to |buf| so that WriteCompleted() can still 319 // Grab a reference to |buf| so that WriteCompleted() can still
305 // use it when Write() completes, as otherwise, this transfers 320 // use it when Write() completes, as otherwise, this transfers
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 594
580 net_log_.EndEvent( 595 net_log_.EndEvent(
581 NetLogEventType::TCP_CONNECT, 596 NetLogEventType::TCP_CONNECT,
582 CreateNetLogSourceAddressCallback(storage.addr, storage.addr_len)); 597 CreateNetLogSourceAddressCallback(storage.addr, storage.addr_len));
583 } 598 }
584 599
585 void TCPSocketPosix::ReadCompleted(const scoped_refptr<IOBuffer>& buf, 600 void TCPSocketPosix::ReadCompleted(const scoped_refptr<IOBuffer>& buf,
586 const CompletionCallback& callback, 601 const CompletionCallback& callback,
587 int rv) { 602 int rv) {
588 DCHECK_NE(ERR_IO_PENDING, rv); 603 DCHECK_NE(ERR_IO_PENDING, rv);
604
589 callback.Run(HandleReadCompleted(buf.get(), rv)); 605 callback.Run(HandleReadCompleted(buf.get(), rv));
590 } 606 }
591 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
592 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) {
593 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { 635 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) {
594 // 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
595 // subsequent read, which either succeeded or failed. If the read 637 // subsequent read, which either succeeded or failed. If the read
596 // succeeded, the socket is considered connected via TCP FastOpen. 638 // succeeded, the socket is considered connected via TCP FastOpen.
597 // 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
598 // subsequent connections. TCP FastOpen status is recorded in both cases. 640 // subsequent connections. TCP FastOpen status is recorded in both cases.
599 // TODO (jri): This currently results in conservative behavior, where TCP 641 // TODO (jri): This currently results in conservative behavior, where TCP
600 // FastOpen is turned off on _any_ error. Implement optimizations, 642 // FastOpen is turned off on _any_ error. Implement optimizations,
601 // such as turning off TCP FastOpen on more specific errors, and 643 // such as turning off TCP FastOpen on more specific errors, and
602 // 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.
603 if (rv >= 0) 645 if (rv >= 0)
604 tcp_fastopen_connected_ = true; 646 tcp_fastopen_connected_ = true;
605 else 647 else
606 g_tcp_fastopen_has_failed = true; 648 g_tcp_fastopen_has_failed = true;
607 UpdateTCPFastOpenStatusAfterRead(); 649 UpdateTCPFastOpenStatusAfterRead();
608 } 650 }
609 651
610 if (rv < 0) { 652 if (rv < 0) {
611 net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR, 653 net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR,
612 CreateNetLogSocketErrorCallback(rv, errno)); 654 CreateNetLogSocketErrorCallback(rv, errno));
613 return rv;
614 } 655 }
615
616 // Notify the watcher only if at least 1 byte was read.
617 if (rv > 0)
618 NotifySocketPerformanceWatcher();
619
620 net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, rv,
621 buf->data());
622 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
623
624 return rv;
625 } 656 }
626 657
627 void TCPSocketPosix::WriteCompleted(const scoped_refptr<IOBuffer>& buf, 658 void TCPSocketPosix::WriteCompleted(const scoped_refptr<IOBuffer>& buf,
628 const CompletionCallback& callback, 659 const CompletionCallback& callback,
629 int rv) { 660 int rv) {
630 DCHECK_NE(ERR_IO_PENDING, rv); 661 DCHECK_NE(ERR_IO_PENDING, rv);
631 callback.Run(HandleWriteCompleted(buf.get(), rv)); 662 callback.Run(HandleWriteCompleted(buf.get(), rv));
632 } 663 }
633 664
634 int TCPSocketPosix::HandleWriteCompleted(IOBuffer* buf, int rv) { 665 int TCPSocketPosix::HandleWriteCompleted(IOBuffer* buf, int rv) {
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 if (info.tcpi_rtt > 0) { 831 if (info.tcpi_rtt > 0) {
801 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); 832 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt);
802 return true; 833 return true;
803 } 834 }
804 } 835 }
805 #endif // defined(TCP_INFO) 836 #endif // defined(TCP_INFO)
806 return false; 837 return false;
807 } 838 }
808 839
809 } // namespace net 840 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_socket_posix.h ('k') | net/socket/tcp_socket_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698