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

Unified Diff: net/socket/tcp_client_socket.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_client_socket.h ('k') | net/socket/tcp_socket_posix.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/socket/tcp_client_socket.cc
diff --git a/net/socket/tcp_client_socket.cc b/net/socket/tcp_client_socket.cc
index 01ce316ee14f54761fa1eefa7da3bc464383fb0a..9e1470ffcdb02e14bff1c1b31a512169231be68b 100644
--- a/net/socket/tcp_client_socket.cc
+++ b/net/socket/tcp_client_socket.cc
@@ -100,6 +100,26 @@ int TCPClientSocket::Connect(const CompletionCallback& callback) {
return rv;
}
+int TCPClientSocket::ReadCommon(IOBuffer* buf,
+ int buf_len,
+ const CompletionCallback& callback,
+ bool read_if_ready) {
+ DCHECK(!callback.is_null());
+
+ // |socket_| is owned by |this| and the callback won't be run once |socket_|
+ // is gone/closed. Therefore, it is safe to use base::Unretained() here.
+ CompletionCallback read_callback = base::Bind(
+ &TCPClientSocket::DidCompleteRead, base::Unretained(this), callback);
+ int result = read_if_ready ? socket_->ReadIfReady(buf, buf_len, read_callback)
+ : socket_->Read(buf, buf_len, read_callback);
+ if (result > 0) {
+ use_history_.set_was_used_to_convey_data();
+ total_received_bytes_ += result;
+ }
+
+ return result;
+}
+
int TCPClientSocket::DoConnectLoop(int result) {
DCHECK_NE(next_connect_state_, CONNECT_STATE_NONE);
@@ -272,19 +292,13 @@ bool TCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
int TCPClientSocket::Read(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) {
- DCHECK(!callback.is_null());
-
- // |socket_| is owned by this class and the callback won't be run once
- // |socket_| is gone. Therefore, it is safe to use base::Unretained() here.
- CompletionCallback read_callback = base::Bind(
- &TCPClientSocket::DidCompleteRead, base::Unretained(this), callback);
- int result = socket_->Read(buf, buf_len, read_callback);
- if (result > 0) {
- use_history_.set_was_used_to_convey_data();
- total_received_bytes_ += result;
- }
+ return ReadCommon(buf, buf_len, callback, /*read_if_ready=*/false);
+}
- return result;
+int TCPClientSocket::ReadIfReady(IOBuffer* buf,
+ int buf_len,
+ const CompletionCallback& callback) {
+ return ReadCommon(buf, buf_len, callback, /*read_if_ready=*/true);
}
int TCPClientSocket::Write(IOBuffer* buf,
« no previous file with comments | « net/socket/tcp_client_socket.h ('k') | net/socket/tcp_socket_posix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698