summaryrefslogtreecommitdiffstats
path: root/media/audio/async_socket_io_handler_win.cc
diff options
context:
space:
mode:
authortommi@chromium.org <tommi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-07 12:45:51 +0000
committertommi@chromium.org <tommi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-07 12:45:51 +0000
commita0e3f0fe89bf3d1a21ea6b078c1c4bcab353069c (patch)
treef2e9027dbceedaaaed995ddd546a07f425b1d648 /media/audio/async_socket_io_handler_win.cc
parentc57ccd8e0dce78d664c79b320079487c8094198a (diff)
downloadchromium_src-a0e3f0fe89bf3d1a21ea6b078c1c4bcab353069c.zip
chromium_src-a0e3f0fe89bf3d1a21ea6b078c1c4bcab353069c.tar.gz
chromium_src-a0e3f0fe89bf3d1a21ea6b078c1c4bcab353069c.tar.bz2
Add support to be able to asynchronously read from a CancelableSyncSocket
on a TYPE_IO message loop thread. This makes it easy to share a thread that uses a message loop (e.g. for IPC and other things) and not require a separate thread to read from the socket. TEST=Run media_unittests. (--gtest_filter=AsyncSocketIoHandlerTest.*) Review URL: https://chromiumcodereview.appspot.com/10540047 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@140994 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/async_socket_io_handler_win.cc')
-rw-r--r--media/audio/async_socket_io_handler_win.cc74
1 files changed, 74 insertions, 0 deletions
diff --git a/media/audio/async_socket_io_handler_win.cc b/media/audio/async_socket_io_handler_win.cc
new file mode 100644
index 0000000..aad3823
--- /dev/null
+++ b/media/audio/async_socket_io_handler_win.cc
@@ -0,0 +1,74 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/audio/async_socket_io_handler.h"
+
+namespace media {
+
+AsyncSocketIoHandler::AsyncSocketIoHandler()
+ : socket_(base::SyncSocket::kInvalidHandle),
+ context_(NULL) {}
+
+AsyncSocketIoHandler::~AsyncSocketIoHandler() {
+ // We need to be deleted on the correct thread to avoid racing with the
+ // message loop thread.
+ DCHECK(CalledOnValidThread());
+
+ if (context_) {
+ if (!read_complete_.is_null()) {
+ // Make the context be deleted by the message pump when done.
+ context_->handler = NULL;
+ } else {
+ delete context_;
+ }
+ }
+}
+
+// Implementation of IOHandler on Windows.
+void AsyncSocketIoHandler::OnIOCompleted(MessageLoopForIO::IOContext* context,
+ DWORD bytes_transfered,
+ 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();
+ }
+}
+
+bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len,
+ const ReadCompleteCallback& callback) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(read_complete_.is_null());
+ DCHECK_NE(socket_, base::SyncSocket::kInvalidHandle);
+
+ read_complete_ = callback;
+
+ DWORD bytes_read = 0;
+ BOOL ok = ::ReadFile(socket_, buffer, buffer_len, &bytes_read,
+ &context_->overlapped);
+ // The completion port will be signaled regardless of completing the read
+ // 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;
+}
+
+bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket) {
+ DCHECK(!context_);
+ DCHECK_EQ(socket_, base::SyncSocket::kInvalidHandle);
+
+ DetachFromThread();
+
+ socket_ = socket;
+ MessageLoopForIO::current()->RegisterIOHandler(socket, this);
+
+ context_ = new MessageLoopForIO::IOContext();
+ context_->handler = this;
+ memset(&context_->overlapped, 0, sizeof(context_->overlapped));
+
+ return true;
+}
+
+} // namespace media.