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

Unified Diff: media/audio/async_socket_io_handler_win.cc

Issue 10540047: Add support to be able to asynchronously read from a CancelableSyncSocket (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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_win.cc
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 0000000000000000000000000000000000000000..8329a2ce99e51064c284dfb9aff93b1bb235fa79
--- /dev/null
+++ b/media/audio/async_socket_io_handler_win.cc
@@ -0,0 +1,77 @@
+// 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);
henrika (OOO until Aug 14) 2012/06/07 12:25:59 fix indentation
tommi (sloooow) - chröme 2012/06/07 12:34:33 Done.
+ if (!ok && GetLastError() != ERROR_IO_PENDING)
+ return false;
+
+ // The completion port will be signaled regardless of completing the read
henrika (OOO until Aug 14) 2012/06/07 12:25:59 Odd place to place a comment.
tommi (sloooow) - chröme 2012/06/07 12:34:33 Done. Also improved the comment.
+ // straight away or asynchronously, so we always set the pending flag until
+ // we get the callback.
+
+ return true;
+}
+
+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.

Powered by Google App Engine
This is Rietveld 408576698