blob: 7497c500c09222fba37eb755211e62cee8e8161f [file] [log] [blame]
[email protected]a0e3f0f2012-06-07 12:45:511// 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#ifndef MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_
6#define MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_
7
8#include "base/message_loop.h"
9#include "base/sync_socket.h"
10#include "base/threading/non_thread_safe.h"
11#include "media/base/media_export.h"
12
13namespace media {
14
15// The message loop callback interface is different based on platforms.
16#if defined(OS_WIN)
[email protected]fb5af232013-04-22 22:40:0317typedef base::MessageLoopForIO::IOHandler MessageLoopIOHandler;
[email protected]a0e3f0f2012-06-07 12:45:5118#elif defined(OS_POSIX)
[email protected]fb5af232013-04-22 22:40:0319typedef base::MessageLoopForIO::Watcher MessageLoopIOHandler;
[email protected]a0e3f0f2012-06-07 12:45:5120#endif
21
22// Extends the CancelableSyncSocket class to allow reading from a socket
23// asynchronously on a TYPE_IO message loop thread. This makes it easy to share
24// a thread that uses a message loop (e.g. for IPC and other things) and not
25// require a separate thread to read from the socket.
26//
27// Example usage (also see the unit tests):
28//
29// class SocketReader {
30// public:
31// SocketReader(base::CancelableSyncSocket* socket)
32// : socket_(socket), buffer_() {
[email protected]26195ea2012-07-04 13:34:2133// io_handler.Initialize(socket_->handle(),
34// base::Bind(&SocketReader::OnDataAvailable,
35// base::Unretained(this));
[email protected]a0e3f0f2012-06-07 12:45:5136// }
37//
38// void AsyncRead() {
[email protected]26195ea2012-07-04 13:34:2139// CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_)));
[email protected]a0e3f0f2012-06-07 12:45:5140// }
41//
42// private:
43// void OnDataAvailable(int bytes_read) {
[email protected]26195ea2012-07-04 13:34:2144// if (ProcessData(&buffer_[0], bytes_read)) {
45// // Issue another read.
46// CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_)));
47// }
[email protected]a0e3f0f2012-06-07 12:45:5148// }
49//
50// media::AsyncSocketIoHandler io_handler;
51// base::CancelableSyncSocket* socket_;
52// char buffer_[kBufferSize];
53// };
54//
55class MEDIA_EXPORT AsyncSocketIoHandler
[email protected]00994922012-06-07 12:59:5056 : public NON_EXPORTED_BASE(base::NonThreadSafe),
57 public NON_EXPORTED_BASE(MessageLoopIOHandler) {
[email protected]a0e3f0f2012-06-07 12:45:5158 public:
59 AsyncSocketIoHandler();
60 virtual ~AsyncSocketIoHandler();
61
[email protected]a0e3f0f2012-06-07 12:45:5162 // Type definition for the callback. The parameter tells how many
63 // bytes were read and is 0 if an error occurred.
64 typedef base::Callback<void(int)> ReadCompleteCallback;
65
[email protected]26195ea2012-07-04 13:34:2166 // Initializes the AsyncSocketIoHandler by hooking it up to the current
67 // thread's message loop (must be TYPE_IO), to do async reads from the socket
68 // on the current thread. The |callback| will be invoked whenever a Read()
69 // has completed.
70 bool Initialize(base::SyncSocket::Handle socket,
71 const ReadCompleteCallback& callback);
72
[email protected]a0e3f0f2012-06-07 12:45:5173 // Attempts to read from the socket. The return value will be |false|
74 // if an error occurred and |true| if data was read or a pending read
[email protected]26195ea2012-07-04 13:34:2175 // was issued. Regardless of async or sync operation, the
76 // ReadCompleteCallback (see above) will be called when data is available.
77 bool Read(char* buffer, int buffer_len);
[email protected]a0e3f0f2012-06-07 12:45:5178
79 private:
80#if defined(OS_WIN)
81 // Implementation of IOHandler on Windows.
[email protected]fb5af232013-04-22 22:40:0382 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
[email protected]a0e3f0f2012-06-07 12:45:5183 DWORD bytes_transfered,
84 DWORD error) OVERRIDE;
85#elif defined(OS_POSIX)
[email protected]fb5af232013-04-22 22:40:0386 // Implementation of base::MessageLoopForIO::Watcher.
[email protected]a0e3f0f2012-06-07 12:45:5187 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {}
88 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE;
89
90 void EnsureWatchingSocket();
91#endif
92
93 base::SyncSocket::Handle socket_;
94#if defined(OS_WIN)
[email protected]fb5af232013-04-22 22:40:0395 base::MessageLoopForIO::IOContext* context_;
[email protected]26195ea2012-07-04 13:34:2196 bool is_pending_;
[email protected]a0e3f0f2012-06-07 12:45:5197#elif defined(OS_POSIX)
[email protected]fb5af232013-04-22 22:40:0398 base::MessageLoopForIO::FileDescriptorWatcher socket_watcher_;
[email protected]a0e3f0f2012-06-07 12:45:5199 // |pending_buffer_| and |pending_buffer_len_| are valid only between
100 // Read() and OnFileCanReadWithoutBlocking().
101 char* pending_buffer_;
102 int pending_buffer_len_;
103 // |true| iff the message loop is watching the socket for IO events.
104 bool is_watching_;
105#endif
106 ReadCompleteCallback read_complete_;
107
108 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler);
109};
110
111} // namespace media.
112
113#endif // MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_