Chromium Code Reviews| Index: media/audio/async_socket_io_handler_win.cc |
| diff --git a/media/audio/async_socket_io_handler_win.cc b/media/audio/async_socket_io_handler_win.cc |
| index aad3823eda920af0940123ed45a2835d6bfc354c..f83f405ed1245fe77928e4f71e19473e2e0636e5 100644 |
| --- a/media/audio/async_socket_io_handler_win.cc |
| +++ b/media/audio/async_socket_io_handler_win.cc |
| @@ -8,7 +8,8 @@ namespace media { |
| AsyncSocketIoHandler::AsyncSocketIoHandler() |
| : socket_(base::SyncSocket::kInvalidHandle), |
| - context_(NULL) {} |
| + context_(NULL), |
| + is_pending_(false) {} |
| AsyncSocketIoHandler::~AsyncSocketIoHandler() { |
| // We need to be deleted on the correct thread to avoid racing with the |
| @@ -16,7 +17,7 @@ AsyncSocketIoHandler::~AsyncSocketIoHandler() { |
| DCHECK(CalledOnValidThread()); |
| if (context_) { |
| - if (!read_complete_.is_null()) { |
| + if (is_pending_) { |
| // Make the context be deleted by the message pump when done. |
| context_->handler = NULL; |
| } else { |
| @@ -31,20 +32,17 @@ void AsyncSocketIoHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, |
| DWORD error) { |
| DCHECK(CalledOnValidThread()); |
| DCHECK_EQ(context_, context); |
| - if (!read_complete_.is_null()) { |
| - read_complete_.Run(error == ERROR_SUCCESS ? bytes_transfered : 0); |
| - read_complete_.Reset(); |
| - } |
| + DCHECK(!read_complete_.is_null()); |
| + is_pending_ = false; |
| + read_complete_.Run(error == ERROR_SUCCESS ? bytes_transfered : 0); |
| } |
| -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(!is_pending_); |
| DCHECK_NE(socket_, base::SyncSocket::kInvalidHandle); |
| - read_complete_ = callback; |
| - |
| DWORD bytes_read = 0; |
| BOOL ok = ::ReadFile(socket_, buffer, buffer_len, &bytes_read, |
| &context_->overlapped); |
| @@ -52,16 +50,20 @@ bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len, |
| // straight away or asynchronously (ERROR_IO_PENDING). OnIOCompleted() will |
| // be called regardless and we don't need to explicitly run the callback |
| // in the case where ok is FALSE and GLE==ERROR_IO_PENDING. |
| - return ok || GetLastError() == ERROR_IO_PENDING; |
| + is_pending_ = !ok && (GetLastError() == ERROR_IO_PENDING); |
|
no longer working on chromium
2012/07/04 09:54:55
From the code, it seems that we run the callback o
tommi (sloooow) - chröme
2012/07/04 12:12:40
That's correct.
|
| + return ok || is_pending_; |
| } |
| -bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket) { |
| +bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket, |
| + const ReadCompleteCallback& callback) { |
| DCHECK(!context_); |
| DCHECK_EQ(socket_, base::SyncSocket::kInvalidHandle); |
| DetachFromThread(); |
| socket_ = socket; |
| + read_complete_ = callback; |
| + |
| MessageLoopForIO::current()->RegisterIOHandler(socket, this); |
| context_ = new MessageLoopForIO::IOContext(); |