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

Side by Side Diff: media/audio/async_socket_io_handler.h

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: Address comments 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | media/audio/async_socket_io_handler_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #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
13 namespace media {
14
15 // The message loop callback interface is different based on platforms.
16 #if defined(OS_WIN)
17 typedef MessageLoopForIO::IOHandler MessageLoopIOHandler;
18 #elif defined(OS_POSIX)
19 typedef MessageLoopForIO::Watcher MessageLoopIOHandler;
20 #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_() {
33 // io_handler.Initialize(socket_->handle());
34 // }
35 //
36 // void AsyncRead() {
37 // CHECK(io_handler.Read(&buffer_[0], sizeof(buffer_),
38 // base::Bind(&SocketReader::OnDataAvailable,
39 // base::Unretained(this)));
40 // }
41 //
42 // private:
43 // void OnDataAvailable(int bytes_read) {
44 // ProcessData(&buffer_[0], bytes_read);
45 // }
46 //
47 // media::AsyncSocketIoHandler io_handler;
48 // base::CancelableSyncSocket* socket_;
49 // char buffer_[kBufferSize];
50 // };
51 //
52 class MEDIA_EXPORT AsyncSocketIoHandler
53 : public base::NonThreadSafe,
54 public MessageLoopIOHandler {
55 public:
56 AsyncSocketIoHandler();
57 virtual ~AsyncSocketIoHandler();
58
59 // Initializes the AsyncSocketIoHandler by hooking it up to the current
60 // thread's message loop (must be TYPE_IO), to do async reads from the socket
61 // on the current thread.
62 bool Initialize(base::SyncSocket::Handle socket);
63
64 // Type definition for the callback. The parameter tells how many
65 // bytes were read and is 0 if an error occurred.
66 typedef base::Callback<void(int)> ReadCompleteCallback;
67
68 // Attempts to read from the socket. The return value will be |false|
69 // if an error occurred and |true| if data was read or a pending read
70 // was issued. Regardless of async or sync operation, the callback will
71 // be called when data is available.
72 bool Read(char* buffer, int buffer_len,
73 const ReadCompleteCallback& callback);
74
75 private:
76 #if defined(OS_WIN)
77 // Implementation of IOHandler on Windows.
78 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
79 DWORD bytes_transfered,
80 DWORD error) OVERRIDE;
81 #elif defined(OS_POSIX)
82 // Implementation of MessageLoopForIO::Watcher.
83 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {}
84 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE;
85
86 void EnsureWatchingSocket();
87 #endif
88
89 base::SyncSocket::Handle socket_;
90 #if defined(OS_WIN)
91 MessageLoopForIO::IOContext* context_;
92 #elif defined(OS_POSIX)
93 MessageLoopForIO::FileDescriptorWatcher socket_watcher_;
94 // |pending_buffer_| and |pending_buffer_len_| are valid only between
95 // Read() and OnFileCanReadWithoutBlocking().
96 char* pending_buffer_;
97 int pending_buffer_len_;
98 // |true| iff the message loop is watching the socket for IO events.
99 bool is_watching_;
100 #endif
101 ReadCompleteCallback read_complete_;
102
103 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler);
104 };
105
106 } // namespace media.
107
108 #endif // MEDIA_AUDIO_ASYNC_SOCKET_IO_HANDLER_H_
OLDNEW
« no previous file with comments | « no previous file | media/audio/async_socket_io_handler_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698