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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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_client_socket.h" 5 #include "net/socket/tcp_client_socket.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 int rv = DoConnectLoop(OK); 93 int rv = DoConnectLoop(OK);
94 if (rv == ERR_IO_PENDING) { 94 if (rv == ERR_IO_PENDING) {
95 connect_callback_ = callback; 95 connect_callback_ = callback;
96 } else { 96 } else {
97 socket_->EndLoggingMultipleConnectAttempts(rv); 97 socket_->EndLoggingMultipleConnectAttempts(rv);
98 } 98 }
99 99
100 return rv; 100 return rv;
101 } 101 }
102 102
103 int TCPClientSocket::ReadCommon(IOBuffer* buf,
104 int buf_len,
105 const CompletionCallback& callback,
106 bool read_if_ready) {
107 DCHECK(!callback.is_null());
108
109 // |socket_| is owned by |this| and the callback won't be run once |socket_|
110 // is gone/closed. Therefore, it is safe to use base::Unretained() here.
111 CompletionCallback read_callback = base::Bind(
112 &TCPClientSocket::DidCompleteRead, base::Unretained(this), callback);
113 int result = read_if_ready ? socket_->ReadIfReady(buf, buf_len, read_callback)
114 : socket_->Read(buf, buf_len, read_callback);
115 if (result > 0) {
116 use_history_.set_was_used_to_convey_data();
117 total_received_bytes_ += result;
118 }
119
120 return result;
121 }
122
103 int TCPClientSocket::DoConnectLoop(int result) { 123 int TCPClientSocket::DoConnectLoop(int result) {
104 DCHECK_NE(next_connect_state_, CONNECT_STATE_NONE); 124 DCHECK_NE(next_connect_state_, CONNECT_STATE_NONE);
105 125
106 int rv = result; 126 int rv = result;
107 do { 127 do {
108 ConnectState state = next_connect_state_; 128 ConnectState state = next_connect_state_;
109 next_connect_state_ = CONNECT_STATE_NONE; 129 next_connect_state_ = CONNECT_STATE_NONE;
110 switch (state) { 130 switch (state) {
111 case CONNECT_STATE_CONNECT: 131 case CONNECT_STATE_CONNECT:
112 DCHECK_EQ(OK, rv); 132 DCHECK_EQ(OK, rv);
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 return kProtoUnknown; 285 return kProtoUnknown;
266 } 286 }
267 287
268 bool TCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { 288 bool TCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
269 return false; 289 return false;
270 } 290 }
271 291
272 int TCPClientSocket::Read(IOBuffer* buf, 292 int TCPClientSocket::Read(IOBuffer* buf,
273 int buf_len, 293 int buf_len,
274 const CompletionCallback& callback) { 294 const CompletionCallback& callback) {
275 DCHECK(!callback.is_null()); 295 return ReadCommon(buf, buf_len, callback, /*read_if_ready=*/false);
296 }
276 297
277 // |socket_| is owned by this class and the callback won't be run once 298 int TCPClientSocket::ReadIfReady(IOBuffer* buf,
278 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. 299 int buf_len,
279 CompletionCallback read_callback = base::Bind( 300 const CompletionCallback& callback) {
280 &TCPClientSocket::DidCompleteRead, base::Unretained(this), callback); 301 return ReadCommon(buf, buf_len, callback, /*read_if_ready=*/true);
281 int result = socket_->Read(buf, buf_len, read_callback);
282 if (result > 0) {
283 use_history_.set_was_used_to_convey_data();
284 total_received_bytes_ += result;
285 }
286
287 return result;
288 } 302 }
289 303
290 int TCPClientSocket::Write(IOBuffer* buf, 304 int TCPClientSocket::Write(IOBuffer* buf,
291 int buf_len, 305 int buf_len,
292 const CompletionCallback& callback) { 306 const CompletionCallback& callback) {
293 DCHECK(!callback.is_null()); 307 DCHECK(!callback.is_null());
294 308
295 // |socket_| is owned by this class and the callback won't be run once 309 // |socket_| is owned by this class and the callback won't be run once
296 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here. 310 // |socket_| is gone. Therefore, it is safe to use base::Unretained() here.
297 CompletionCallback write_callback = base::Bind( 311 CompletionCallback write_callback = base::Bind(
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 void TCPClientSocket::EmitTCPMetricsHistogramsOnDisconnect() { 403 void TCPClientSocket::EmitTCPMetricsHistogramsOnDisconnect() {
390 base::TimeDelta rtt; 404 base::TimeDelta rtt;
391 if (socket_->GetEstimatedRoundTripTime(&rtt)) { 405 if (socket_->GetEstimatedRoundTripTime(&rtt)) {
392 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TcpRtt.AtDisconnect", rtt, 406 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TcpRtt.AtDisconnect", rtt,
393 base::TimeDelta::FromMilliseconds(1), 407 base::TimeDelta::FromMilliseconds(1),
394 base::TimeDelta::FromMinutes(10), 100); 408 base::TimeDelta::FromMinutes(10), 100);
395 } 409 }
396 } 410 }
397 411
398 } // namespace net 412 } // namespace net
OLDNEW
« 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