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

Side by Side Diff: net/socket/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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/socket_posix.h" 5 #include "net/socket/socket_posix.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <netinet/in.h> 8 #include <netinet/in.h>
9 #include <sys/socket.h> 9 #include <sys/socket.h>
10 #include <utility> 10 #include <utility>
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 PLOG(ERROR) << "WatchFileDescriptor failed on read, errno " << errno; 268 PLOG(ERROR) << "WatchFileDescriptor failed on read, errno " << errno;
269 return MapSystemError(errno); 269 return MapSystemError(errno);
270 } 270 }
271 271
272 read_buf_ = buf; 272 read_buf_ = buf;
273 read_buf_len_ = buf_len; 273 read_buf_len_ = buf_len;
274 read_callback_ = callback; 274 read_callback_ = callback;
275 return ERR_IO_PENDING; 275 return ERR_IO_PENDING;
276 } 276 }
277 277
278 int SocketPosix::ReadIfReady(IOBuffer* buf,
279 int buf_len,
280 const CompletionCallback& callback) {
281 // Reuse Read() but do not hold on to |buf| and |buf_len|.
282 int rv = Read(buf, buf_len, callback);
283 read_buf_ = nullptr;
284 read_buf_len_ = 0;
285 return rv;
286 }
davidben 2017/02/01 22:25:57 Nit: It might be a little clearer to make lines 25
xunjieli 2017/02/03 16:35:33 Done. I like your approach better. It's easier to
287
278 int SocketPosix::Write(IOBuffer* buf, 288 int SocketPosix::Write(IOBuffer* buf,
279 int buf_len, 289 int buf_len,
280 const CompletionCallback& callback) { 290 const CompletionCallback& callback) {
281 DCHECK(thread_checker_.CalledOnValidThread()); 291 DCHECK(thread_checker_.CalledOnValidThread());
282 DCHECK_NE(kInvalidSocket, socket_fd_); 292 DCHECK_NE(kInvalidSocket, socket_fd_);
283 DCHECK(!waiting_connect_); 293 DCHECK(!waiting_connect_);
284 CHECK(write_callback_.is_null()); 294 CHECK(write_callback_.is_null());
285 // Synchronous operation not supported 295 // Synchronous operation not supported
286 DCHECK(!callback.is_null()); 296 DCHECK(!callback.is_null());
287 DCHECK_LT(0, buf_len); 297 DCHECK_LT(0, buf_len);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 waiting_connect_ = false; 454 waiting_connect_ = false;
445 base::ResetAndReturn(&write_callback_).Run(rv); 455 base::ResetAndReturn(&write_callback_).Run(rv);
446 } 456 }
447 457
448 int SocketPosix::DoRead(IOBuffer* buf, int buf_len) { 458 int SocketPosix::DoRead(IOBuffer* buf, int buf_len) {
449 int rv = HANDLE_EINTR(read(socket_fd_, buf->data(), buf_len)); 459 int rv = HANDLE_EINTR(read(socket_fd_, buf->data(), buf_len));
450 return rv >= 0 ? rv : MapSystemError(errno); 460 return rv >= 0 ? rv : MapSystemError(errno);
451 } 461 }
452 462
453 void SocketPosix::ReadCompleted() { 463 void SocketPosix::ReadCompleted() {
464 // If |read_buf_| is nullptr, ReadIfReady() is used instead of Read(), so
465 // do not continue reading but wait for the caller to retry.
466 if (read_buf_ == nullptr) {
467 bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
468 DCHECK(ok);
469 base::ResetAndReturn(&read_callback_).Run(OK);
470 return;
471 }
454 int rv = DoRead(read_buf_.get(), read_buf_len_); 472 int rv = DoRead(read_buf_.get(), read_buf_len_);
455 if (rv == ERR_IO_PENDING) 473 if (rv == ERR_IO_PENDING)
456 return; 474 return;
457 475
458 bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); 476 bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
459 DCHECK(ok); 477 DCHECK(ok);
460 read_buf_ = NULL; 478 read_buf_ = NULL;
461 read_buf_len_ = 0; 479 read_buf_len_ = 0;
462 base::ResetAndReturn(&read_callback_).Run(rv); 480 base::ResetAndReturn(&read_callback_).Run(rv);
davidben 2017/02/01 22:25:57 You could also write this: int rv = OK; // If
xunjieli 2017/02/03 16:35:33 Acknowledged. Thanks. This is no longer applicable
463 } 481 }
464 482
465 int SocketPosix::DoWrite(IOBuffer* buf, int buf_len) { 483 int SocketPosix::DoWrite(IOBuffer* buf, int buf_len) {
466 #if defined(OS_LINUX) || defined(OS_ANDROID) 484 #if defined(OS_LINUX) || defined(OS_ANDROID)
467 // Disable SIGPIPE for this write. Although Chromium globally disables 485 // Disable SIGPIPE for this write. Although Chromium globally disables
468 // SIGPIPE, the net stack may be used in other consumers which do not do 486 // SIGPIPE, the net stack may be used in other consumers which do not do
469 // this. MSG_NOSIGNAL is a Linux-only API. On OS X, this is a setsockopt on 487 // this. MSG_NOSIGNAL is a Linux-only API. On OS X, this is a setsockopt on
470 // socket creation. 488 // socket creation.
471 int rv = HANDLE_EINTR(send(socket_fd_, buf->data(), buf_len, MSG_NOSIGNAL)); 489 int rv = HANDLE_EINTR(send(socket_fd_, buf->data(), buf_len, MSG_NOSIGNAL));
472 #else 490 #else
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 write_buf_ = NULL; 528 write_buf_ = NULL;
511 write_buf_len_ = 0; 529 write_buf_len_ = 0;
512 write_callback_.Reset(); 530 write_callback_.Reset();
513 } 531 }
514 532
515 waiting_connect_ = false; 533 waiting_connect_ = false;
516 peer_address_.reset(); 534 peer_address_.reset();
517 } 535 }
518 536
519 } // namespace net 537 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698