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

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

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Fix tests for real Created 3 years, 10 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
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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
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);
591 callback.Run(HandleReadCompleted(buf.get(), rv)); 605 callback.Run(HandleReadCompleted(buf.get(), rv));
592 } 606 }
593 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 if (rv < 0) {
614 net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR,
615 CreateNetLogSocketErrorCallback(rv, errno));
616 }
617 callback.Run(rv);
618 }
619
594 int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) { 620 int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) {
595 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { 621 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) {
davidben 2017/02/01 22:25:58 Should this code also run in the ReadIfReadyComple
xunjieli 2017/02/03 16:35:33 Done.
596 // A TCP FastOpen connect-with-write was attempted. This read was a 622 // A TCP FastOpen connect-with-write was attempted. This read was a
597 // subsequent read, which either succeeded or failed. If the read 623 // subsequent read, which either succeeded or failed. If the read
598 // succeeded, the socket is considered connected via TCP FastOpen. 624 // succeeded, the socket is considered connected via TCP FastOpen.
599 // If the read failed, TCP FastOpen is (conservatively) turned off for all 625 // If the read failed, TCP FastOpen is (conservatively) turned off for all
600 // subsequent connections. TCP FastOpen status is recorded in both cases. 626 // subsequent connections. TCP FastOpen status is recorded in both cases.
601 // TODO (jri): This currently results in conservative behavior, where TCP 627 // TODO (jri): This currently results in conservative behavior, where TCP
602 // FastOpen is turned off on _any_ error. Implement optimizations, 628 // FastOpen is turned off on _any_ error. Implement optimizations,
603 // such as turning off TCP FastOpen on more specific errors, and 629 // such as turning off TCP FastOpen on more specific errors, and
604 // re-attempting TCP FastOpen after a certain amount of time has passed. 630 // re-attempting TCP FastOpen after a certain amount of time has passed.
605 if (rv >= 0) 631 if (rv >= 0)
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 if (info.tcpi_rtt > 0) { 836 if (info.tcpi_rtt > 0) {
811 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); 837 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt);
812 return true; 838 return true;
813 } 839 }
814 } 840 }
815 #endif // defined(TCP_INFO) 841 #endif // defined(TCP_INFO)
816 return false; 842 return false;
817 } 843 }
818 844
819 } // namespace net 845 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698