Chromium Code Reviews
[email protected] (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

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: 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
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 // Private implementation.
henrika (OOO until Aug 14) 2012/06/07 12:25:59 Remove comment?
tommi (sloooow) - chröme 2012/06/07 12:34:33 Done.
77
78 #if defined(OS_WIN)
79 // Implementation of IOHandler on Windows.
80 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
81 DWORD bytes_transfered,
82 DWORD error) OVERRIDE;
83 #elif defined(OS_POSIX)
84 // Implementation of MessageLoopForIO::Watcher.
85 virtual void OnFileCanWriteWithoutBlocking(int socket) OVERRIDE {}
86 virtual void OnFileCanReadWithoutBlocking(int socket) OVERRIDE;
87
88 void EnsureWatchingSocket();
89 #endif
90
91 // Member variables.
henrika (OOO until Aug 14) 2012/06/07 12:25:59 Needed?
tommi (sloooow) - chröme 2012/06/07 12:34:33 Done.
92
93 base::SyncSocket::Handle socket_;
94 #if defined(OS_WIN)
95 MessageLoopForIO::IOContext* context_;
96 #elif defined(OS_POSIX)
97 MessageLoopForIO::FileDescriptorWatcher socket_watcher_;
98 // |pending_buffer_| and |pending_buffer_len_| are valid only between
99 // Read() and OnFileCanReadWithoutBlocking().
100 char* pending_buffer_;
101 int pending_buffer_len_;
102 // |true| iff the message loop is watching the socket for IO events.
103 bool is_watching_;
104 #endif
105 ReadCompleteCallback read_complete_;
106
107 DISALLOW_COPY_AND_ASSIGN(AsyncSocketIoHandler);
108 };
109
110 } // namespace media.
111
112 #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') | media/audio/async_socket_io_handler_posix.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698