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

Side by Side Diff: media/audio/async_socket_io_handler_win.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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/audio/async_socket_io_handler_unittest.cc ('k') | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/audio/async_socket_io_handler.h" 5 #include "media/audio/async_socket_io_handler.h"
6 6
7 namespace media { 7 namespace media {
8 8
9 AsyncSocketIoHandler::AsyncSocketIoHandler() 9 AsyncSocketIoHandler::AsyncSocketIoHandler()
10 : socket_(base::SyncSocket::kInvalidHandle), 10 : socket_(base::SyncSocket::kInvalidHandle),
11 context_(NULL) {} 11 context_(NULL),
12 is_pending_(false) {}
12 13
13 AsyncSocketIoHandler::~AsyncSocketIoHandler() { 14 AsyncSocketIoHandler::~AsyncSocketIoHandler() {
14 // We need to be deleted on the correct thread to avoid racing with the 15 // We need to be deleted on the correct thread to avoid racing with the
15 // message loop thread. 16 // message loop thread.
16 DCHECK(CalledOnValidThread()); 17 DCHECK(CalledOnValidThread());
17 18
18 if (context_) { 19 if (context_) {
19 if (!read_complete_.is_null()) { 20 if (is_pending_) {
20 // Make the context be deleted by the message pump when done. 21 // Make the context be deleted by the message pump when done.
21 context_->handler = NULL; 22 context_->handler = NULL;
22 } else { 23 } else {
23 delete context_; 24 delete context_;
24 } 25 }
25 } 26 }
26 } 27 }
27 28
28 // Implementation of IOHandler on Windows. 29 // Implementation of IOHandler on Windows.
29 void AsyncSocketIoHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, 30 void AsyncSocketIoHandler::OnIOCompleted(MessageLoopForIO::IOContext* context,
30 DWORD bytes_transfered, 31 DWORD bytes_transfered,
31 DWORD error) { 32 DWORD error) {
32 DCHECK(CalledOnValidThread()); 33 DCHECK(CalledOnValidThread());
33 DCHECK_EQ(context_, context); 34 DCHECK_EQ(context_, context);
34 if (!read_complete_.is_null()) { 35 DCHECK(!read_complete_.is_null());
35 read_complete_.Run(error == ERROR_SUCCESS ? bytes_transfered : 0); 36 is_pending_ = false;
36 read_complete_.Reset(); 37 read_complete_.Run(error == ERROR_SUCCESS ? bytes_transfered : 0);
37 }
38 } 38 }
39 39
40 bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len, 40 bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len) {
41 const ReadCompleteCallback& callback) {
42 DCHECK(CalledOnValidThread()); 41 DCHECK(CalledOnValidThread());
43 DCHECK(read_complete_.is_null()); 42 DCHECK(!read_complete_.is_null());
43 DCHECK(!is_pending_);
44 DCHECK_NE(socket_, base::SyncSocket::kInvalidHandle); 44 DCHECK_NE(socket_, base::SyncSocket::kInvalidHandle);
45 45
46 read_complete_ = callback;
47
48 DWORD bytes_read = 0; 46 DWORD bytes_read = 0;
49 BOOL ok = ::ReadFile(socket_, buffer, buffer_len, &bytes_read, 47 BOOL ok = ::ReadFile(socket_, buffer, buffer_len, &bytes_read,
50 &context_->overlapped); 48 &context_->overlapped);
51 // The completion port will be signaled regardless of completing the read 49 // The completion port will be signaled regardless of completing the read
52 // straight away or asynchronously (ERROR_IO_PENDING). OnIOCompleted() will 50 // straight away or asynchronously (ERROR_IO_PENDING). OnIOCompleted() will
53 // be called regardless and we don't need to explicitly run the callback 51 // be called regardless and we don't need to explicitly run the callback
54 // in the case where ok is FALSE and GLE==ERROR_IO_PENDING. 52 // in the case where ok is FALSE and GLE==ERROR_IO_PENDING.
55 return ok || GetLastError() == ERROR_IO_PENDING; 53 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.
54 return ok || is_pending_;
56 } 55 }
57 56
58 bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket) { 57 bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket,
58 const ReadCompleteCallback& callback) {
59 DCHECK(!context_); 59 DCHECK(!context_);
60 DCHECK_EQ(socket_, base::SyncSocket::kInvalidHandle); 60 DCHECK_EQ(socket_, base::SyncSocket::kInvalidHandle);
61 61
62 DetachFromThread(); 62 DetachFromThread();
63 63
64 socket_ = socket; 64 socket_ = socket;
65 read_complete_ = callback;
66
65 MessageLoopForIO::current()->RegisterIOHandler(socket, this); 67 MessageLoopForIO::current()->RegisterIOHandler(socket, this);
66 68
67 context_ = new MessageLoopForIO::IOContext(); 69 context_ = new MessageLoopForIO::IOContext();
68 context_->handler = this; 70 context_->handler = this;
69 memset(&context_->overlapped, 0, sizeof(context_->overlapped)); 71 memset(&context_->overlapped, 0, sizeof(context_->overlapped));
70 72
71 return true; 73 return true;
72 } 74 }
73 75
74 } // namespace media. 76 } // namespace media.
OLDNEW
« no previous file with comments | « media/audio/async_socket_io_handler_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698