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

Unified Diff: media/audio/async_socket_io_handler_posix.cc

Issue 10697069: Move the callback out of the Read method and into Initialize to make Read loops simpler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix expectations for the synchronous test Created 8 years, 5 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
Index: media/audio/async_socket_io_handler_posix.cc
diff --git a/media/audio/async_socket_io_handler_posix.cc b/media/audio/async_socket_io_handler_posix.cc
index c4d3169640f2a77ebc4c16011c5afa5778704b2f..c5f9ae8d960ddf440ff5926dec3d35ab6e188be6 100644
--- a/media/audio/async_socket_io_handler_posix.cc
+++ b/media/audio/async_socket_io_handler_posix.cc
@@ -20,12 +20,15 @@ AsyncSocketIoHandler::~AsyncSocketIoHandler() {
void AsyncSocketIoHandler::OnFileCanReadWithoutBlocking(int socket) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(socket, socket_);
- if (!read_complete_.is_null()) {
+ DCHECK(!read_complete_.is_null());
+
+ if (pending_buffer_) {
int bytes_read = HANDLE_EINTR(read(socket_, pending_buffer_,
pending_buffer_len_));
DCHECK_GT(bytes_read, 0);
+ pending_buffer_ = NULL;
+ pending_buffer_len_ = 0;
read_complete_.Run(bytes_read > 0 ? bytes_read : 0);
- read_complete_.Reset();
} else {
// We're getting notifications that we can read from the socket while
// we're not waiting for data. In order to not starve the message loop,
@@ -35,17 +38,16 @@ void AsyncSocketIoHandler::OnFileCanReadWithoutBlocking(int socket) {
}
}
-bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len,
- const ReadCompleteCallback& callback) {
+bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len) {
DCHECK(CalledOnValidThread());
- DCHECK(read_complete_.is_null());
+ DCHECK(!read_complete_.is_null());
+ DCHECK(!pending_buffer_);
EnsureWatchingSocket();
int bytes_read = HANDLE_EINTR(read(socket_, buffer, buffer_len));
if (bytes_read < 0) {
if (errno == EAGAIN) {
- read_complete_ = callback;
pending_buffer_ = buffer;
pending_buffer_len_ = buffer_len;
} else {
@@ -53,17 +55,19 @@ bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len,
return false;
}
} else {
- callback.Run(bytes_read);
+ read_complete_.Run(bytes_read);
}
return true;
}
-bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket) {
+bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket,
+ const ReadCompleteCallback& callback) {
DCHECK_EQ(socket_, base::SyncSocket::kInvalidHandle);
DetachFromThread();
socket_ = socket;
+ read_complete_ = callback;
// SyncSocket is blocking by default, so let's convert it to non-blocking.
int value = fcntl(socket, F_GETFL);

Powered by Google App Engine
This is Rietveld 408576698