OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/audio/async_socket_io_handler.h" | |
6 | |
7 namespace media { | |
8 | |
9 AsyncSocketIoHandler::AsyncSocketIoHandler() | |
10 : socket_(base::SyncSocket::kInvalidHandle), | |
11 context_(NULL) {} | |
12 | |
13 AsyncSocketIoHandler::~AsyncSocketIoHandler() { | |
14 // We need to be deleted on the correct thread to avoid racing with the | |
15 // message loop thread. | |
16 DCHECK(CalledOnValidThread()); | |
17 | |
18 if (context_) { | |
19 if (!read_complete_.is_null()) { | |
20 // Make the context be deleted by the message pump when done. | |
21 context_->handler = NULL; | |
22 } else { | |
23 delete context_; | |
24 } | |
25 } | |
26 } | |
27 | |
28 // Implementation of IOHandler on Windows. | |
29 void AsyncSocketIoHandler::OnIOCompleted(MessageLoopForIO::IOContext* context, | |
30 DWORD bytes_transfered, | |
31 DWORD error) { | |
32 DCHECK(CalledOnValidThread()); | |
33 DCHECK_EQ(context_, context); | |
34 if (!read_complete_.is_null()) { | |
35 read_complete_.Run(error == ERROR_SUCCESS ? bytes_transfered : 0); | |
36 read_complete_.Reset(); | |
37 } | |
38 } | |
39 | |
40 bool AsyncSocketIoHandler::Read(char* buffer, int buffer_len, | |
41 const ReadCompleteCallback& callback) { | |
42 DCHECK(CalledOnValidThread()); | |
43 DCHECK(read_complete_.is_null()); | |
44 DCHECK_NE(socket_, base::SyncSocket::kInvalidHandle); | |
45 | |
46 read_complete_ = callback; | |
47 | |
48 DWORD bytes_read = 0; | |
49 BOOL ok = ::ReadFile(socket_, buffer, buffer_len, &bytes_read, | |
50 &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.
| |
51 if (!ok && GetLastError() != ERROR_IO_PENDING) | |
52 return false; | |
53 | |
54 // 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.
| |
55 // straight away or asynchronously, so we always set the pending flag until | |
56 // we get the callback. | |
57 | |
58 return true; | |
59 } | |
60 | |
61 bool AsyncSocketIoHandler::Initialize(base::SyncSocket::Handle socket) { | |
62 DCHECK(!context_); | |
63 DCHECK_EQ(socket_, base::SyncSocket::kInvalidHandle); | |
64 | |
65 DetachFromThread(); | |
66 | |
67 socket_ = socket; | |
68 MessageLoopForIO::current()->RegisterIOHandler(socket, this); | |
69 | |
70 context_ = new MessageLoopForIO::IOContext(); | |
71 context_->handler = this; | |
72 memset(&context_->overlapped, 0, sizeof(context_->overlapped)); | |
73 | |
74 return true; | |
75 } | |
76 | |
77 } // namespace media. | |
OLD | NEW |