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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/socket/tcp_socket_posix.cc
diff --git a/net/socket/tcp_socket_posix.cc b/net/socket/tcp_socket_posix.cc
index a37d83b5e724071fcb3e22f2bb8b990b94f34b4a..2dd16420c9fad8b0753518f442bf944b6fbf1332 100644
--- a/net/socket/tcp_socket_posix.cc
+++ b/net/socket/tcp_socket_posix.cc
@@ -293,6 +293,21 @@ int TCPSocketPosix::Read(IOBuffer* buf,
return rv;
}
+int TCPSocketPosix::ReadIfReady(IOBuffer* buf,
+ int buf_len,
+ const CompletionCallback& callback) {
+ DCHECK(socket_);
+ DCHECK(!callback.is_null());
+
+ int rv =
+ socket_->ReadIfReady(buf, buf_len,
+ base::Bind(&TCPSocketPosix::ReadIfReadyCompleted,
+ base::Unretained(this), callback));
+ if (rv != ERR_IO_PENDING)
+ rv = HandleReadCompleted(buf, rv);
+ return rv;
+}
+
int TCPSocketPosix::Write(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) {
@@ -586,10 +601,37 @@ void TCPSocketPosix::ReadCompleted(const scoped_refptr<IOBuffer>& buf,
const CompletionCallback& callback,
int rv) {
DCHECK_NE(ERR_IO_PENDING, rv);
+
callback.Run(HandleReadCompleted(buf.get(), rv));
}
+void TCPSocketPosix::ReadIfReadyCompleted(const CompletionCallback& callback,
+ int rv) {
+ DCHECK_NE(ERR_IO_PENDING, rv);
+ DCHECK_GE(OK, rv);
+
+ HandleReadCompletedHelper(rv);
+ callback.Run(rv);
+}
+
int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) {
+ HandleReadCompletedHelper(rv);
+
+ if (rv < 0)
+ return rv;
+
+ // Notify the watcher only if at least 1 byte was read.
+ if (rv > 0)
+ NotifySocketPerformanceWatcher();
+
+ net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, rv,
+ buf->data());
+ NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
+
+ return rv;
+}
+
+void TCPSocketPosix::HandleReadCompletedHelper(int rv) {
if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) {
// A TCP FastOpen connect-with-write was attempted. This read was a
// subsequent read, which either succeeded or failed. If the read
@@ -610,18 +652,7 @@ int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) {
if (rv < 0) {
net_log_.AddEvent(NetLogEventType::SOCKET_READ_ERROR,
CreateNetLogSocketErrorCallback(rv, errno));
- return rv;
}
-
- // Notify the watcher only if at least 1 byte was read.
- if (rv > 0)
- NotifySocketPerformanceWatcher();
-
- net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, rv,
- buf->data());
- NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
-
- return rv;
}
void TCPSocketPosix::WriteCompleted(const scoped_refptr<IOBuffer>& buf,
« 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